LeetCode-169. Majority Element
问题描述
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
Example :
1 | Input: nums = [3,2,3] |
Constraints:
n == nums.length1 <= n <= 5 * 104-109 <= nums[i] <= 109
LeetCode-7. Reverse Integer
问题描述
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Constraints:
-231 <= x <= 231 - 1
LeetCode-136. Single Number
问题描述
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example :
1 | Input: nums = [2,2,1] |
Constraints:
1 <= nums.length <= 3 * 104-3 * 104 <= nums[i] <= 3 * 104- Each element in the array appears twice except for one element which appears only once.
LeetCode-14. Longest Common Prefix
问题描述
rite 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 | Input: strs = ["flower","flow","flight"] |
Constraints:
1 <= strs.length <= 2000 <= strs[i].length <= 200strs[i]consists of only lower-case English letters.
LeetCode-706. Design HashMap
问题描述
Design a HashMap without using any built-in hash table libraries.
Implement the MyHashMap class:
MyHashMap()initializes the object with an empty map.void put(int key, int value)inserts a(key, value)pair into the HashMap. If thekeyalready exists in the map, update the correspondingvalue.int get(int key)returns thevalueto which the specifiedkeyis mapped, or-1if this map contains no mapping for thekey.void remove(key)removes thekeyand its correspondingvalueif the map contains the mapping for thekey.
Example :
1 | Input |
Constraints:
0 <= key, value <= 106- At most
104calls will be made toput,get, andremove.
LeetCode-705. Design HashSet
问题描述
Design a HashSet without using any built-in hash table libraries.
Implement MyHashSet class:
void add(key)Inserts the valuekeyinto the HashSet.bool contains(key)Returns whether the valuekeyexists in the HashSet or not.void remove(key)Removes the valuekeyin the HashSet. Ifkeydoes not exist in the HashSet, do nothing.
Example :
1 | Input |
Constraints:
0 <= key <= 106- At most
104calls will be made toadd,remove, andcontains.
LeetCode-155. Min Stack
问题描述
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Implement the MinStack class:
MinStack()initializes the stack object.void push(int val)pushes the elementvalonto the stack.void pop()removes the element on the top of the stack.int top()gets the top element of the stack.int getMin()retrieves the minimum element in the stack.
Example :
1 | Input |
LeetCode-746. Min Cost Climbing Stairs
问题描述
You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.
You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.
Example :
1 | Input: cost = [10,15,20] |
Constraints:
2 <= cost.length <= 10000 <= cost[i] <= 999