classSolution { public: intkthSmallest(vector<vector<int>>& matrix, int k){ int n = matrix.size(); int lo = matrix[0][0], hi = matrix[n - 1][n - 1]; while (lo < hi) { int m = lo + (hi - lo) / 2; int cnt = 0; for (constauto &v : matrix) { // 统计所有不大于m的数的个数 cnt += (upper_bound(v.begin(), v.end(), m) - v.begin()); } if (cnt < k) { // 如果不大于m(包括m)的数不到k个,说明m不是最终结果 lo = m + 1; } else { hi = m; } } return lo; } };
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Constructor initializes an empty nested list. * NestedInteger(); * * // Constructor initializes a single integer. * NestedInteger(int value); * * // Return true if this NestedInteger holds a single integer, rather than a nested list. * bool isInteger() const; * * // Return the single integer that this NestedInteger holds, if it holds a single integer * // The result is undefined if this NestedInteger holds a nested list * int getInteger() const; * * // Set this NestedInteger to hold a single integer. * void setInteger(int value); * * // Set this NestedInteger to hold a nested list and adds a nested integer to it. * void add(const NestedInteger &ni); * * // Return the nested list that this NestedInteger holds, if it holds a nested list * // The result is undefined if this NestedInteger holds a single integer * const vector<NestedInteger> &getList() const; * }; */ classSolution { public: intdepthSum(vector<NestedInteger>& nestedList){ int depth = 1, res = 0; while (!nestedList.empty()) { vector<NestedInteger> nextLevel; for (constauto &ni : nestedList) { if (ni.isInteger()) { res += ni.getInteger() * depth; } else { nextLevel.insert(end(nextLevel), begin(ni.getList()), end(ni.getList())); } } nestedList.swap(nextLevel); ++depth; } return res; } };
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Constructor initializes an empty nested list. * NestedInteger(); * * // Constructor initializes a single integer. * NestedInteger(int value); * * // Return true if this NestedInteger holds a single integer, rather than a nested list. * bool isInteger() const; * * // Return the single integer that this NestedInteger holds, if it holds a single integer * // The result is undefined if this NestedInteger holds a nested list * int getInteger() const; * * // Set this NestedInteger to hold a single integer. * void setInteger(int value); * * // Set this NestedInteger to hold a nested list and adds a nested integer to it. * void add(const NestedInteger &ni); * * // Return the nested list that this NestedInteger holds, if it holds a nested list * // The result is undefined if this NestedInteger holds a single integer * const vector<NestedInteger> &getList() const; * }; */ classSolution { public: intdepthSum(vector<NestedInteger>& nestedList){ list<NestedInteger> q(begin(nestedList), end(nestedList)); int depth = 1, res = 0; while (!q.empty()) { for (int i = q.size(); i > 0; --i) { auto x = q.front(); q.pop_front(); if (x.isInteger()) { res += x.getInteger() * depth; } else { q.insert(end(q), begin(x.getList()), end(x.getList())); } } ++depth; } return res; } };
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Constructor initializes an empty nested list. * NestedInteger(); * * // Constructor initializes a single integer. * NestedInteger(int value); * * // Return true if this NestedInteger holds a single integer, rather than a nested list. * bool isInteger() const; * * // Return the single integer that this NestedInteger holds, if it holds a single integer * // The result is undefined if this NestedInteger holds a nested list * int getInteger() const; * * // Set this NestedInteger to hold a single integer. * void setInteger(int value); * * // Set this NestedInteger to hold a nested list and adds a nested integer to it. * void add(const NestedInteger &ni); * * // Return the nested list that this NestedInteger holds, if it holds a nested list * // The result is undefined if this NestedInteger holds a single integer * const vector<NestedInteger> &getList() const; * }; */ classSolution { public: intdepthSum(vector<NestedInteger>& nestedList){ queue<NestedInteger> q; for (constauto &ni : nestedList) { q.push(ni); } int res = 0, depth = 1; while (!q.empty()) { for (int i = q.size(); i > 0; --i) { auto x = q.front(); q.pop(); if (x.isInteger()) { res += x.getInteger() * depth; } else { for (constauto &ni : x.getList()) { q.push(ni); } } } ++depth; } return res; } };
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Constructor initializes an empty nested list. * NestedInteger(); * * // Constructor initializes a single integer. * NestedInteger(int value); * * // Return true if this NestedInteger holds a single integer, rather than a nested list. * bool isInteger() const; * * // Return the single integer that this NestedInteger holds, if it holds a single integer * // The result is undefined if this NestedInteger holds a nested list * int getInteger() const; * * // Set this NestedInteger to hold a single integer. * void setInteger(int value); * * // Set this NestedInteger to hold a nested list and adds a nested integer to it. * void add(const NestedInteger &ni); * * // Return the nested list that this NestedInteger holds, if it holds a nested list * // The result is undefined if this NestedInteger holds a single integer * const vector<NestedInteger> &getList() const; * }; */ classSolution { public: intdepthSum(vector<NestedInteger>& nestedList){ return dfs(nestedList, 1); }
intdfs(constvector<NestedInteger> &nestedList, int depth){ int res = 0; for (constauto &ni : nestedList) { if (ni.isInteger()) { res += ni.getInteger() * depth; } else { res += dfs(ni.getList(), depth + 1); } } return res; } };
intpick(int target){ int res = -1, count = 0; for (auto it = b; it != e; ++it) { if (*it != target) continue; if (rand() % ++count == 0) { res = distance(b, it); } } return res; }
vector<int>::iterator b, e; };
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int param_1 = obj.pick(target); */
intdfs(TreeNode *root){ // 返回以root为根的最长链有几个结点 if (!root) return0; int l = dfs(root->left), r = dfs(root->right); res = max(res, l + r); // 人家问的是边不是点,不要加1 return max(l, r) + 1; }
/* // Definition for a Node. class Node { public: int val; Node* next; Node() {} Node(int _val) { val = _val; next = NULL; } Node(int _val, Node* _next) { val = _val; next = _next; } }; */
classSolution { public: Node* insert(Node* head, int insertVal){ if (!head) { auto res = new Node(insertVal); res->next = res; return res; } auto prev = head, curr = head->next; do { // 因为上来如果判断prev != head没法写循环,改成dowhile就可以了 if (prev->val <= insertVal && insertVal <= curr->val) break; if (prev->val > curr->val && (prev->val <= insertVal || insertVal <= curr->val)) break; prev = curr; curr = prev->next; } while (prev != head); prev->next = new Node(insertVal, curr); return head; } };
classSolution { public: vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) { int m = A.size(), n = B.size(); int i = 0, j = 0; vector<vector<int>> res; while (i < m && j < n) { int s = max(A[i][0], B[j][0]); int e = min(A[i][1], B[j][1]); // 找intersection if (s <= e) { // 如果有intersection res.push_back({s, e}); } if (e == A[i][1]) { // 如果A[i]区间比较靠前则看下一个 ++i; } if (e == B[j][1]) { ++j; } } return res; } };
Posted onEdited onInLeetCodeDisqus: Symbols count in article: 1.1kReading time ≈1 mins.
O(m+n) time O(1) space 关键是从后往前扫描!!
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: voidmerge(vector<int>& nums1, int m, vector<int>& nums2, int n){ for (int i = m + n - 1, i1 = m - 1, i2 = n - 1; i2 >= 0; --i) { if (i1 >= 0) { nums1[i] = nums1[i1] > nums2[i2] ? nums1[i1--] : nums2[i2--]; } else { nums1[i] = nums2[i2--]; } } } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: voidmerge(vector<int>& nums1, int m, vector<int>& nums2, int n){ for (int i = m + n - 1, i1 = m - 1, i2 = n - 1; i >= 0; --i) { if (i1 >= 0 && i2 >= 0) { nums1[i] = nums1[i1] > nums2[i2] ? nums1[i1--] : nums2[i2--]; } elseif (i2 >= 0) { // i1到头了,只扫nums2即可 nums1[i] = nums2[i2--]; } else { // i2到头了,nums1保持不变,不用再扫了 break; } } } };
O(m+n) time O(m+n) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: voidmerge(vector<int>& nums1, int m, vector<int>& nums2, int n){ vector<int> res(m + n); for (int i = 0, j = 0; i < m || j < n;) { int a = i < m ? nums1[i] : INT_MAX; int b = j < n ? nums2[j] : INT_MAX; res[i + j] = min(a, b); if (res[i + j] == a) ++i; else ++j; } nums1 = res; } };