Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).
Implement the MyQueue class:
void push(int x) Pushes element x to the back of the queue.
int pop() Removes the element from the front of the queue and returns it.
int peek() Returns the element at the front of the queue.
boolean empty() Returns true if the queue is empty, false otherwise.
Notes:
You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack’s standard operations.
Explanation MyQueue myQueue = new MyQueue(); myQueue.push(1); // queue is: [1] myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) myQueue.peek(); // return 1 myQueue.pop(); // return 1, queue is [2] myQueue.empty(); // return false
A range[a,b] is the set of all integers from a to b (inclusive).
Return the smallest sorted list of ranges that cover all the numbers in the array exactly. That is, each element of nums is covered by exactly one of the ranges, and there is no integer x such that x is in one of the ranges but not in nums.
classSolution { public: vector<string> summaryRanges(vector<int>& nums){ vector<string> ret; int i = 0; int n = nums.size(); while (i < n) { int low = i; i++; while (i < n && nums[i] == nums[i - 1] + 1) { i++; } int high = i - 1; string temp = to_string(nums[low]); if (low < high) { temp.append("->"); temp.append(to_string(nums[high])); } ret.push_back(move(temp)); } return ret; } };
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
Implement the MyStack class:
void push(int x) Pushes element x to the top of the stack.
int pop() Removes the element on the top of the stack and returns it.
int top() Returns the element on the top of the stack.
boolean empty() Returns true if the stack is empty, false otherwise.
Notes:
You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty operations are valid.
Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue’s standard operations.
intpop(){ int r = que1.front(); que1.pop(); return r; }
inttop(){ return que1.front(); }
boolempty(){ return que1.empty(); } };
/** * Your MyStack object will be instantiated and called as such: * MyStack* obj = new MyStack(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->top(); * bool param_4 = obj->empty(); */
intpop(){ int r = que.front(); que.pop(); return r; }
inttop(){ return que.front(); }
boolempty(){ return que.empty(); } };
/** * Your MyStack object will be instantiated and called as such: * MyStack* obj = new MyStack(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->top(); * bool param_4 = obj->empty(); */
Given an integer array nums and an integer k, return trueif there are two distinct indicesiandjin the array such thatnums[i] == nums[j]andabs(i - j) <= k.
Example 1:
1 2
Input: nums = [1,2,3,1], k = 3 Output: true
Example 2:
1 2
Input: nums = [1,0,1,1], k = 1 Output: true
Example 3:
1 2
Input: nums = [1,2,3,1,2,3], k = 2 Output: false
哈希表:
时间复杂度:O(n)
空间复杂度:O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: boolcontainsNearbyDuplicate(vector<int>& nums, int k){ unordered_map<int, int> hash; int nums_size = nums.size(); for (int i = 0; i < nums_size; i++) { int num = nums[i]; if (hash.count(num) && i - hash[num] <= k)returntrue; hash[num] = i; } returnfalse; } };
滑动窗口:
时间复杂度:O(n)
空间复杂度:O(k+1)
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: boolcontainsNearbyDuplicate(vector<int>& nums, int k){ unordered_set<int> hash; int nums_size = nums.size(); for (int i = 0; i < nums_size; i++) { if (i > k)hash.erase(nums[i - k - 1]); if (hash.count(nums[i]))returntrue; hash.emplace(nums[i]); } returnfalse; } };