voiddfs(TreeNode *p, string &s){ // 前序遍历拼字符串 if (!p) { s += " #"; return; } s += " " + to_string(p->val); dfs(p->left, s); dfs(p->right, s); }
string ss, st;
intfind(conststring &haystack, conststring &needle){ int h = haystack.length(), n = needle.length(); long M = INT_MAX, B = 256; // INT_MAX是质数! if (h < n) return-1; long highest_power = 1, hh = 0, nh = 0; for (int i = 0; i < n; ++i) { highest_power = (highest_power * B) % M; nh = (nh * B + needle[i]) % M; hh = (hh * B + haystack[i]) % M; } for (int i = 0; i < h - n + 1; ++i) { if (i >= 1) { hh = (hh * B + M - haystack[i - 1] * highest_power % M + haystack[i + n - 1]) % M; // 这里highest_power是B的n次方,因为先整体左移再减高位,如果先减高位再整体左移就是n-1次方了 } if (hh == nh && haystack.substr(i, n) == needle) { return i; } } return-1; } };
classSolution { public: intfindCircleNum(vector<vector<int>>& M){ n = M.size(); visited.resize(n); int res = 0; for (int i = 0; i < n; ++i) { if (!visited[i]) { visit(M, i); ++res; } } return res; }
voidvisit(constvector<vector<int>> &M, int u){ visited[u] = true; for (int v = 0; v < n; ++v) { // 这里必须从0开始,因为访问u的时候,小于u的并不一定已经被访问过,为了dfs把所有相关的都访问到,必须从0开始 if (!visited[v] && M[u][v]) { visit(M, v); } } }
Posted onEdited onInLeetCodeDisqus: Symbols count in article: 491Reading time ≈1 mins.
左下到右上二分 O(m+n) time O(1) space 这道题跟74. Search a 2D Matrix的区别是从左上到右下并非严格递增,所以不能直接用全矩阵二分(复杂度更低)不过这个方法是可以用到那道题上的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: boolsearchMatrix(vector<vector<int>>& matrix, int target){ if (matrix.empty() || matrix[0].empty()) returnfalse; int m = matrix.size(), n = matrix[0].size(); int r = m - 1, c = 0; while (r >= 0 && c < n) { if (matrix[r][c] == target) returntrue; if (matrix[r][c] > target) { --r; } else { ++c; } } returnfalse; } };
/** Resets the array to its original configuration and return it. */ vector<int> reset(){ return nums; }
/** Returns a random shuffling of the array. */ vector<int> shuffle(){ auto res = nums; int n = res.size(); for (int i = n - 1; i > 0; --i) { swap(res[rand() % (i + 1)], res[i]); } return res; }
vector<int> nums; };
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * vector<int> param_1 = obj.reset(); * vector<int> param_2 = obj.shuffle(); */
classSolution { public: boolisPerfectSquare(int num){ for (int i = 1; num > 0; i += 2) { num -= i; } return num == 0; } };
用这个 O(logn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: boolisPerfectSquare(int num){ int l = 1, r = num; while (l <= r) { // 小于等于可行,因为只需要找 long m = l + (r - l) / 2; long p = m * m; if (p == num) { returntrue; } elseif (p < num) { l = m + 1; } else { r = m - 1; } } returnfalse; } };
Newton method
1 2 3 4 5 6 7 8 9 10
classSolution { public: boolisPerfectSquare(int num){ long x = num; while (x * x > num) { x = (x + num / x) >> 1; } return x * x == num; } };
Posted onEdited onInLeetCodeDisqus: Symbols count in article: 1kReading time ≈1 mins.
binary search O(logx)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: intmySqrt(int x){ long lo = 0, hi = x; // 一定要从0开始 while (lo < hi) { // 这道题用小于比较好(<=也行)因为不一定取到精确值 long m = (lo + hi + 1) / 2, s = m * m; // 注意必须是long防止溢出,并且要+1来取m if (s <= x) { lo = m; } else { hi = m - 1; } } return lo; } };
classSolution { public: intmySqrt(int x){ long min = 0, max = x; while (min <= max) { // 一定是min <= max而不是min < max是因为最后一定要是min > max来跳出循环,min == max是合法的 long mid = (min + max) / 2; long p = mid * mid; if (p == x) { return mid; } elseif (p < x) { min = mid + 1; } else { max = mid - 1; } } return max; // 是max而不是min是因为最后二分查找一定结束在一个较小的数上,比如5是2而不是3,10是3而不是4,而min一定要比max大才能跳出while循环 } };
newton’s method O(logx)
1 2 3 4 5 6 7 8 9 10
classSolution { public: intmySqrt(int x){ long r = x; while (r * r > x) { r = (r + x / r) / 2; } return r; } };
double版
1 2 3 4 5 6 7 8 9 10 11 12
doubles(double x){ double lo = 0, hi = max(x, 1.0); while ((hi - lo) > 1e-8) { auto m = (lo + hi) / 2, p = m * m; if (p <= x) { lo = m; } else { hi = m; } } return lo; }
Posted onEdited onInLeetCodeDisqus: Symbols count in article: 420Reading time ≈1 mins.
O(log(mn)) time O(1) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: boolsearchMatrix(vector<vector<int>>& matrix, int target){ if (matrix.empty() || matrix[0].empty()) returnfalse; int m = matrix.size(), n = matrix[0].size(), l = 0, r = m * n - 1; while (l <= r) { int m = l + (r - l) / 2; int x = matrix[m / n][m % n]; if (x == target) returntrue; if (x < target) { l = m + 1; } else { r = m - 1; } } returnfalse; } };