classMyQueue { public: /** Initialize your data structure here. */ MyQueue() {
}
/** Push element x to the back of queue. */ voidpush(int x){ s.push(x); }
/** Removes the element from in front of queue and returns that element. */ intpop(){ adjust(); int res = q.top(); q.pop(); return res; }
/** Get the front element. */ intpeek(){ adjust(); return q.top(); }
/** Returns whether the queue is empty. */ boolempty(){ return s.empty() && q.empty(); }
voidadjust(){ if (q.empty()) { // 这行很重要,不需要每次都调整 while (!s.empty()) { q.push(s.top()); s.pop(); } } }
stack<int> s; stack<int> q; };
/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * bool param_4 = obj.empty(); */
Posted onInLeetCodeDisqus: Symbols count in article: 300Reading time ≈1 mins.
O(logn) time O(1) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intsearch(vector<int>& nums, int target){ int n = nums.size(), l = 0, r = n - 1; while (l <= r) { int m = l + (r - l) / 2; if (nums[m] == target) return m; if (nums[m] < target) { l = m + 1; } else { r = m - 1; } } return-1; } };
Posted onInLeetCodeDisqus: Symbols count in article: 661Reading time ≈1 mins.
O(32) time 把两个数相加的结果拆成两部分 一部分是每个bit的无进位和 一部分是进位 不考虑进位的话每一bit的和就是异或的结果 每一个进位就是两个数相与再左移一位即可 必须用unsigned因为runtime error: left shift of negative value -2147483648,对INT_MIN左移位。不能对负数进行左移,就是说最高位符号位必须要为0,才能左移 因为左移以后要补0 所以最多32个iteration
1 2 3 4 5 6
classSolution { public: intgetSum(int a, int b){ return b == 0 ? a : getSum(a ^ b, (unsigned)(a & b) << 1); } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: intgetSum(int a, int b){ int res = 0; int c = 0; for (int i = 0; i < 32 && (a || b || c); ++i, a >>= 1, b >>= 1) { int a_bit = (a & 1); int b_bit = (b & 1); int sum_bit = a_bit ^ b_bit ^ c; c = (a_bit & b_bit) | (b_bit & c) | (a_bit & c); res |= (sum_bit << i); } return res; } };
classSolution { public: boolsearch(vector<int>& nums, int target){ int n = nums.size(); int l = 0, r = n - 1; while (l <= r) { int m = l + (r - l) / 2; if (nums[m] == target) returntrue; if (nums[l] < nums[m]) { if (nums[l] <= target && target < nums[m]) { r = m - 1; } else { l = m + 1; } } elseif (nums[l] > nums[m]) { if (nums[m] < target && target <= nums[r]) { l = m + 1; } else { r = m - 1; } } else { ++l; // 因为nums[m]不是target而nums[m] == nums[l],所以++l来缩小搜索范围 } } returnfalse; } };
classSolution { public: boolsearch(vector<int>& nums, int target){ int n = nums.size(); int l = 0, r = n - 1; while (l <= r) { int m = l + (r - l) / 2; if (nums[m] == target) returntrue; if (nums[m] > nums[r]) { if (nums[l] <= target && target < nums[m]) { r = m - 1; } else { l = m + 1; } } elseif (nums[m] < nums[r]) { if (nums[m] < target && target <= nums[r]) { l = m + 1; } else { r = m - 1; } } else { --r; } } returnfalse; } };
Posted onInLeetCodeDisqus: Symbols count in article: 273Reading time ≈1 mins.
O(n) time O(n) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intsumOfUnique(vector<int>& nums){ unordered_map<int, int> m; for (int x : nums) { ++m[x]; } int res = 0; for (auto &[x, cnt] : m) { if (cnt == 1) { res += x; } } return res; } };