leetcode-151-reverse-words-in-a-string
題目Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
題目連結
Example 1
12Input: s = "the sky is blue&qu ...
leetcode-14-longest-common-prefix
題目Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
題目連結
Example 1
12Input: strs = ["flower","flow","flight"]Output: "fl"
Example 2
123Input: strs = ["dog","racecar","car"]Output: ""Explanation: There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 200
0 <= ...
leetcode-58-length-of-last-word
題目Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
題目連結
Example 1
123Input: s = "Hello World"Output: 5Explanation: The last word is "World" with length 5.
Example 2
123Input: s = " fly me to the moon "Output: 4Explanation: The last word is "moon" with length 4.
Example 3
123Input: s = "luffy is still joyboy"Output: 6 ...
leetcode-12-integer-to-roman
題目Seven different symbols represent Roman numerals with the following values:
Symbol
Value
I
1
V
5
X
10
L
50
D
500
M
1000
Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:
If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and ...
leetcode-13-roman-to-integer
題目Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol
Value
I
1
V
5
X
10
L
50
D
500
M
1000
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because t ...
leetcode-42-trapping-rain-water
題目Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
題目連結
Example 1
123Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]Output: 6Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Example 2
12Input: height = [4,2,0,3,2,5]Output: 9
Constraints:
n == height.length
1 <= ...
leetcode-135-candy
題目There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.Children with a higher rating get more candies than their neighbors.Return the minimum number of candies you need to have to distribute the candies to the children.
題目連結
Example 1
123Input: ratings = [1,0,2]Output: 5Explanation: You can allocate to the first, ...
leetcode-134-gas-station
題目There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas and cost, return the starting gas station’s index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a soluti ...
leetcode-238-product-of-array-except-self
題目Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
題目連結
Example 1
12Input: nums = [1,2,3,4]Output: [24,12,8,6]
Example 2
12Input: nums = [-1,1,0,-3,3]Output: [0,0,9,0,0]
Constraints:
2 <= val <= 105
-30 < ...
leetcode-380-insert-delete-getrandom-o1
題目Implement the RandomizedSet class:
RandomizedSet() Initializes the RandomizedSet object.
bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
int getRandom() Returns a random element from the current set of elements (it’s guaranteed that at least one element exists when this method is called). Each ...