├── .gitignore ├── LICENSE ├── README.md ├── coding interview ├── 48.cpp └── 52-II.cpp ├── kick start ├── 2018 │ ├── A │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ ├── B │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ ├── C │ │ └── A.cpp │ ├── D │ │ └── A.cpp │ ├── E │ │ └── A.cpp │ ├── F │ │ └── A.cpp │ └── G │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp ├── 2019 │ ├── A │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ ├── B │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ ├── C │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ ├── D │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ ├── E │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ ├── F │ │ ├── A.cpp │ │ ├── B.cpp │ │ ├── C-1.cpp │ │ └── C-2.cpp │ ├── G │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp │ └── H │ │ ├── A.cpp │ │ ├── B.cpp │ │ └── C.cpp └── 2020 │ ├── A │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D.cpp │ ├── B │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ └── D.cpp │ └── C │ ├── A.cpp │ ├── B.cpp │ ├── C.cpp │ ├── D.cpp │ └── h.txt ├── leetcode ├── 1.cpp ├── 10.cpp ├── 101.cpp ├── 1013.cpp ├── 102.cpp ├── 103.cpp ├── 104.cpp ├── 105.cpp ├── 108.cpp ├── 11.cpp ├── 112.cpp ├── 115.cpp ├── 116.cpp ├── 118.cpp ├── 1192.cpp ├── 120.cpp ├── 1200.cpp ├── 1201.cpp ├── 1202.cpp ├── 1203.cpp ├── 1204.cpp ├── 1217.cpp ├── 1218.cpp ├── 1219.cpp ├── 122.cpp ├── 123.cpp ├── 124.cpp ├── 125.cpp ├── 126.cpp ├── 1266.cpp ├── 1267.cpp ├── 127.cpp ├── 1275.cpp ├── 128.cpp ├── 129.cpp ├── 1297.cpp ├── 13.cpp ├── 130.cpp ├── 131.cpp ├── 1311.cpp ├── 1319.cpp ├── 132.cpp ├── 1332.cpp ├── 1333.cpp ├── 1334.cpp ├── 1335.cpp ├── 1339.cpp ├── 134.cpp ├── 1351.cpp ├── 1352.cpp ├── 1353.cpp ├── 1354.cpp ├── 136.cpp ├── 138-1.cpp ├── 138-2.cpp ├── 139.cpp ├── 14.cpp ├── 140.cpp ├── 141.cpp ├── 142.cpp ├── 146.cpp ├── 148-1.cpp ├── 148-2.cpp ├── 148-3.cpp ├── 149-1.cpp ├── 149-2.cpp ├── 15.cpp ├── 152.cpp ├── 155.cpp ├── 160-1.cpp ├── 160-2.cpp ├── 169.cpp ├── 17.cpp ├── 171.cpp ├── 172.cpp ├── 174.cpp ├── 179.cpp ├── 18.cpp ├── 189-1.cpp ├── 189-2.cpp ├── 189-3.cpp ├── 19.cpp ├── 190.cpp ├── 191.cpp ├── 198.cpp ├── 2.cpp ├── 20.cpp ├── 204.cpp ├── 206.cpp ├── 207.cpp ├── 208.cpp ├── 21.cpp ├── 210.cpp ├── 212.cpp ├── 213.cpp ├── 215.cpp ├── 217.cpp ├── 218.cpp ├── 22.cpp ├── 221.cpp ├── 227.cpp ├── 23.cpp ├── 230.cpp ├── 234.cpp ├── 236.cpp ├── 240.cpp ├── 26.cpp ├── 264.cpp ├── 268.cpp ├── 27.cpp ├── 271.cpp ├── 279.cpp ├── 28-1.cpp ├── 28-2.cpp ├── 283.cpp ├── 287-1.cpp ├── 287-2.cpp ├── 287-3.cpp ├── 289.cpp ├── 295.cpp ├── 3.cpp ├── 300.cpp ├── 303.cpp ├── 304.cpp ├── 309.cpp ├── 312.cpp ├── 315.cpp ├── 32.cpp ├── 322.cpp ├── 326-1.cpp ├── 326-2.cpp ├── 328.cpp ├── 329.cpp ├── 334.cpp ├── 337.cpp ├── 338.cpp ├── 34.cpp ├── 344.cpp ├── 349.cpp ├── 350.cpp ├── 357.cpp ├── 36.cpp ├── 368.cpp ├── 371.cpp ├── 38.cpp ├── 380.cpp ├── 384.cpp ├── 387.cpp ├── 39.cpp ├── 395.cpp ├── 4.cpp ├── 41.cpp ├── 42-1.cpp ├── 42-2.cpp ├── 44.cpp ├── 45.cpp ├── 454.cpp ├── 46.cpp ├── 48.cpp ├── 49.cpp ├── 5-1.cpp ├── 5-2.cpp ├── 5291.cpp ├── 5292.cpp ├── 5293.cpp ├── 5294.cpp ├── 53.cpp ├── 54.cpp ├── 5431.cpp ├── 55.cpp ├── 56.cpp ├── 567.cpp ├── 568.cpp ├── 62.cpp ├── 63.cpp ├── 64.cpp ├── 647.cpp ├── 649.cpp ├── 66.cpp ├── 687.cpp ├── 69.cpp ├── 695.cpp ├── 7.cpp ├── 70.cpp ├── 73.cpp ├── 744.cpp ├── 75.cpp ├── 76.cpp ├── 78.cpp ├── 79.cpp ├── 8.cpp ├── 84.cpp ├── 85.cpp ├── 864.cpp ├── 87.cpp ├── 88.cpp ├── 91.cpp ├── 94.cpp ├── 96.cpp ├── 97.cpp ├── 98.cpp └── 980.cpp └── sample └── union_find.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Local build setting environment 2 | .vscode/ 3 | executable/ 4 | sample.cpp 5 | 6 | # Prerequisites 7 | *.d 8 | 9 | # Compiled Object files 10 | *.slo 11 | *.lo 12 | *.o 13 | *.obj 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Compiled Dynamic libraries 20 | *.so 21 | *.dylib 22 | *.dll 23 | 24 | # Fortran module files 25 | *.mod 26 | *.smod 27 | 28 | # Compiled Static libraries 29 | *.lai 30 | *.la 31 | *.a 32 | *.lib 33 | 34 | # Executables 35 | *.exe 36 | *.out 37 | *.app 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 杨攀 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithm 2 | 这里记录博主算法之路,也尝试写下自己学习算法时的收获。与常见的算法题解不同,博主**尝试**总结整理一套求解问题的思考方式,希望做到知其然,知其所以然~ 3 | 4 | 总结文章在这里: 5 | * [算法笔记(一)-概述,面对问题的思考方式](https://akeeper.space/blog/351.html) 6 | 7 | 学习算法,刷题是重中之重,该repo现包括LeetCode和google kick start 8 | ## leetcode 9 | 目前ac 100+,答案尽可能地对运行时间进行了优化,即faster than 97.00% of C++ online submissions at least. 10 | 11 | 具体题解在这里: 12 | * [leetcode题解](https://akeeper.space/blog/leetcode-answers) 13 | 14 | ## google kick start 15 | Kick Start算是Google的APAC Test,同时也是Google亚太地区招聘的笔试平台,Google亚太地区技术岗求职(正式/实习)均需要在该平台测试。(梦想还是要有的,万一进google了呢~) 16 | 17 | 具体题解在这里: 18 | * [google kick start题解](https://akeeper.space/blog/kick-start) 19 | 20 | > 觉得我的文章对您有帮助的话,就给个[star](https://github.com/Akeepers/algorithm)吧~ 21 | -------------------------------------------------------------------------------- /coding interview/48.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Solution { 11 | public: 12 | int lengthOfLongestSubstring(string s) { 13 | if (s.length() < 2) return s.length(); 14 | unordered_map counts; 15 | int res = 0, l = 0, n = s.length(); 16 | for (int i = 0; i < n; ++i) { 17 | if (++counts[s[i]] > 1) { 18 | res = max(res, i - l); 19 | while (--counts[s[l++]] != 1) { 20 | } 21 | } 22 | } 23 | res = max(res, n - l); 24 | return res; 25 | } 26 | }; 27 | 28 | int main() { 29 | system("pause"); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /coding interview/52-II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define INF 0x3f3f3f3f 11 | #define INF64 0x3f3f3f3f3f3f3f3f 12 | 13 | class Solution { 14 | public: 15 | int missingNumber(vector& nums) { 16 | int l = 0, r = nums.size() - 1; 17 | while (l <= r) { 18 | auto mid = (l + r) >> 1; 19 | if (nums[mid] != mid) { 20 | if (mid == 0 || nums[mid - 1] == mid - 1) { 21 | return mid; 22 | } 23 | r = mid - 1; 24 | } else { 25 | l = mid + 1; 26 | } 27 | } 28 | if (l == nums.size()) return l; 29 | return -1; 30 | } 31 | }; 32 | 33 | int main() { 34 | vector inputs = {0, 1, 3}; 35 | Solution s; 36 | s.missingNumber(inputs); 37 | system("pause"); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /kick start/2018/A/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | double cal(int n, int k, vector &values) 9 | { 10 | sort(values.begin(), values.end()); 11 | long long sum = 0; 12 | for (auto value : values) 13 | sum += value; 14 | if (k == 0) 15 | return sum / (double)n; 16 | double res = 0.0; 17 | res += values.back() / (double)n; 18 | res += (n - 1) / (double)n / (double)n * sum; 19 | return res; 20 | } 21 | 22 | int main() 23 | { 24 | int t = 0; 25 | cin >> t; 26 | for (int i = 1; i <= t; ++i) 27 | { 28 | long long n, k; 29 | cin >> n >> k; 30 | vector values(n, 0); 31 | for (int j = 0; j < n;) 32 | cin >> values[j++]; 33 | 34 | auto ret = cal(n,k,values); 35 | cout<< "Case #" << i << ": " << ret << endl; 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /kick start/2018/B/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | typedef long long ll; 14 | 15 | ll count(ll d) 16 | { 17 | ll cur = d; 18 | vector bits; 19 | while (cur != 0) 20 | { 21 | auto bit = cur % 10; 22 | bits.emplace_back(bit); 23 | cur = cur / 10; 24 | } 25 | ll res = 0; 26 | auto n = bits.size(); 27 | for (int i = n - 1; i >=0; --i) 28 | { 29 | if (i > 0) 30 | res += bits[i] * powl(9, i - 1) * 8; 31 | else 32 | { 33 | for (ll j = d - bits[i]; j <= d; ++j) 34 | { 35 | if (j % 9 > 0) 36 | res++; 37 | } 38 | } 39 | } 40 | return res; 41 | } 42 | 43 | void slove() 44 | { 45 | ll f, l; 46 | cin >> f >> l; 47 | auto res = count(l) - count(f) +1; 48 | cout << res << endl; 49 | } 50 | 51 | int main() 52 | { 53 | int t = 0; 54 | cin >> t; 55 | for (int i = 1; i <= t; ++i) 56 | { 57 | cout << "Case #" << i << ": "; 58 | slove(); 59 | } 60 | return 0; 61 | } -------------------------------------------------------------------------------- /kick start/2018/B/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int maxEnergy(vector> &inputs) 9 | { 10 | sort(inputs.begin(), inputs.end(), [](const array &a, const array &b) -> bool { return b[0] * a[2] > a[0] * b[2]; }); 11 | int n = inputs.size(); 12 | int totalTime = 0; 13 | for (auto stone : inputs) 14 | { 15 | totalTime += stone[0]; 16 | } 17 | vector> dp(n+1, vector(totalTime+1, 0)); 18 | for (int i = 1; i <= n; ++i) 19 | { 20 | auto stone = inputs[i - 1]; 21 | for (int j = 1; j <= totalTime; ++j) 22 | { 23 | if (stone[0] > j) 24 | continue; 25 | dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - stone[0]] + stone[1] - stone[2] * j); 26 | } 27 | } 28 | return dp[n][totalTime]; 29 | 30 | } 31 | int main() 32 | { 33 | int t = 0; 34 | cin >> t; 35 | for (int i = 1; i <= t; ++i) 36 | { 37 | int n; 38 | vector> inputs; 39 | cin >> n; 40 | while (n--) 41 | { 42 | array temp; 43 | cin >> temp[0] >> temp[1] >> temp[2]; 44 | inputs.push_back(temp); 45 | } 46 | int ret = maxEnergy(inputs); 47 | cout << "Case #" << i << ": " << ret << endl; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /kick start/2018/D/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | typedef long long ll; 16 | const ll INF = 1e17; 17 | 18 | void slove() 19 | { 20 | ll n, o, d, a, b, c, m, l; 21 | cin >> n >> o >> d; 22 | vector candies(n + 1, 0); 23 | cin >> candies[1] >> candies[2] >> a >> b >> c >> m >> l; 24 | 25 | vector sum(n + 1, 0); 26 | for (ll i = 3; i <= n; ++i) 27 | candies[i] = (a * candies[i - 1] + b * candies[i - 2] + c) % m; 28 | for (ll i = 1; i <= n; ++i) 29 | { 30 | candies[i] += l; 31 | sum[i] = sum[i - 1] + candies[i]; 32 | } 33 | multiset mt; 34 | 35 | ll r = 1,odd = 0,res = -INF; 36 | for (ll i = 1; i <= n; ++i) 37 | { 38 | while (r <= n && odd + (candies[r] & 1) <= o) 39 | { 40 | mt.insert(sum[r]); 41 | odd += (candies[r++] & 1); 42 | } 43 | if (r <= i) 44 | { 45 | r = i + 1; 46 | } 47 | else 48 | { 49 | auto it = mt.lower_bound(sum[i - 1] + d + 1); 50 | if (it != mt.begin()) 51 | { 52 | --it; 53 | res = max(res, *it - sum[i - 1]); 54 | } 55 | mt.erase(mt.find(sum[i])); 56 | odd -= (candies[i] & 1); 57 | } 58 | } 59 | if (res == -INF) 60 | cout << "IMPOSSIBLE" << endl; 61 | else 62 | cout << res << endl; 63 | } 64 | 65 | int main() 66 | { 67 | int t = 0; 68 | cin >> t; 69 | for (int i = 1; i <= t; ++i) 70 | { 71 | cout << "Case #" << i << ": "; 72 | slove(); 73 | } 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /kick start/2018/E/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | typedef long long ll; 16 | const ll INF = 1e17; 17 | 18 | void slove() 19 | { 20 | int n, k; 21 | cin >> n >> k; 22 | vector A(n + 1, 0); 23 | for (int i = 1; i <= n; ++i) 24 | cin >> A[i]; 25 | sort(A.begin(), A.end()); 26 | int day = 1, eated = 1, res = 0; 27 | for (int i = 1; i <= n; ++i) 28 | { 29 | if (A[i] >= day) 30 | { 31 | res++; 32 | day = eated / k + 1; 33 | eated++; 34 | } 35 | else 36 | { 37 | continue; 38 | } 39 | } 40 | cout << res << endl; 41 | } 42 | 43 | int main() 44 | { 45 | int t = 0; 46 | cin >> t; 47 | for (int i = 1; i <= t; ++i) 48 | { 49 | cout << "Case #" << i << ": "; 50 | slove(); 51 | } 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /kick start/2018/F/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | typedef long long ll; 16 | const ll INF = 1e17; 17 | 18 | string encode(char c, vector &count) 19 | { 20 | count[c - 'A']++; 21 | string res = ""; 22 | for (auto item : count) 23 | res += 'A' + item; 24 | return res; 25 | } 26 | void slove() 27 | { 28 | int l; 29 | cin >> l; 30 | string a, b; 31 | cin >> a >> b; 32 | unordered_set strsOfB; 33 | for (int i = 0; i < l; ++i) 34 | { 35 | vector tmp(26, 0); 36 | for (int j = i; j < l; ++j) 37 | { 38 | strsOfB.insert(encode(b[j], tmp)); 39 | } 40 | } 41 | 42 | int res = 0; 43 | for (int i = 0; i < l; ++i) 44 | { 45 | vector tmp(26, 0); 46 | for (int j = i; j < l; ++j) 47 | { 48 | auto str = encode(a[j], tmp); 49 | if (strsOfB.count(str)) 50 | res++; 51 | } 52 | } 53 | cout << res << endl; 54 | } 55 | 56 | int main() 57 | { 58 | int t = 0; 59 | cin >> t; 60 | for (int i = 1; i <= t; ++i) 61 | { 62 | cout << "Case #" << i << ": "; 63 | slove(); 64 | } 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /kick start/2019/A/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void slove() 9 | { 10 | int n, p, score; 11 | cin >> n >> p; 12 | vector scores(n + 1, 0); 13 | vector sum(n + 1, 0); 14 | for (int i = 1; i <= n; ++i) 15 | { 16 | cin >> scores[i]; 17 | } 18 | sort(scores.begin(), scores.end()); 19 | for (int i = 1; i <= n; ++i) 20 | { 21 | sum[i] += sum[i - 1] + scores[i]; 22 | } 23 | 24 | int res = INT_MAX; 25 | for (int i = p; i <= n; ++i) 26 | { 27 | int max = scores[i]; 28 | int cur = p * max - (sum[i] - sum[i - p]); 29 | res = min(cur, res); 30 | } 31 | cout << res << endl; 32 | } 33 | int main() 34 | { 35 | int t = 0; 36 | cin >> t; 37 | for (int i = 1; i <= t; ++i) 38 | { 39 | 40 | cout << "Case #" << i << ": "; 41 | slove(); 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /kick start/2019/B/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int maxEnergy(vector> &inputs) 9 | { 10 | sort(inputs.begin(), inputs.end(), [](const array &a, const array &b) -> bool { return b[0] * a[2] > a[0] * b[2]; }); 11 | int n = inputs.size(); 12 | int totalTime = 0; 13 | for (auto stone : inputs) 14 | { 15 | totalTime += stone[0]; 16 | } 17 | vector> dp(n+1, vector(totalTime+1, 0)); 18 | for (int i = 1; i <= n; ++i) 19 | { 20 | auto stone = inputs[i - 1]; 21 | for (int j = 1; j <= totalTime; ++j) 22 | { 23 | if (stone[0] > j) 24 | continue; 25 | dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - stone[0]] + stone[1] - stone[2] * j); 26 | } 27 | } 28 | return dp[n][totalTime]; 29 | 30 | } 31 | int main() 32 | { 33 | int t = 0; 34 | cin >> t; 35 | for (int i = 1; i <= t; ++i) 36 | { 37 | int n; 38 | vector> inputs; 39 | cin >> n; 40 | while (n--) 41 | { 42 | array temp; 43 | cin >> temp[0] >> temp[1] >> temp[2]; 44 | inputs.push_back(temp); 45 | } 46 | int ret = maxEnergy(inputs); 47 | cout << "Case #" << i << ": " << ret << endl; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /kick start/2019/D/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | typedef long long ll; 13 | 14 | int main() 15 | { 16 | vector xorOdd(1025); 17 | for (int i = 0; i <= 1024; ++i) 18 | { 19 | xorOdd[i] = bitset<11>(i).count() & 1; 20 | } 21 | auto t = 0; 22 | cin >> t; 23 | for (int i = 1; i <= t; ++i) 24 | { 25 | cout << "Case #" << i << ":"; 26 | int n, q; 27 | cin >> n >> q; 28 | set indexs; 29 | for (int j = 0; j < n; j++) 30 | { 31 | int x; 32 | cin >> x; 33 | if (xorOdd[x]) 34 | indexs.insert(j); 35 | } 36 | for (int j = 0; j < q; j++) 37 | { 38 | int tmp1, tmp2; 39 | cin >> tmp1 >> tmp2; 40 | if (indexs.count(tmp1)) 41 | indexs.erase(tmp1); 42 | if (xorOdd[tmp2]) 43 | indexs.insert(tmp1); 44 | if (indexs.size() % 2 == 0) 45 | cout << " " << n; 46 | else 47 | { 48 | int r = *indexs.rbegin(); 49 | int l = *indexs.begin(); 50 | cout<<" "< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | typedef long long ll; 13 | 14 | void dfs(int cur, const vector> &graph, vector &visited) 15 | { 16 | visited[cur] = true; 17 | if (graph[cur].empty()) 18 | { 19 | return; 20 | } 21 | for (auto next : graph[cur]) 22 | { 23 | if (visited[next]) 24 | continue; 25 | dfs(next, graph, visited); 26 | } 27 | 28 | } 29 | 30 | int slove(int n, vector> &graph) 31 | { 32 | int cnt = 0; 33 | vector visited(n, false); 34 | for (int i = 0; i < n; ++i) 35 | { 36 | if (!visited[i]){ 37 | dfs(i, graph, visited); 38 | cnt++; 39 | } 40 | } 41 | return 2 * (cnt - 1) + n - cnt; 42 | } 43 | int main() 44 | { 45 | 46 | auto t = 0; 47 | cin >> t; 48 | for (int i = 1; i <= t; ++i) 49 | { 50 | 51 | int n, m; 52 | cin >> n >> m; 53 | vector> graph(n); 54 | for (int j = 0; j < m; j++) 55 | { 56 | int x1, x2; 57 | cin >> x1 >> x2; 58 | graph[x1 - 1].emplace_back(x2 - 1); 59 | graph[x2 - 1].emplace_back(x1 - 1); 60 | } 61 | auto res = slove(n, graph); 62 | cout << "Case #" << i << ": " << res << endl; 63 | } 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /kick start/2019/F/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | typedef long long ll; 16 | 17 | #define INF 0x3f3f3f3f 18 | #define INF64 0x3f3f3f3f3f3f3f3f 19 | 20 | void slove() 21 | { 22 | int n, k; 23 | cin >> n >> k; 24 | vector walls(n + 1, 0); 25 | unordered_set unique; 26 | for (int i = 1; i <= n; ++i) 27 | { 28 | cin >> walls[i]; 29 | unique.insert(walls[i]); 30 | } 31 | vector nums(unique.begin(), unique.end()); 32 | auto t = nums.size(); 33 | vector>> dp(n + 1, vector>(k + 1, vector(t, INF64))); 34 | vector> minD(n + 1, vector(k + 1, INF64)); 35 | minD[1][0] = 0; 36 | for (int i = 0; i < t; ++i) 37 | { 38 | if (walls[1] != nums[i]) 39 | dp[1][0][i] = 1; 40 | else 41 | dp[1][0][i] = 0; 42 | } 43 | 44 | for (int i = 2; i <= n; ++i) 45 | { 46 | for (int j = 0; j <= min(k, i - 1); ++j) 47 | { 48 | for (int m = 0; m < t; ++m) 49 | { 50 | dp[i][j][m] = dp[i - 1][j][m]; 51 | if (j > 0) 52 | dp[i][j][m] = min(dp[i][j][m], minD[i - 1][j - 1]); 53 | if (walls[i] != nums[m]) 54 | dp[i][j][m]++; 55 | minD[i][j] = min(minD[i][j], dp[i][j][m]); 56 | } 57 | } 58 | } 59 | ll res = INF64; 60 | for (int j = 0; j <= k; ++j) 61 | for (int m = 0; m < t; ++m) 62 | res = min(res, dp[n][j][m]); 63 | cout << res << endl; 64 | } 65 | 66 | int main() 67 | { 68 | int t = 0; 69 | cin >> t; 70 | for (int i = 1; i <= t; ++i) 71 | { 72 | cout << "Case #" << i << ": "; 73 | slove(); 74 | } 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /kick start/2019/F/C-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | typedef long long ll; 16 | 17 | #define INF 0x3f3f3f3f 18 | #define INF64 0x3f3f3f3f3f3f3f3f 19 | 20 | ll cal(vector> &graph, vector &sub, vector &values) 21 | { 22 | ll res = 0; 23 | unordered_set visited; 24 | for (auto item : sub) 25 | { 26 | if (!visited.count(item)) 27 | { 28 | res += values[item]; 29 | visited.insert(item); 30 | } 31 | for (auto adj : graph[item]) 32 | { 33 | if (!visited.count(adj)) 34 | { 35 | res += values[adj]; 36 | visited.insert(adj); 37 | } 38 | } 39 | } 40 | return res; 41 | } 42 | void slove() 43 | { 44 | int v; 45 | cin >> v; 46 | vector> graph(v); 47 | vector values(v); 48 | for (int i = 0; i < v; ++i) 49 | { 50 | cin >> values[i]; 51 | } 52 | for (int i = 0; i < v - 1; ++i) 53 | { 54 | int v1, v2; 55 | cin >> v1 >> v2; 56 | graph[v1 - 1].emplace_back(v2 - 1); 57 | graph[v2 - 1].emplace_back(v1 - 1); 58 | } 59 | ll res = -INF64; 60 | 61 | for (ll i = 0; i <= (1 << v); ++i) 62 | { 63 | vector sub; 64 | for (ll j = 0; j < v; ++j) 65 | { 66 | if (i & (1 << j)) 67 | sub.emplace_back(j); 68 | } 69 | res = max(res, cal(graph, sub, values)); 70 | } 71 | cout << res << endl; 72 | } 73 | 74 | int main() 75 | { 76 | int t = 0; 77 | cin >> t; 78 | for (int i = 1; i <= t; ++i) 79 | { 80 | cout << "Case #" << i << ": "; 81 | slove(); 82 | } 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /kick start/2019/G/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | typedef long long ll; 16 | 17 | #define INF 0x3f3f3f3f 18 | #define INF64 0x3f3f3f3f3f3f3f3f 19 | 20 | void slove() 21 | { 22 | int n, m, q, tmp; 23 | cin >> n >> m >> q; 24 | unordered_set pages; 25 | vector readers(q, 0); 26 | for (int i = 0; i < m; ++i) 27 | { 28 | cin >> tmp; 29 | pages.insert(tmp); 30 | } 31 | for (int i = 0; i < q; ++i) 32 | { 33 | cin >> readers[i]; 34 | } 35 | vector count(n + 1, 0); 36 | for (int i = 1; i <= n; ++i) 37 | { 38 | int cnt = n / i; 39 | for (int j = i; j <= n; j += i) 40 | { 41 | if (pages.count(j)) 42 | cnt--; 43 | } 44 | count[i] = cnt; 45 | } 46 | ll res = 0; 47 | for (auto reader : readers) 48 | { 49 | res += count[reader]; 50 | } 51 | cout << res << endl; 52 | } 53 | 54 | int main() 55 | { 56 | int t = 0; 57 | cin >> t; 58 | for (int i = 1; i <= t; ++i) 59 | { 60 | cout << "Case #" << i << ": "; 61 | slove(); 62 | } 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /kick start/2019/G/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | typedef long long ll; 16 | 17 | #define INF 0x3f3f3f3f 18 | #define INF64 0x3f3f3f3f3f3f3f3f 19 | 20 | void slove() 21 | { 22 | ll n, m; 23 | cin >> n >> m; 24 | vector nums(n, 0); 25 | for (ll i = 0; i < n; ++i) 26 | { 27 | cin >> nums[i]; 28 | } 29 | vector count(51, 0); 30 | for (ll i = 0; i < 51; ++i) 31 | { 32 | for (ll j = 0; j < n; ++j) 33 | { 34 | if ((nums[j] & (1ll << i)) > 0) 35 | count[i]++; 36 | } 37 | } 38 | ll res = 0; 39 | ll sum = 0; 40 | for (ll i = 50; i >= 0; --i) 41 | { 42 | ll a = n - count[i]; 43 | if (a <= count[i]) 44 | { 45 | sum += (1ll << i) * a; 46 | res += (1ll << i); 47 | } 48 | else 49 | { 50 | sum += (1ll << i) * count[i]; 51 | } 52 | } 53 | if (sum > m) 54 | { 55 | cout << -1 << endl; 56 | return; 57 | } 58 | for (ll i = 50; i >= 0; --i) 59 | { 60 | ll a = n - count[i]; 61 | if (a > count[i]) 62 | { 63 | if (sum + (a - count[i]) * (1ll << i) <= m) 64 | { 65 | sum += (a - count[i]) * (1ll << i); 66 | res += (1ll << i); 67 | } 68 | } 69 | } 70 | cout << res << endl; 71 | } 72 | 73 | int main() 74 | { 75 | ll t = 0; 76 | cin >> t; 77 | for (ll i = 1; i <= t; ++i) 78 | { 79 | cout << "Case #" << i << ": "; 80 | slove(); 81 | } 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /kick start/2019/H/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | typedef long long ll; 15 | 16 | #define INF 0x3f3f3f3f 17 | #define INF64 0x3f3f3f3f3f3f3f3f 18 | 19 | void slove() 20 | { 21 | int n, tmp; 22 | cin >> n; 23 | vector minHeap; 24 | vector papers(n, 0); 25 | cin >> tmp; 26 | minHeap.emplace_back(tmp); 27 | make_heap(minHeap.begin(), minHeap.end(), greater()); 28 | int cur = 1; 29 | for (int i = 2; i <= n; ++i) 30 | { 31 | cin >> tmp; 32 | if (tmp > cur) 33 | { 34 | minHeap.push_back(tmp); 35 | push_heap(minHeap.begin(), minHeap.end(), greater()); 36 | } 37 | while (!minHeap.empty() && minHeap.front() <= cur) 38 | { 39 | pop_heap(minHeap.begin(), minHeap.end(), greater()); 40 | minHeap.pop_back(); 41 | } 42 | cur = max(cur, (int)minHeap.size()); 43 | // cin >> papers[i]; 44 | } 45 | cout << cur << endl; 46 | } 47 | 48 | int main() 49 | { 50 | int t = 0; 51 | cin >> t; 52 | for (int i = 1; i <= t; ++i) 53 | { 54 | cout << "Case #" << i << ": "; 55 | slove(); 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /kick start/2019/H/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | typedef long long ll; 16 | 17 | void slove() 18 | { 19 | bool res = false; 20 | vector nums(200, 0); 21 | ll cnt = 0, tmp, cnt1 = 0, cnt2 = 0, sum = 0; 22 | vector> others; 23 | for (int i = 1; i <= 9; ++i) 24 | { 25 | cin >> tmp; 26 | if (tmp > 10) 27 | { 28 | cnt1++; 29 | others.push_back(make_pair(tmp, i)); 30 | } 31 | else 32 | { 33 | sum += tmp; 34 | while ((tmp--) > 0) 35 | { 36 | nums[cnt++] = i; 37 | } 38 | } 39 | if (tmp > 6) 40 | cnt2++; 41 | } 42 | if (cnt1 >= 2 || cnt2 >= 3) 43 | { 44 | cout << "Yes" << endl; 45 | return; 46 | } 47 | if (!others.empty()) 48 | { 49 | tmp = sum + ((others[0].first - sum) & 1); 50 | while ((tmp--) > 0) 51 | { 52 | nums[cnt++] = others[0].second; 53 | } 54 | } 55 | 56 | do 57 | { 58 | ll cur = 0; 59 | int t = 1; 60 | for (int i = 0; i < cnt; ++i) 61 | { 62 | cur += t * nums[i]; 63 | t = -t; 64 | } 65 | if (cur % 11 == 0) 66 | { 67 | res = true; 68 | break; 69 | } 70 | } while (next_permutation(nums.begin(), nums.begin() + cnt)); 71 | cout << (res ? "YES" : "NO") << endl; 72 | } 73 | 74 | int main() 75 | { 76 | int t = 0; 77 | cin >> t; 78 | for (int i = 1; i <= t; ++i) 79 | { 80 | cout << "Case #" << i << ": "; 81 | slove(); 82 | } 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /kick start/2020/A/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void slove() { 9 | int n, b; 10 | cin >> n >> b; 11 | vector costs(n, 0); 12 | for (int i = 1; i <= n; ++i) { 13 | cin >> costs[i - 1]; 14 | } 15 | sort(costs.begin(), costs.end()); 16 | int res = 0, cur = 0; 17 | for (auto cost : costs) { 18 | cur += cost; 19 | if (cur > b) break; 20 | res++; 21 | } 22 | cout << res << endl; 23 | } 24 | int main() { 25 | int t = 0; 26 | cin >> t; 27 | for (int i = 1; i <= t; ++i) { 28 | cout << "Case #" << i << ": "; 29 | slove(); 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /kick start/2020/A/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void slove() { 9 | int n, k, p; 10 | cin >> n >> k >> p; 11 | 12 | vector> beaty(n, vector(k, 0)); 13 | for (int i = 0; i < n; ++i) { 14 | for (int j = 0; j < k; ++j) { 15 | cin >> beaty[i][j]; 16 | } 17 | } 18 | vector> sums(n, vector(k + 1, 0)); 19 | for (int i = 1; i <= k; ++i) { 20 | for (int j = 0; j < n; ++j) { 21 | sums[j][i] = sums[j][i - 1] + beaty[j][i - 1]; 22 | } 23 | } 24 | vector> dp(n + 1, vector(p + 1, 0)); 25 | for (int i = 1; i <= n; ++i) { 26 | for (int j = 1; j <= p; ++j) { 27 | dp[i][j] = INT_MIN; 28 | int max_per = min(j, k); 29 | for (int k = 0; k <= max_per; ++k) { 30 | dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + sums[i - 1][k]); 31 | } 32 | } 33 | } 34 | cout << dp[n][p] << endl; 35 | } 36 | 37 | int main() { 38 | int t = 0; 39 | cin >> t; 40 | for (int i = 1; i <= t; ++i) { 41 | cout << "Case #" << i << ": "; 42 | slove(); 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /kick start/2020/A/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | typedef long long ll; 11 | 12 | bool is_sussess(vector& times, ll target, ll k) { 13 | ll cur = 0; 14 | for (int i = 1; i < times.size(); ++i) { 15 | ll cur_diff = llabs(times[i] - times[i - 1]); 16 | if (cur_diff > target) { 17 | cur += cur_diff / target; 18 | if (cur_diff % target == 0) { 19 | cur--; 20 | } 21 | } 22 | if (cur > k) return false; 23 | } 24 | return true; 25 | } 26 | 27 | void slove() { 28 | ll n, k; 29 | cin >> n >> k; 30 | 31 | vector times(n, 0); 32 | ll max_diff = 0; 33 | for (int i = 0; i < n; ++i) { 34 | cin >> times[i]; 35 | if (i == 0) continue; 36 | max_diff = max(max_diff, llabs(times[i] - times[i - 1])); 37 | } 38 | ll l = 1, r = max_diff; 39 | while (l <= r) { 40 | ll mid = (l + r) / 2; 41 | if (is_sussess(times, mid, k)) { 42 | r = mid-1; 43 | } else { 44 | l = mid+1; 45 | } 46 | } 47 | cout << l << endl; 48 | } 49 | 50 | int main() { 51 | int t = 0; 52 | cin >> t; 53 | for (int i = 1; i <= t; ++i) { 54 | cout << "Case #" << i << ": "; 55 | slove(); 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /kick start/2020/B/A.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void slove() { 9 | int n; 10 | cin >> n; 11 | vector heights(n, 0); 12 | for (int i = 1; i <= n; ++i) { 13 | cin >> heights[i - 1]; 14 | } 15 | int res = 0, be = heights[0], af = heights[1]; 16 | for (int i = 1; i < n - 1; ++i) { 17 | if (heights[i] > be && heights[i] < af) { 18 | res++; 19 | } 20 | if (i == n - 2) break; 21 | be = heights[i]; 22 | af = heights[i + 2]; 23 | } 24 | cout << res << endl; 25 | } 26 | int main() { 27 | int t = 0; 28 | cin >> t; 29 | for (int i = 1; i <= t; ++i) { 30 | cout << "Case #" << i << ": "; 31 | slove(); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /kick start/2020/B/C.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | typedef long long ll; 11 | 12 | bool is_sussess(vector& times, ll target, ll k) { 13 | ll cur = 0; 14 | for (int i = 1; i < times.size(); ++i) { 15 | ll cur_diff = llabs(times[i] - times[i - 1]); 16 | if (cur_diff > target) { 17 | cur += cur_diff / target; 18 | if (cur_diff % target == 0) { 19 | cur--; 20 | } 21 | } 22 | if (cur > k) return false; 23 | } 24 | return true; 25 | } 26 | 27 | void slove() { 28 | ll n, k; 29 | cin >> n >> k; 30 | 31 | vector times(n, 0); 32 | ll max_diff = 0; 33 | for (int i = 0; i < n; ++i) { 34 | cin >> times[i]; 35 | if (i == 0) continue; 36 | max_diff = max(max_diff, llabs(times[i] - times[i - 1])); 37 | } 38 | ll l = 1, r = max_diff; 39 | while (l <= r) { 40 | ll mid = (l + r) / 2; 41 | if (is_sussess(times, mid, k)) { 42 | r = mid-1; 43 | } else { 44 | l = mid+1; 45 | } 46 | } 47 | cout << l << endl; 48 | } 49 | 50 | int main() { 51 | int t = 0; 52 | cin >> t; 53 | for (int i = 1; i <= t; ++i) { 54 | cout << "Case #" << i << ": "; 55 | slove(); 56 | } 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /kick start/2020/C/B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void slove() { 9 | int n, k, p; 10 | cin >> n >> k >> p; 11 | 12 | vector> beaty(n, vector(k, 0)); 13 | for (int i = 0; i < n; ++i) { 14 | for (int j = 0; j < k; ++j) { 15 | cin >> beaty[i][j]; 16 | } 17 | } 18 | vector> sums(n, vector(k + 1, 0)); 19 | for (int i = 1; i <= k; ++i) { 20 | for (int j = 0; j < n; ++j) { 21 | sums[j][i] = sums[j][i - 1] + beaty[j][i - 1]; 22 | } 23 | } 24 | vector> dp(n + 1, vector(p + 1, 0)); 25 | for (int i = 1; i <= n; ++i) { 26 | for (int j = 1; j <= p; ++j) { 27 | dp[i][j] = INT_MIN; 28 | int max_per = min(j, k); 29 | for (int k = 0; k <= max_per; ++k) { 30 | dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + sums[i - 1][k]); 31 | } 32 | } 33 | } 34 | cout << dp[n][p] << endl; 35 | } 36 | 37 | int main() { 38 | int t = 0; 39 | cin >> t; 40 | for (int i = 1; i <= t; ++i) { 41 | cout << "Case #" << i << ": "; 42 | slove(); 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /kick start/2020/C/D.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | typedef long long ll; 12 | 13 | void slove() { 14 | int n, q; 15 | cin >> n >> q; 16 | 17 | vector nums(n + 1), nums1(n + 1), nums2(n + 1); 18 | vector sums(n + 1, 0), sums1(n + 1, 0), sums2(n + 1, 0); 19 | 20 | for (int i = 1; i <= n; ++i) { 21 | cin >> nums[i]; 22 | // sums[i] = sums[i - 1] + nums[i]; 23 | // if (i & 1) { 24 | // nums1[i] = -2 * nums[i]; 25 | // nums2[i] = 0; 26 | // } else { 27 | // nums2[i] = -2 * nums[i]; 28 | // nums1[i] = 0; 29 | // } 30 | // sums1[i] = sums1[i - 1] + nums1[i]; 31 | // sums2[i] = sums2[i - 1] + nums2[i]; 32 | } 33 | int res = 0; 34 | for (int i = 0; i < q; ++i) { 35 | char c; 36 | int x, y; 37 | cin >> c >> x >> y; 38 | if (c == 'Q') { 39 | int k = 1, cur = 0; 40 | for (int j = x; j <= y; ++j) { 41 | cur += k * nums[j] * (j - x + 1); 42 | k *= -1; 43 | } 44 | res += cur; 45 | } else { 46 | nums[x] = y; 47 | } 48 | } 49 | cout << res << endl; 50 | } 51 | 52 | int main() { 53 | int t = 0; 54 | cin >> t; 55 | for (int i = 1; i <= t; ++i) { 56 | cout << "Case #" << i << ": "; 57 | slove(); 58 | } 59 | system("pause"); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /leetcode/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | vector twoSum(vector &nums, int target) 11 | { 12 | unordered_map indexs; 13 | int i = 0; 14 | vector ret(0); 15 | for (auto num : nums) 16 | { 17 | auto temp = target - num; 18 | if (indexs.find(temp) != indexs.end()) 19 | { 20 | ret.push_back(i); 21 | ret.push_back(indexs[temp]); 22 | break; 23 | } 24 | if (indexs.find(num) == indexs.end()) 25 | indexs.insert({num, i}); 26 | i++; 27 | } 28 | return ret; 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /leetcode/10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Solution 6 | { 7 | private: 8 | public: 9 | bool isMatch(string s, string p) 10 | { 11 | int m = s.size(), n = p.size(); 12 | vector> opt(m + 1, vector(n + 1, false)); 13 | opt[0][0] = true; 14 | for (int i = 0; i <= m; i++) { 15 | for (int j = 1; j <= n; j++) { 16 | if (p[j - 1] == '*') { 17 | opt[i][j] = opt[i][j - 2] || (i && opt[i - 1][j] && (s[i - 1] == p[j - 2] || p[j - 2] == '.')); 18 | } else { 19 | opt[i][j] = i && opt[i - 1][j - 1] && (s[i - 1] == p[j - 1] || p[j - 1] == '.'); 20 | } 21 | } 22 | } 23 | return opt[m][n]; 24 | } 25 | }; 26 | int main() 27 | { 28 | system("pause"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/101.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct TreeNode 7 | { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | bool check(TreeNode *node1, TreeNode *node2) 17 | { 18 | if (node1 == nullptr || node2 == nullptr) 19 | return node2 == nullptr && node1 == nullptr; 20 | if (node1->val == node2->val) 21 | return check(node1->left, node2->right) && check(node1->right, node2->left); 22 | return false; 23 | } 24 | 25 | public: 26 | bool isSymmetric(TreeNode *root) 27 | { 28 | if (root == nullptr) 29 | return true; 30 | return check(root->left, root->right); 31 | } 32 | }; 33 | int main() 34 | { 35 | system("pause"); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /leetcode/1013.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | #define INF 0x3f3f3f3f 12 | #define INF64 0x3f3f3f3f3f3f3f3f 13 | 14 | class Solution { 15 | public: 16 | bool canThreePartsEqualSum(vector& A) { 17 | if (A.size() < 3) return false; 18 | int n = A.size(); 19 | vector sum(n, 0); 20 | sum[0] = A[0]; 21 | for (int i = 1; i < n; ++i) { 22 | sum[i] += sum[i - 1] + A[i]; 23 | } 24 | if (sum[n - 1] % 3 != 0) return false; 25 | int d = sum[n - 1] / 3; 26 | int cur = d, cnt = 1; 27 | for (int i = 0; i < n; ++i) { 28 | if (cur == sum[i]) { 29 | if (cnt == 1) 30 | cur = d * (++cnt); 31 | else 32 | return i != n - 1; 33 | } 34 | } 35 | return false; 36 | } 37 | }; 38 | int main() { 39 | vector inputs = {10, -10, 10, -10, 10, -10, 10, -10}; 40 | Solution s; 41 | s.canThreePartsEqualSum(inputs); 42 | system("pause"); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /leetcode/102.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct TreeNode 8 | { 9 | int val; 10 | TreeNode *left; 11 | TreeNode *right; 12 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 13 | }; 14 | 15 | class Solution 16 | { 17 | public: 18 | vector> FlevelOrder(TreeNode *root) 19 | { 20 | vector> res; 21 | if (root == nullptr) 22 | return res; 23 | queue q; 24 | vector vec; 25 | q.push(root); 26 | q.push(nullptr); 27 | while (!q.empty()) 28 | { 29 | 30 | auto cur = q.front(); 31 | q.pop(); 32 | if (cur == nullptr) 33 | { 34 | res.emplace_back(vec); 35 | vec.resize(0); 36 | if (!q.empty()) 37 | q.push(nullptr); 38 | } 39 | else 40 | { 41 | vec.emplace_back(cur->val); 42 | if (cur->left != nullptr) 43 | q.push(cur->left); 44 | if (cur->right != nullptr) 45 | q.push(cur->right); 46 | } 47 | } 48 | return res; 49 | } 50 | }; 51 | 52 | int main() 53 | { 54 | system("pause"); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /leetcode/103.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct TreeNode 8 | { 9 | int val; 10 | TreeNode *left; 11 | TreeNode *right; 12 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 13 | }; 14 | 15 | class Solution 16 | { 17 | private: 18 | void visit(stack s, vector> &ret, bool &flag) 19 | { 20 | vector vec; 21 | stack stack; 22 | while (!s.empty()) 23 | { 24 | vec.emplace_back(s.top()->val); 25 | if (flag) 26 | { 27 | if (s.top()->left != nullptr) 28 | stack.push(s.top()->left); 29 | if (s.top()->right != nullptr) 30 | stack.push(s.top()->right); 31 | } 32 | else 33 | { 34 | if (s.top()->right != nullptr) 35 | stack.push(s.top()->right); 36 | if (s.top()->left != nullptr) 37 | stack.push(s.top()->left); 38 | } 39 | s.pop(); 40 | } 41 | flag = !flag; 42 | ret.emplace_back(vec); 43 | if (!stack.empty()) 44 | visit(stack, ret, flag); 45 | } 46 | 47 | public: 48 | vector> zigzagLevelOrder(TreeNode *root) 49 | { 50 | vector> ret; 51 | if(root==nullptr) 52 | return ret; 53 | stack stack; 54 | stack.push(root); 55 | bool flag = true; 56 | visit(stack, ret, flag); 57 | return ret; 58 | } 59 | }; 60 | int main() 61 | { 62 | system("pause"); 63 | return 0; 64 | } -------------------------------------------------------------------------------- /leetcode/104.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct TreeNode 7 | { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | private: 17 | int visit(TreeNode *node) 18 | { 19 | if (node == nullptr) 20 | return 0; 21 | return max(visit(node->right) + 1, visit(node->left) + 1); 22 | } 23 | 24 | public: 25 | int maxDepth(TreeNode *root) 26 | { 27 | return visit(root); 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | system("pause"); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /leetcode/105.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct TreeNode { 9 | int val; 10 | TreeNode *left; 11 | TreeNode *right; 12 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 13 | }; 14 | 15 | class Solution { 16 | private: 17 | TreeNode* buildNode(vector& preorder, vector& inorder,int beginPre, int endPre,int beginIn,int endIn){ 18 | if(beginPre==endPre) 19 | 20 | } 21 | public: 22 | TreeNode* buildTree(vector& preorder, vector& inorder) { 23 | 24 | } 25 | }; 26 | int main() 27 | { 28 | system("pause"); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /leetcode/108.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct TreeNode 7 | { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | TreeNode *createNode(vector &nums, int left, int right) 17 | { 18 | if (left > right) 19 | return nullptr; 20 | int mid = (left + right) / 2; 21 | if (left == right) 22 | return new TreeNode(nums[mid]); 23 | 24 | TreeNode *node = new TreeNode(nums[mid]); 25 | 26 | node->left = createNode(nums, left, mid - 1); 27 | node->right = createNode(nums, mid + 1, right); 28 | return node; 29 | } 30 | 31 | public: 32 | TreeNode *sortedArrayToBST(vector &nums) 33 | { 34 | if (nums.empty()) 35 | return nullptr; 36 | auto root = createNode(nums, 0, nums.size() - 1); 37 | return root; 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | system("pause"); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /leetcode/11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int maxArea(vector &height) 12 | { 13 | int i = 0, j = height.size() - 1, water = 0; 14 | while (i < j) 15 | { 16 | int h = min(height[i], height[j]); 17 | water = max(water, (j - i) * h); 18 | while (height[i] <= h && i < j) 19 | i++; 20 | while (height[j] <= h && i < j) 21 | j--; 22 | } 23 | return water; 24 | } 25 | }; 26 | 27 | int main() 28 | { 29 | system("pause"); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /leetcode/112.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct TreeNode 8 | { 9 | int val; 10 | TreeNode *left; 11 | TreeNode *right; 12 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 13 | }; 14 | 15 | class Solution 16 | { 17 | public: 18 | bool hasPathSum(TreeNode *root, int sum) 19 | { 20 | if (root == nullptr) 21 | return false; 22 | if (root->left == nullptr && root->right == nullptr && sum == root->val) 23 | return true; 24 | return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); 25 | } 26 | }; 27 | int main() 28 | { 29 | system("pause"); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /leetcode/115.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int numDistinct(string s, string t) 12 | { 13 | int m = s.length(), n = t.length(); 14 | if (m == 0 || m < n) 15 | { 16 | return 0; 17 | } 18 | vector> dp(m + 1, vector(n + 1, 0)); 19 | for (int i = 0; i <= m; ++i) 20 | { 21 | dp[i][0] = 1; 22 | } 23 | for (int i = 1; i <= m; ++i) 24 | { 25 | for (int j = 1; j <= n; ++j) 26 | { 27 | dp[i][j] = dp[i - 1][j] + (s[i - 1] == t[j - 1] ? dp[i - 1][j - 1] : 0); 28 | } 29 | } 30 | return dp[m][n]; 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | system("pause"); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /leetcode/116.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | // Definition for a Node. 5 | class Node { 6 | public: 7 | int val; 8 | Node* left; 9 | Node* right; 10 | Node* next; 11 | 12 | Node() {} 13 | 14 | Node(int _val, Node* _left, Node* _right, Node* _next) { 15 | val = _val; 16 | left = _left; 17 | right = _right; 18 | next = _next; 19 | } 20 | }; 21 | class Solution { 22 | private: 23 | void popluate(Node* node){ 24 | if(node->left==nullptr) 25 | return; 26 | node->left->next = node->right; 27 | node->right->next = node->next == nullptr ? nullptr : node->next->left; 28 | popluate(node->left); 29 | popluate(node->right); 30 | } 31 | public: 32 | Node* connect(Node* root) { 33 | root->next = nullptr; 34 | popluate(root); 35 | return root; 36 | } 37 | }; 38 | int main(){ 39 | system("pause"); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /leetcode/118.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | vector> generate(int numRows) 10 | { 11 | vector> res; 12 | if (numRows < 1) 13 | return res; 14 | for (int i = 0; i < numRows; ++i) 15 | res.emplace_back(vector(i + 1, 1)); 16 | if (numRows == 1) 17 | return res; 18 | for (int i = 1; i < numRows - 1; ++i) 19 | for (int j = 1; j <= i; ++j) 20 | res[i + 1][j] = res[i][j - 1] + res[i][j]; 21 | return res; 22 | } 23 | }; 24 | int main() 25 | { 26 | system("pause"); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /leetcode/120.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int minimumTotal(vector> &triangle) 12 | { 13 | long long m = triangle.size(), res = INT_MAX; 14 | if (m == 0) 15 | return 0; 16 | vector> dp(2, vector(m, INT_MAX)); 17 | dp[0][0] = triangle[0][0]; 18 | for (int i = 1; i < m; ++i) 19 | { 20 | for (int j = 0; j <= i; ++j) 21 | { 22 | dp[1][j] = min(dp[0][j], j > 0 ? dp[0][j - 1] : INT_MAX) + triangle[i][j]; 23 | } 24 | dp[0] = dp[1]; 25 | } 26 | for (auto item : dp[0]) 27 | { 28 | res = min(res, item); 29 | } 30 | return res; 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | system("pause"); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /leetcode/1200.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Solution { 13 | public: 14 | vector> minimumAbsDifference(vector& arr) { 15 | int diff = INT_MAX,n=arr.size(); 16 | sort(arr.begin(), arr.end()); 17 | for(int i=0;i> res; 22 | for (int i = 0; i < n - 1; ++i) 23 | { 24 | if(arr[i + 1] - arr[i]==diff) 25 | { 26 | res.emplace_back(vector{arr[i], arr[i + 1]}); 27 | } 28 | } 29 | return res; 30 | } 31 | }; 32 | 33 | 34 | int main() 35 | { 36 | return 0; 37 | } -------------------------------------------------------------------------------- /leetcode/1201.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | 10 | class Solution 11 | { 12 | ll lcm(ll a,ll b){ 13 | return a * b / __gcd(a, b); 14 | } 15 | 16 | public: 17 | int nthUglyNumber(int n, int a, int b, int c) 18 | { 19 | if(a>b) 20 | swap(a, b); 21 | if(b>c) 22 | swap(b, c); 23 | if(a>b) 24 | swap(a, b); 25 | ll ab = lcm(a, b); 26 | ll bc = lcm(b, c); 27 | ll ac = lcm(a, c); 28 | ll abc = lcm(a, bc); 29 | ll l = 0, r = 2e9; 30 | while(l 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | 10 | class Solution 11 | { 12 | void dfs(vector> &g, int cur, vector &indexs, vector &visited) 13 | { 14 | indexs.emplace_back(cur); 15 | visited[cur] = true; 16 | for (auto i : g[cur]) 17 | { 18 | if (!visited[i]) 19 | dfs(g, i, indexs, visited); 20 | } 21 | } 22 | 23 | public: 24 | string smallestStringWithSwaps(string s, vector> &pairs) 25 | { 26 | int n = s.length(); 27 | vector> graph(n); 28 | for (auto pair : pairs) 29 | { 30 | graph[pair[0]].emplace_back(pair[1]); 31 | graph[pair[1]].emplace_back(pair[0]); 32 | } 33 | vector visited(n, false); 34 | for (int i = 0; i < n; ++i) 35 | { 36 | if (visited[i]) 37 | continue; 38 | vector indexs; 39 | dfs(graph, i, indexs, visited); 40 | sort(indexs.begin(), indexs.end()); 41 | vector tmp; 42 | for (auto index : indexs) 43 | { 44 | tmp.emplace_back(s[index]); 45 | } 46 | sort(tmp.begin(), tmp.end()); 47 | for (int j = 0; j < indexs.size(); ++j) 48 | { 49 | s[indexs[j]] = tmp[j]; 50 | } 51 | } 52 | return s; 53 | } 54 | }; 55 | 56 | int main() 57 | { 58 | system("pause"); 59 | return 0; 60 | } -------------------------------------------------------------------------------- /leetcode/1204.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | typedef long long ll; 12 | 13 | const vector dx = {1, -1}; 14 | const vector dy = {1, -1}; 15 | 16 | class Solution 17 | { 18 | { 19 | bool check(int i, int j, int m, int n) 20 | { 21 | return (i < m && i >= 0) && (j < n && j >= 0); 22 | } 23 | 24 | public: 25 | int getMaximumGold(vector> &grid) 26 | { 27 | if (grid.empty() || grid[0].empty()) 28 | return 0; 29 | int m = grid.size(), n = grid[0].size(); 30 | vector> visited(m, vector(n, false)); 31 | int res = 0; 32 | for (int i = 0; i < m; ++i) 33 | { 34 | for (int j = 0; j < n; ++j) 35 | { 36 | if (grid[i][j] != 0 && !visited[i][j]) 37 | { 38 | queue> q; 39 | q.insert(make_pair(i, j)); 40 | } 41 | } 42 | } 43 | } 44 | }; 45 | 46 | int main() 47 | { 48 | 49 | system("pause"); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /leetcode/1217.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Solution 12 | { 13 | public: 14 | int minCostToMoveChips(vector &chips) 15 | { 16 | int even = 0, odd = 0; 17 | for (auto chip : chips) 18 | { 19 | chip & 1 ? odd++ : even++; 20 | } 21 | return min(odd, even); 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | 28 | system("pause"); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /leetcode/1218.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Solution 10 | { 11 | public: 12 | int longestSubsequence(vector &arr, int difference) 13 | { 14 | 15 | unordered_map forwards; 16 | int res = 0; 17 | for (auto item : arr) 18 | { 19 | if (forwards.count(item - difference)) 20 | { 21 | forwards[item] = forwards[item - difference] + 1; 22 | res = max(res, forwards[item]); 23 | } 24 | else 25 | { 26 | forwards[item] = 1; 27 | } 28 | } 29 | return res == 0 ? 1 : res; 30 | } 31 | }; 32 | 33 | int main() 34 | { 35 | system("pause"); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /leetcode/1219.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | const vector dx = {-1, 0, 0, 1}; 12 | const vector dy = {0, 1, -1, 0}; 13 | 14 | class Solution 15 | { 16 | bool check(int i, int j, int m, int n) 17 | { 18 | return (i < m && i >= 0) && (j < n && j >= 0); 19 | } 20 | 21 | int dfs(vector> &grid, int x, int y, int m, int n) 22 | { 23 | auto tmp = grid[x][y]; 24 | grid[x][y] = 0; 25 | auto res = 0; 26 | for (int i = 0; i < 4; ++i) 27 | { 28 | 29 | auto x1 = x + dx[i], y1 = y + dy[i]; 30 | if (check(x1, y1, m, n) && grid[x1][y1] != 0) 31 | { 32 | res = max(res, grid[x1][y1] + dfs(grid, x1, y1, m, n)); 33 | } 34 | } 35 | grid[x][y] = tmp; 36 | return res; 37 | } 38 | 39 | public: 40 | int getMaximumGold(vector> &grid) 41 | { 42 | if (grid.empty() || grid[0].empty()) 43 | return 0; 44 | int m = grid.size(), n = grid[0].size(); 45 | int res = 0; 46 | for (int i = 0; i < m; ++i) 47 | { 48 | for (int j = 0; j < n; ++j) 49 | { 50 | if (grid[i][j] > 0) 51 | res = max(res, grid[i][j] + dfs(grid, i, j, m, n)); 52 | } 53 | } 54 | return res; 55 | } 56 | }; 57 | 58 | int main() 59 | { 60 | 61 | system("pause"); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /leetcode/122.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int maxProfit(vector &prices) 10 | { 11 | if (prices.size() < 2) 12 | return 0; 13 | int min = prices[0]; 14 | int profit = 0; 15 | for (int i = 1; i < prices.size() - 1; ++i) 16 | { 17 | if (prices[i] < min) 18 | { 19 | min = prices[i]; 20 | continue; 21 | } 22 | if (prices[i] > prices[i + 1]) 23 | { 24 | profit += prices[i] - min; 25 | min = prices[i + 1]; 26 | i++; 27 | } 28 | } 29 | profit += prices.back() - min > 0 ? prices.back() - min : 0; 30 | return profit; 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | system("pause"); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /leetcode/123.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | private: 9 | void maxProfitInFrontOrder(vector &prices, vector &dp) 10 | { 11 | int min = prices[0]; 12 | dp[0] = 0; 13 | int profit = 0; 14 | for (int i = 1; i < prices.size(); ++i) 15 | { 16 | profit = max(prices[i] - min, profit); 17 | dp[i] = profit; 18 | if (min > prices[i]) 19 | min = prices[i]; 20 | } 21 | } 22 | void maxProfitInBackOrder(vector &prices, vector &dp) 23 | { 24 | int max1 = prices.back(); 25 | dp.back() = 0; 26 | int profit = 0; 27 | for (int i = prices.size() - 2; i >= 0; --i) 28 | { 29 | profit = max(max1 - prices[i], profit); 30 | dp[i] = profit; 31 | if (max1 < prices[i]) 32 | max1 = prices[i]; 33 | } 34 | } 35 | 36 | public: 37 | int maxProfit(vector &prices) 38 | { 39 | if (prices.size() < 2) 40 | return 0; 41 | vector frontDp(prices.size(), 0); 42 | vector backDp(prices.size(), 0); 43 | maxProfitInFrontOrder(prices, frontDp); 44 | maxProfitInBackOrder(prices, backDp); 45 | int profit = 0; 46 | for (int i = 1; i < prices.size() - 1; ++i) 47 | { 48 | profit = max(frontDp[i] + backDp[i + 1], profit); 49 | } 50 | profit = max(frontDp.back(), profit); 51 | return profit; 52 | } 53 | }; 54 | 55 | int main() 56 | { 57 | vector inputs = {3, 2, 6, 5, 0, 3}; 58 | Solution solution; 59 | cout << solution.maxProfit(inputs) << endl; 60 | system("pause"); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /leetcode/124.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | struct TreeNode 10 | { 11 | int val; 12 | TreeNode *left; 13 | TreeNode *right; 14 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 15 | }; 16 | 17 | class Solution 18 | { 19 | private: 20 | int dfs(TreeNode *node, int &res) 21 | { 22 | int l = node->left != nullptr ? dfs(node->left, res) : INT_MIN; 23 | int r = node->right != nullptr ? dfs(node->right, res) : INT_MIN; 24 | res = max(res, l); 25 | res = max(res, r); 26 | res = max(res, node->val); 27 | if (l > 0) 28 | res = max(res, l + node->val); 29 | if (r > 0) 30 | res = max(res, r + node->val); 31 | if (l != INT_MIN && r != INT_MIN && l + r > 0) 32 | res = max(res, l + r + node->val); 33 | int resL = l > 0 ? l + node->val : node->val; 34 | int resR = r > 0 ? r + node->val : node->val; 35 | return max(resL, resR); 36 | } 37 | 38 | public: 39 | int maxPathSum(TreeNode *root) 40 | { 41 | int res = INT_MIN; 42 | if (root != nullptr) 43 | dfs(root, res); 44 | return res; 45 | } 46 | }; 47 | 48 | int main() 49 | { 50 | system("pause"); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /leetcode/125.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | bool isPalindrome(string s) 11 | { 12 | for (int i = 0, j = s.size() - 1; i < j; i++, j--) 13 | { 14 | while (isalnum(s[i]) == false && i < j) 15 | i++; 16 | while (isalnum(s[j]) == false && i < j) 17 | j--; 18 | if (toupper(s[i]) != toupper(s[j])) 19 | return false; 20 | } 21 | 22 | return true; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | system("pause"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/1266.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Solution { 12 | public: 13 | int minTimeToVisitAllPoints(vector>& points) { 14 | if(points.size()<2) 15 | return 0; 16 | auto start=points[0]; 17 | int res=0; 18 | for(int i=1;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Solution { 12 | public: 13 | int countServers(vector>& grid) 14 | { 15 | vector rows(grid.size(),0),columns(grid[0].size(),0); 16 | for(int i=0;i1||rows[i]>1)) 24 | result++; 25 | return result; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | 32 | system("pause"); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /leetcode/1275.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Solution { 12 | public: 13 | string tictactoe(vector>& moves) { 14 | vector A(8, 0), B(8, 0); 15 | for (int i = 0; i < moves.size(); i++) { 16 | int r = moves[i][0], c = moves[i][1]; 17 | vector& player = (i % 2 == 0) ? A : B; 18 | player[r]++; 19 | player[c + 3]++; 20 | if (r == c) player[6]++; 21 | if (r == 2 - c) player[7]++; 22 | } 23 | for (int i = 0; i < 8; i++) { 24 | if (A[i] == 3) return "A"; 25 | if (B[i] == 3) return "B"; 26 | } 27 | return moves.size() == 9 ? "Draw" : "Pending"; 28 | } 29 | }; 30 | int main() { 31 | system("pause"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /leetcode/128.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | //hash table 10 | class Solution 11 | { 12 | public: 13 | int longestConsecutive(vector &nums) 14 | { 15 | unordered_set records(nums.begin(), nums.end()); 16 | int res = 0; 17 | for (auto num : nums) 18 | { 19 | if (!records.count(num)) 20 | continue; 21 | records.erase(num); 22 | auto pre = num - 1, next = num + 1; 23 | while (records.count(pre)) 24 | records.erase(pre--); 25 | while (records.count(next)) 26 | records.erase(next++); 27 | res = max(res, next - pre - 1); 28 | } 29 | return res; 30 | } 31 | }; 32 | 33 | int main() 34 | { 35 | 36 | string begin = "hit"; 37 | string end = "cog"; 38 | vector list = {"hot", "dot", "dog", "lot", "log", "cog"}; 39 | Solution solution; 40 | solution.ladderLength(begin, end, list); 41 | system("pause"); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /leetcode/129.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct TreeNode 8 | { 9 | int val; 10 | TreeNode *left; 11 | TreeNode *right; 12 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 13 | }; 14 | 15 | class Solution 16 | { 17 | private: 18 | void visit(TreeNode *node, int &sum, int cur) 19 | { 20 | if (node == nullptr) 21 | return; 22 | cur = cur * 10 + node->val; 23 | if (node->left == nullptr && node->right == nullptr) 24 | sum += cur; 25 | visit(node->left, sum, cur); 26 | visit(node->right, sum, cur); 27 | } 28 | 29 | public: 30 | int sumNumbers(TreeNode *root) 31 | { 32 | int sum = 0; 33 | visit(root, sum, 0); 34 | return sum; 35 | } 36 | }; 37 | int main() 38 | { 39 | system("pause"); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /leetcode/1297.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: yangpan 3 | * @Date: 2020-01-31 15:43:15 4 | * @LastEditors : yangpan 5 | * @LastEditTime : 2020-01-31 16:05:04 6 | * @Description: file content 7 | * @FilePath: \leetcode\1297.cpp 8 | */ 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | using namespace std; 18 | 19 | class Solution { 20 | public: 21 | int maxFreq(string s, int maxLetters, int minSize, int maxSize) { 22 | int n = s.length(), cnt = 0; 23 | if (n < minSize) { 24 | return 0; 25 | } 26 | unordered_map counts; 27 | vector chars(26, 0); 28 | for (int i = 0; i < minSize; ++i) { 29 | int tmp = s[i] - 'a'; 30 | chars[tmp]++; 31 | if (chars[tmp] == 1) cnt++; 32 | } 33 | if (cnt <= maxLetters) { 34 | counts[s.substr(0, minSize)]++; 35 | } 36 | for (int i = minSize; i < n; ++i) { 37 | int tmp = s[i] - 'a'; 38 | chars[tmp]++; 39 | if (chars[tmp] == 1) cnt++; 40 | tmp = s[i - minSize] - 'a'; 41 | chars[tmp]--; 42 | if (chars[tmp] == 0) cnt--; 43 | if (cnt <= maxLetters) { 44 | counts[s.substr(i - minSize + 1, minSize)]++; 45 | } 46 | } 47 | int res = 0; 48 | for (auto& it : counts) { 49 | res = max(res, it.second); 50 | } 51 | return res; 52 | } 53 | }; 54 | int main() { 55 | system("pause"); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /leetcode/131.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | 8 | class Solution 9 | { 10 | private: onePartition(string s){ 11 | 12 | } 13 | public: 14 | vector> partition(string s) 15 | { 16 | vector partitionIndexs(s.size() - 1,0); 17 | int n = s.size(); 18 | } 19 | }; 20 | int main() 21 | { 22 | system("pause"); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /leetcode/1319.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: yangpan 3 | * @Date: 2020-01-31 16:05:49 4 | * @LastEditors : yangpan 5 | * @LastEditTime : 2020-01-31 20:22:30 6 | * @Description: file content 7 | * @FilePath: \leetcode\1319.cpp 8 | */ 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | using namespace std; 19 | 20 | class UnionFind { 21 | private: 22 | std::vector _parents; 23 | int _len; 24 | 25 | public: 26 | UnionFind(int n) { 27 | _len = n; 28 | _parents.resize(n); 29 | for (int i = 0; i < n; ++i) { 30 | _parents[i] = i; 31 | } 32 | } 33 | int find(int u) { 34 | if (_parents[u] != u) { 35 | _parents[u] = find(_parents[u]); 36 | } 37 | return _parents[u]; 38 | } 39 | void unin(int u, int v) { 40 | int fau = find(u); 41 | int fav = find(v); 42 | _parents[fav] = fau; 43 | } 44 | 45 | int get_connected_component_cnt() { 46 | int cnt = -1; 47 | for (int i = 0; i < _len; ++i) { 48 | if (i == _parents[i]) cnt++; 49 | } 50 | return cnt; 51 | } 52 | }; 53 | 54 | class Solution { 55 | public: 56 | int makeConnected(int n, vector>& connections) { 57 | if (connections.size() < n - 1) { 58 | return -1; 59 | } 60 | auto uf = UnionFind(n); 61 | for (auto& connection : connections) { 62 | uf.unin(connection[0], connection[1]); 63 | } 64 | return uf.get_connected_component_cnt(); 65 | } 66 | }; 67 | int main() { 68 | system("pause"); 69 | return 0; 70 | } -------------------------------------------------------------------------------- /leetcode/132.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int minCut(string s) 12 | { 13 | int n = s.size(); 14 | vector dp(n + 1, INT_MAX); 15 | dp[0] = -1; 16 | for (int i = 0; i < n; i++) 17 | { 18 | for (int len = 0; i - len >= 0 && i + len < n && s[i - len] == s[i + len]; len++) 19 | dp[i + len + 1] = min(dp[i + len + 1], dp[i - len] + 1); 20 | for (int len = 0; i - len >= 0 && i + len + 1 < n && s[i - len] == s[i + len + 1]; len++) 21 | dp[i + len + 2] = min(dp[i + len + 2], dp[i - len] + 1); 22 | } 23 | return dp[n]; 24 | } 25 | }; 26 | 27 | int main() 28 | { 29 | system("pause"); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /leetcode/1332.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | class Solution { 10 | public: 11 | int removePalindromeSub(string s) { 12 | if (s.length() < 2) { 13 | return s.length(); 14 | } 15 | return s == string(s.rbegin(), s.rend()) ? 1 : 2; 16 | } 17 | }; 18 | 19 | int main() { 20 | system("pause"); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /leetcode/1333.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: yangpan 3 | * @Date: 2020-01-27 10:16:29 4 | * @LastEditors: yangpan 5 | * @LastEditTime: 2020-01-31 16:29:50 6 | * @Description: file content 7 | * @FilePath: \leetcode\1333.cpp 8 | */ 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | bool mycompare(const vector &a, const vector &b) { 18 | if (a[1] != b[1]) { 19 | return a[1] > b[1]; 20 | } else { 21 | return a[0] > b[0]; 22 | } 23 | } 24 | 25 | class Solution { 26 | public: 27 | vector filterRestaurants(vector> &restaurants, 28 | int veganFriendly, int maxPrice, 29 | int maxDistance) { 30 | vector> temp; 31 | for (auto restaurant : restaurants) { 32 | if (veganFriendly == 0 || veganFriendly == restaurant[2]) { 33 | if (restaurant[3] <= maxPrice && restaurant[4] <= maxDistance) { 34 | temp.push_back(restaurant); 35 | } 36 | } 37 | } 38 | sort(temp.begin(), temp.end(), mycompare); 39 | vector res; 40 | for (auto item : temp) { 41 | res.push_back(item[0]); 42 | } 43 | return res; 44 | } 45 | }; 46 | 47 | int main() { 48 | system("pause"); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /leetcode/1334.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define INF 0x3f3f3f3f 11 | #define INF64 0x3f3f3f3f3f3f3f3f 12 | 13 | class Solution { 14 | public: 15 | int findTheCity(int n, vector>& edges, int distanceThreshold) { 16 | int ret = 0; 17 | vector> distance(n, vector(n, INF)); 18 | for (auto& edge : edges) { 19 | distance[edge[0]][edge[1]] = distance[edge[1]][edge[0]] = edge[2]; 20 | } 21 | for (int k = 0; k < n; ++k) { 22 | for (int i = 0; i < n; ++i) { 23 | for (int j = 0; j < n; ++j) { 24 | distance[i][j] = 25 | min(distance[i][j], distance[i][k] + distance[k][j]); 26 | } 27 | } 28 | } 29 | int num = INT_MAX; 30 | for (int i = 0; i < n; ++i) { 31 | int cur = 0; 32 | for (int j = 0; j < n; ++j) { 33 | if (i != j && distance[i][j] <= distanceThreshold) { 34 | cur++; 35 | } 36 | } 37 | if (cur < num) { 38 | num = cur; 39 | ret = i; 40 | } 41 | } 42 | return ret; 43 | } 44 | }; 45 | 46 | int main() { 47 | system("pause"); 48 | return 0; 49 | } -------------------------------------------------------------------------------- /leetcode/1335.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define INF 0x3f3f3f3f 11 | #define INF64 0x3f3f3f3f3f3f3f3f 12 | 13 | class Solution { 14 | public: 15 | int minDifficulty(vector& jobDifficulty, int d) { 16 | int n = jobDifficulty.size(); 17 | if (n < d) { 18 | return -1; 19 | } 20 | vector> dp(d, vector(n, INT_MAX)); 21 | int curMax = -1; 22 | for (int i = 0; i < n; ++i) { 23 | curMax = max(jobDifficulty[i], curMax); 24 | dp[0][i] = curMax; 25 | } 26 | for (int i = 1; i < d; ++i) { 27 | for (int j = i; j < n; ++j) { 28 | int localMax = jobDifficulty[j]; 29 | for (int k = j; k >= i; --k) { 30 | localMax = max(localMax, jobDifficulty[k]); 31 | dp[i][j] = min(dp[i][j], dp[i - 1][k - 1] + localMax); 32 | } 33 | } 34 | } 35 | return dp[d - 1][n - 1]; 36 | } 37 | }; 38 | 39 | int main() { 40 | system("pause"); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /leetcode/1339.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: yangpan 3 | * @Date: 2020-02-02 11:25:30 4 | * @LastEditors : yangpan 5 | * @LastEditTime : 2020-02-04 11:21:31 6 | * @Description: file content 7 | * @FilePath: \leetcode\1339.cpp 8 | */ 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | struct TreeNode { 19 | int val; 20 | TreeNode* left; 21 | TreeNode* right; 22 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 23 | }; 24 | 25 | typedef long long ll; 26 | 27 | const ll m = 1e9 + 7; 28 | class Solution { 29 | private: 30 | ll _total_sum = 0, result = 0; 31 | int count(TreeNode* node) { 32 | int sum = 0; 33 | if (nullptr == node) return 0; 34 | sum = node->val + count(node->left) + count(node->right); 35 | result = max(result, sum * (_total_sum - sum)); 36 | return sum; 37 | } 38 | 39 | public: 40 | int maxProduct(TreeNode* root) { 41 | _total_sum = count(root); 42 | count(root); 43 | return result % m; 44 | } 45 | }; 46 | 47 | int main() { 48 | vector inputs = {6, 4, 14, 6, 8, 13, 9, 7, 10, 6, 12}; 49 | Solution s; 50 | s.maxJumps(inputs, 2); 51 | system("pause"); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /leetcode/134.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int canCompleteCircuit(vector &gas, vector &cost) 10 | { 11 | int sum = 0; 12 | int index = 0; 13 | int before = 0; 14 | for (int i = 0; i < gas.size(); ++i) 15 | { 16 | sum += gas[i] - cost[i]; 17 | if (sum < 0) 18 | { 19 | before += sum; 20 | sum = 0; 21 | index = i + 1; 22 | } 23 | } 24 | if (before + sum < 0) 25 | return -1; 26 | return index; 27 | } 28 | }; 29 | 30 | int main() 31 | { 32 | Solution solution; 33 | vector gas = {1, 2, 3, 4, 5}; 34 | vector cost = {3, 4, 5, 1, 2}; 35 | solution.canCompleteCircuit(gas, cost); 36 | system("pause"); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /leetcode/1351.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: yangpan 3 | * @Date: 2020-02-17 12:53:56 4 | * @LastEditors : yangpan 5 | * @LastEditTime : 2020-02-17 12:54:12 6 | * @Description: file content 7 | * @FilePath: \leetcode\1351.cpp 8 | */ 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | #define INF 0x3f3f3f3f 19 | #define INF64 0x3f3f3f3f3f3f3f3f 20 | 21 | class Solution { 22 | public: 23 | int countNegatives(vector>& grid) { 24 | int cnt=0,m=grid.size(),n=grid[0].size(); 25 | for(int i=0;i 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | using namespace std; 18 | 19 | #define INF 0x3f3f3f3f 20 | #define INF64 0x3f3f3f3f3f3f3f3f 21 | 22 | class Solution { 23 | public: 24 | int maxEvents(vector>& events) { 25 | sort(events.begin(),events.end()); 26 | priority_queue,greater> q; 27 | int i=0,n=events.size(),res=0; 28 | for(int d=1;d<=100000;++d){ 29 | while(i 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | #define INF 0x3f3f3f3f 19 | #define INF64 0x3f3f3f3f3f3f3f3f 20 | 21 | class Solution { 22 | void get_max(const vector& vec, long long& m, long long& sum, 23 | long long& pos) { 24 | sum = 0; 25 | m = 0; 26 | for (int i = 0; i < vec.size(); ++i) { 27 | sum += vec[i]; 28 | if (vec[i] > m) { 29 | m = vec[i]; 30 | pos = i; 31 | } 32 | } 33 | } 34 | 35 | public: 36 | bool isPossible(vector& target) { 37 | vector copy(target); 38 | long long count_of_one = 0, m = 0, sum = 0, pos = 0; 39 | for (auto item : copy) { 40 | count_of_one += item == 1 ? 1 : 0; 41 | } 42 | while (count_of_one < copy.size()) { 43 | get_max(copy, m, sum, pos); 44 | long long other = sum - m; 45 | long long sub = m - other; 46 | if (sub == 1) { 47 | count_of_one++; 48 | copy[pos] = sub; 49 | } else if (sub <= 0) { 50 | return false; 51 | } else { 52 | copy[pos] = sub; 53 | } 54 | } 55 | return true; 56 | } 57 | }; 58 | 59 | int main() { 60 | system("pause"); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /leetcode/136.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | class Solution 7 | { 8 | public: 9 | int singleNumber(vector &nums) 10 | { 11 | return accumulate(begin(nums), end(nums), 0, std::bit_xor()); 12 | } 13 | }; 14 | int main() 15 | { 16 | system("pause"); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/138-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Definition for a Node. 8 | class Node 9 | { 10 | public: 11 | int val; 12 | Node *next; 13 | Node *random; 14 | 15 | Node() {} 16 | 17 | Node(int _val, Node *_next, Node *_random) 18 | { 19 | val = _val; 20 | next = _next; 21 | random = _random; 22 | } 23 | }; 24 | 25 | class Solution 26 | { 27 | public: 28 | Node *copyRandomList(Node *head) 29 | { 30 | if (head == nullptr) 31 | return nullptr; 32 | 33 | unordered_map hashTable; 34 | Node *node = head; 35 | while (node != nullptr) 36 | { 37 | hashTable[node]=new Node(node->val, nullptr, nullptr); 38 | node = node->next; 39 | } 40 | node = head; 41 | while (node != nullptr) 42 | { 43 | hashTable[node]->next = hashTable[node->next]; 44 | if(node->random!=nullptr) 45 | hashTable[node]->random = hashTable[node->random]; 46 | node = node->next; 47 | } 48 | return hashTable[head]; 49 | } 50 | }; 51 | 52 | int main() 53 | { 54 | system("pause"); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /leetcode/139.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Solution 11 | { 12 | public: 13 | bool wordBreak(string s, vector &wordDict) 14 | { 15 | unordered_set dicts; 16 | int minLength = INT_MAX, maxLength = 0; 17 | for (auto item : wordDict) 18 | { 19 | dicts.insert(item); 20 | minLength = min(minLength, (int)item.length()); 21 | maxLength = max(maxLength, (int)item.length()); 22 | } 23 | vector dp(s.size() + 1, false); 24 | dp[0] = true; 25 | for (int i = minLength; i <= s.size(); ++i) 26 | { 27 | for (int len = minLength; len <= min(maxLength, i); ++len) 28 | { 29 | if (dp[i - len] && dicts.count(s.substr(i - len, len))) 30 | dp[i] = true; 31 | } 32 | } 33 | return dp[s.size()]; 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | Solution solution; 40 | vector inputs = {"apple", "pen"}; 41 | solution.wordBreak("applepenapple", inputs); 42 | system("pause"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /leetcode/14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | string longestCommonPrefix(vector& strs) { 9 | int n = strs.size(); 10 | string ret = ""; 11 | if(n==0) 12 | return ret; 13 | for (int i = 0; i < strs[0].size();++i){ 14 | for (int j = 1; j < n;++j){ 15 | if(i>=strs[j].size()||strs[0][i]!=strs[j][i]) 16 | return ret; 17 | } 18 | ret += strs[0][i]; 19 | } 20 | return ret; 21 | } 22 | }; 23 | 24 | int main(){ 25 | system("pause"); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/141.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct ListNode 8 | { 9 | int val; 10 | ListNode *next; 11 | ListNode(int x) : val(x), next(NULL) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | bool hasCycle(ListNode *head) 18 | { 19 | if (head == nullptr) 20 | return false; 21 | auto slow = head; 22 | auto fast = head; 23 | while (fast != nullptr && fast->next != nullptr) 24 | { 25 | slow = slow->next; 26 | fast = fast->next->next; 27 | if (slow == fast) 28 | return true; 29 | } 30 | return false; 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | system("pause"); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /leetcode/142.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | struct ListNode 8 | { 9 | int val; 10 | ListNode *next; 11 | ListNode(int x) : val(x), next(NULL) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | ListNode *detectCycle(ListNode *head) 18 | { 19 | if (head == nullptr || head->next == nullptr) 20 | return nullptr; 21 | auto slow = head; 22 | auto fast = head; 23 | while (fast != nullptr && fast->next != nullptr) 24 | { 25 | slow = slow->next; 26 | fast = fast->next->next; 27 | if (slow == fast) 28 | break; 29 | } 30 | if (slow != fast) 31 | return nullptr; 32 | fast = head; 33 | while (fast != slow) 34 | { 35 | slow = slow->next; 36 | fast = fast->next; 37 | } 38 | return fast; 39 | } 40 | }; 41 | 42 | int main() 43 | { 44 | system("pause"); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /leetcode/148-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | //Definition for singly-linked list. 7 | struct ListNode { 8 | int val; 9 | ListNode *next; 10 | ListNode(int x) : val(x), next(NULL) {} 11 | }; 12 | 13 | class Solution { 14 | private: 15 | ListNode* partition(ListNode* key,ListNode* end){ 16 | auto p1=key; 17 | auto p2 = key->next; 18 | while(p2!=end){ 19 | if(p2->valval){ 20 | p1 = p1->next; 21 | auto temp = p1->val; 22 | p1->val = p2->val; 23 | p2->val = temp; 24 | } 25 | p2 = p2->next; 26 | } 27 | if(p1!=key){ 28 | auto temp=key->val; 29 | key->val=p1->val; 30 | p1->val = temp; 31 | } 32 | return p1; 33 | } 34 | void quickSort(ListNode* head,ListNode* end){ 35 | if(head!=end){ 36 | auto node = partition(head, end); 37 | quickSort(head, node); 38 | quickSort(node->next, end); 39 | } 40 | } 41 | 42 | public: 43 | ListNode* sortList(ListNode* head) { 44 | quickSort(head, nullptr); 45 | return head; 46 | } 47 | }; 48 | 49 | int main(){ 50 | system("pause"); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /leetcode/149-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int maxPoints(vector> &points) 12 | { 13 | int n = points.size(); 14 | if (n < 3) 15 | return n; 16 | auto pointOne = points[0]; 17 | } 18 | }; 19 | 20 | int main() 21 | { 22 | system("pause"); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /leetcode/15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | vector> threeSum(vector &nums) 12 | { 13 | vector> res; 14 | if (nums.size() < 3) 15 | return res; 16 | sort(nums.begin(), nums.end()); 17 | if (nums.front() > 0 || nums.back() < 0) 18 | return res; 19 | for (int i = 0; i < nums.size() - 2; ++i) 20 | { 21 | if (nums[i] > 0) 22 | break; 23 | auto target = 0 - nums[i]; 24 | int front = i + 1, back = nums.size() - 1; 25 | while (front < back) 26 | { 27 | auto sum = nums[front] + nums[back]; 28 | if (sum > target) 29 | back--; 30 | else if (sum < target) 31 | front++; 32 | else 33 | { 34 | res.emplace_back(vector{nums[i], nums[front++], nums[back--]}); 35 | while (front < back && nums[front - 1] == nums[front]) 36 | front++; 37 | while (front < back && nums[back + 1] == nums[back]) 38 | back--; 39 | } 40 | } 41 | while (i < nums.size() - 2 && nums[i] == nums[i + 1]) 42 | i++; 43 | } 44 | return res; 45 | } 46 | }; 47 | 48 | int main() 49 | { 50 | system("pause"); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /leetcode/152.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int maxProduct(vector &nums) 11 | { 12 | if (nums.empty()) 13 | return 0; 14 | if (nums.size() == 1) 15 | return nums[0]; 16 | long minPre = nums[0], maxPre = nums[0], res = nums[0]; 17 | for (int i = 1; i < nums.size(); ++i) 18 | { 19 | int temp1 = minPre * nums[i]; 20 | int temp2 = maxPre * nums[i]; 21 | maxPre = max(max(temp1,temp2), nums[i]); 22 | minPre = min(min(temp1,temp2), nums[i]); 23 | res = max(res, maxPre); 24 | } 25 | return res; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | vector input = {-4, -3, -2}; 32 | Solution s; 33 | s.maxProduct(input); 34 | system("pause"); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /leetcode/155.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int val; 9 | Node *next; 10 | Node *min; 11 | 12 | Node(int _val) 13 | { 14 | val = _val; 15 | next = nullptr; 16 | min = nullptr; 17 | } 18 | }; 19 | 20 | class MinStack 21 | { 22 | private: 23 | Node *head; 24 | Node *min; 25 | int stackSize; 26 | 27 | public: 28 | /** initialize your data structure here. */ 29 | MinStack() 30 | { 31 | head = nullptr; 32 | min = nullptr; 33 | stackSize = 0; 34 | } 35 | 36 | void push(int x) 37 | { 38 | Node *node = new Node(x); 39 | node->next = head; 40 | head = node; 41 | stackSize++; 42 | if(min==nullptr||min->val>head->val){ 43 | head->min = min; 44 | min = head; 45 | } 46 | } 47 | void pop() 48 | { 49 | Node *temp = head; 50 | head = head->next; 51 | if(temp==min){ 52 | min = min->min; 53 | } 54 | delete(temp); 55 | stackSize--; 56 | } 57 | 58 | int top() 59 | { 60 | return head->val; 61 | } 62 | 63 | int getMin() 64 | { 65 | return min->val; 66 | } 67 | }; 68 | 69 | int main(){ 70 | MinStack minStacl; 71 | return 0; 72 | } -------------------------------------------------------------------------------- /leetcode/160-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct ListNode 7 | { 8 | int val; 9 | ListNode *next; 10 | ListNode(int x) : val(x), next(NULL) {} 11 | }; 12 | 13 | class Solution 14 | { 15 | public: 16 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 17 | { 18 | if (headA == nullptr || headB == nullptr) 19 | return nullptr; 20 | int m = 0; 21 | int n = 0; 22 | auto nodeA = headA; 23 | while (nodeA != nullptr) 24 | { 25 | m++; 26 | nodeA = nodeA->next; 27 | } 28 | auto nodeB = headB; 29 | while (nodeB != nullptr) 30 | { 31 | n++; 32 | nodeB = nodeB->next; 33 | } 34 | 35 | nodeA = headA; 36 | nodeB = headB; 37 | if (m < n) 38 | { 39 | int d = n - m; 40 | while (d--) 41 | nodeB = nodeB->next; 42 | } 43 | else 44 | { 45 | int d = m - n; 46 | while (d--) 47 | nodeA = nodeA->next; 48 | } 49 | while ((nodeA != nullptr && nodeB != nullptr) && nodeA != nodeB) 50 | { 51 | nodeA = nodeA->next; 52 | nodeB = nodeB->next; 53 | } 54 | return (nodeA == nullptr || nodeB == nullptr) ? nullptr : nodeA; 55 | } 56 | }; 57 | 58 | int main() 59 | { 60 | system("pause"); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /leetcode/160-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct ListNode 7 | { 8 | int val; 9 | ListNode *next; 10 | ListNode(int x) : val(x), next(NULL) {} 11 | }; 12 | 13 | class Solution 14 | { 15 | public: 16 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 17 | { 18 | auto nodeA = headA; 19 | auto nodeB = headB; 20 | while (nodeA != nodeB) 21 | { 22 | nodeA = nodeA == nullptr ? headB : nodeA->next; 23 | nodeB = nodeB == nullptr ? headA : nodeB->next; 24 | } 25 | return nodeA; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | system("pause"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /leetcode/169.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int majorityElement(vector &nums) 11 | { 12 | unordered_map counts; 13 | int n = nums.size(); 14 | for(auto num:nums){ 15 | if(++counts[num]>n/2){ 16 | return num; 17 | } 18 | } 19 | return 0; 20 | } 21 | }; 22 | 23 | int main() 24 | { 25 | system("pause"); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Solution 10 | { 11 | private: 12 | static unordered_map mapping; 13 | void visit(string &digits, int pos, string cur, vector &res) 14 | { 15 | if (pos == digits.size()) 16 | { 17 | res.emplace_back(cur); 18 | return; 19 | } 20 | if (digits[pos] - '9' > 0 || digits[pos] - '2' < 0) 21 | return; 22 | for (auto item : mapping[digits[pos]]) 23 | { 24 | visit(digits, pos + 1, cur + item, res); 25 | } 26 | } 27 | 28 | public: 29 | vector letterCombinations(string digits) 30 | { 31 | vector res; 32 | if (digits.empty()) 33 | return res; 34 | visit(digits, 0, "", res); 35 | return res; 36 | } 37 | }; 38 | unordered_map Solution::mapping = {{'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7',"pqrs"}, {'8', "tuv"}, {'9',"wxyz"}}; 39 | 40 | int main() 41 | { 42 | system("pause"); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /leetcode/171.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int titleToNumber(string s) 11 | { 12 | if (s.empty()) 13 | return 0; 14 | int i = 0, j = s.size() - 1; 15 | int res = 0; 16 | while (j >= 0) 17 | res += pow(26, i++) * (s[j--] - 'A'+1); 18 | return res; 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | system("pause"); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/172.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int trailingZeroes(int n) 9 | { 10 | int result = 0; 11 | for (long i = 5; i <= n; i *= 5) 12 | { 13 | result += (n / i); 14 | } 15 | return result; 16 | } 17 | }; 18 | 19 | int main() 20 | { 21 | system("pause"); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/174.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int calculateMinimumHP(vector> &dungeon) 12 | { 13 | int m = dungeon.size(), n = dungeon[0].size(); 14 | vector> dp(m + 1, vector(n + 1, INT_MAX)); 15 | dp[m][n - 1] = 1; 16 | dp[m - 1][n] = 1; 17 | for (int i = m - 1; i >= 0; i--) 18 | { 19 | for (int j = n - 1; j >= 0; j--) 20 | { 21 | int need = min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]; 22 | dp[i][j] = need <= 0 ? 1 : need; 23 | } 24 | } 25 | return dp[0][0]; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | system("pause"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /leetcode/179.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Solution 13 | { 14 | public: 15 | string largestNumber(vector &nums) 16 | { 17 | string res = ""; 18 | if (nums.empty()) 19 | return res; 20 | vector strs(nums.size()); 21 | for (int i = 0; i < nums.size(); ++i) 22 | { 23 | strs[i] = to_string(nums[i]); 24 | } 25 | sort(strs.begin(), strs.end(), [](const string &a, const string &b) { return a + b > b + a; }); 26 | for (auto str : strs) 27 | { 28 | res += str; 29 | } 30 | return res[0]=='0'?"0":res; 31 | } 32 | }; 33 | 34 | class Solution2 35 | { 36 | public: 37 | string largestNumber(vector &nums) 38 | { 39 | if (all_of(nums.begin(), nums.end(),[](int num) { return num == 0; })) 40 | return "0"; 41 | vector strs; 42 | transform(nums.begin(), nums.end(), back_inserter(strs), [](int num) { return to_string(num); }); 43 | sort(strs.begin(), strs.end(), [](const string &a, const string &b) { return a + b > b + a; }); 44 | return accumulate(strs.begin(), strs.end(), string("")); 45 | } 46 | }; 47 | 48 | int main() 49 | { 50 | system("pause"); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /leetcode/18.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | vector> fourSum(vector &nums, int target) 10 | { 11 | } 12 | }; 13 | 14 | int main() 15 | { 16 | system("pause"); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /leetcode/189-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | void rotate(vector &nums, int k) 10 | { 11 | if (nums.empty() || k % nums.size() == 0) 12 | return; 13 | int n = nums.size(); 14 | vector vec(nums); 15 | for (int i = 0; i < n; ++i) 16 | { 17 | nums[(i + k) % n] = vec[i]; 18 | } 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | system("pause"); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /leetcode/189-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | void rotate(vector &nums, int k) 11 | { 12 | if (nums.empty() || k % nums.size() == 0) 13 | return; 14 | int n = nums.size(); 15 | k = k % n; 16 | reverse(nums.begin(), nums.begin() + n - k); 17 | reverse(nums.begin() + n - k, nums.end()); 18 | reverse(nums.begin(), nums.end()); 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | system("pause"); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /leetcode/189-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | void rotate(vector &nums, int k) 10 | { 11 | if (nums.empty() || k % nums.size() == 0) 12 | return; 13 | int n = nums.size(); 14 | k = k % n; 15 | int start = 0; 16 | while (n && (k %= n)) 17 | { 18 | for (int i = 0; i < k; ++i) 19 | { 20 | swap(nums[i + start], nums[n - k + i + start]); 21 | } 22 | start += k; 23 | n -= k; 24 | } 25 | } 26 | }; 27 | 28 | int main() 29 | { 30 | system("pause"); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /leetcode/19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Solution 6 | { 7 | public: 8 | ListNode *removeNthFromEnd(ListNode *head, int n) 9 | { 10 | ListNode *newHead = new ListNode(-1); 11 | newHead->next = head; 12 | auto slow = newHead, fast = head; 13 | while (n--) 14 | fast = fast->next; 15 | while (fast != nullptr) 16 | { 17 | slow = slow->next; 18 | fast = fast->next; 19 | } 20 | slow->next = slow->next->next; 21 | return newHead->next; 22 | } 23 | }; 24 | int main() 25 | { 26 | system("pause"); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /leetcode/190.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | uint32_t reverseBits(uint32_t n) 10 | { 11 | n = (n >> 16) | (n << 16); 12 | n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8); 13 | n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4); 14 | n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2); 15 | n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1); 16 | return n; 17 | } 18 | }; 19 | 20 | int main() 21 | { 22 | system("pause"); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /leetcode/191.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int hammingWeight(uint32_t n) 10 | { 11 | int count = 0; 12 | for (int i = 0; i < 32; ++i) 13 | { 14 | count += (n & 1); 15 | n = n >> 1; 16 | } 17 | return count; 18 | } 19 | }; 20 | 21 | class Solution2 22 | { 23 | public: 24 | int hammingWeight(uint32_t n) 25 | { 26 | int count = 0; 27 | while (n) 28 | { 29 | n = n & (n - 1); 30 | count++; 31 | } 32 | return count; 33 | } 34 | }; 35 | int main() 36 | { 37 | 38 | system("pause"); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /leetcode/198.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int rob(vector &nums) 10 | { 11 | if (nums.empty()) 12 | return 0; 13 | if (nums.size() == 1) 14 | return nums[0]; 15 | if (nums.size() == 2) 16 | return max(nums[0], nums[1]); 17 | vector dp(nums.size(), 0); 18 | dp[0] = nums[0]; 19 | dp[1] = max(nums[0], nums[1]); 20 | for (int i = 2; i < nums.size(); ++i) 21 | { 22 | dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]); 23 | } 24 | return dp.back(); 25 | } 26 | }; 27 | 28 | int main() 29 | { 30 | system("pause"); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /leetcode/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct ListNode 6 | { 7 | int val; 8 | ListNode *next; 9 | ListNode(int x) : val(x), next(NULL) {} 10 | }; 11 | class Solution 12 | { 13 | public: 14 | ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) 15 | { 16 | auto head = new ListNode(0); 17 | auto node = head; 18 | auto add = 0; 19 | auto val = 0; 20 | while (l1 != nullptr || l2 != nullptr) 21 | { 22 | val = (l1 != nullptr ? l1->val : 0) + (l2 != nullptr ? l2->val : 0) + add; 23 | add = val / 10; 24 | node->val = val%10; 25 | l1 = l1 != nullptr ? l1->next : nullptr; 26 | l2 = l2 != nullptr ? l2->next : nullptr; 27 | if (l1 != nullptr || l2 != nullptr){ 28 | node->next = new ListNode(0); 29 | node = node->next; 30 | } 31 | } 32 | //Boundary conditions, such as l1 is [5] and l2 is [5]. 33 | if(add>0){ 34 | node->next = new ListNode(1); 35 | node = node->next; 36 | } 37 | return head; 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | auto node1 = new ListNode(2); 44 | auto node2 = new ListNode(4); 45 | auto node3 = new ListNode(3); 46 | node1->next = node2; 47 | node2->next = node3; 48 | auto node4 = new ListNode(5); 49 | auto node5 = new ListNode(6); 50 | auto node6 = new ListNode(4); 51 | node4->next = node5; 52 | node5->next = node6; 53 | Solution solution; 54 | auto result = solution.addTwoNumbers(node1, node4); 55 | while (result != nullptr) 56 | { 57 | cout << result->val << endl; 58 | result = result->next; 59 | } 60 | system("pause"); 61 | return 0; 62 | } -------------------------------------------------------------------------------- /leetcode/20.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | class Solution 7 | { 8 | 9 | public: 10 | bool isValid(string s) 11 | { 12 | map brackets; 13 | brackets.insert(pair('(', ')')); 14 | brackets.insert(pair('[', ']')); 15 | brackets.insert(pair('{', '}')); 16 | 17 | stack st; 18 | 19 | for (auto c:s) 20 | { 21 | if ((c == '(') || (c == '[') || (c == '{')) 22 | st.push(c); 23 | else 24 | { 25 | if (st.size() == 0) 26 | return false; 27 | else 28 | { 29 | if (c == brackets[st.top()]) 30 | st.pop(); 31 | else 32 | return false; 33 | } 34 | } 35 | } 36 | 37 | if (st.size() == 0) 38 | return true; 39 | else 40 | return false; 41 | } 42 | }; 43 | 44 | int main() 45 | { 46 | Solution solution; 47 | solution.isValid("()"); 48 | system("pause"); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /leetcode/204.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int countPrimes(int n) 12 | { 13 | vector marked(n, true); 14 | int cnt = 0; 15 | for (int i = 2; i < n; ++i) 16 | { 17 | if (marked[i]) 18 | { 19 | cnt++; 20 | for (int j = i + i; j < n; j += i) 21 | marked[j] = false; 22 | } 23 | } 24 | return cnt; 25 | } 26 | }; 27 | 28 | int main() 29 | { 30 | system("pause"); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /leetcode/206.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | //Definition for singly-linked list. 6 | struct ListNode 7 | { 8 | int val; 9 | ListNode *next; 10 | ListNode(int x) : val(x), next(NULL) {} 11 | }; 12 | 13 | class Solution 14 | { 15 | public: 16 | ListNode *reverseList(ListNode *head) 17 | { 18 | ListNode *result = nullptr; 19 | if (head != nullptr) 20 | { 21 | result = new ListNode(head->val); 22 | head = head->next; 23 | 24 | while (head != nullptr) 25 | { 26 | auto node = new ListNode(head->val); 27 | node->next = result; 28 | result = node; 29 | head = head->next; 30 | } 31 | 32 | return result; 33 | } 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | system("pause"); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /leetcode/21.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct ListNode 7 | { 8 | int val; 9 | ListNode *next; 10 | ListNode(int x) : val(x), next(NULL) {} 11 | }; 12 | 13 | class Solution 14 | { 15 | public: 16 | ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) 17 | { 18 | if (l1 == nullptr) 19 | return l2; 20 | if (l2 == nullptr) 21 | return l1; 22 | ListNode *root = nullptr; 23 | if (l1->val < l2->val) 24 | { 25 | root = l1; 26 | l1 = l1->next; 27 | } 28 | else 29 | { 30 | root = l2; 31 | l2 = l2->next; 32 | } 33 | ListNode *node = root; 34 | while (l1 != nullptr && l2 != nullptr) 35 | { 36 | if (l1->val < l2->val) 37 | { 38 | node->next = l1; 39 | l1 = l1->next; 40 | node = node->next; 41 | } 42 | else 43 | { 44 | node->next = l2; 45 | l2 = l2->next; 46 | node = node->next; 47 | } 48 | } 49 | if (l1 != nullptr) 50 | node->next = l1; 51 | else 52 | node->next = l2; 53 | return root; 54 | } 55 | }; 56 | 57 | int main() 58 | { 59 | system("pause"); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /leetcode/210.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Solution 12 | { 13 | 14 | public: 15 | vector findOrder(int numCourses, vector> &prerequisites) 16 | { 17 | vector> graph(numCourses); 18 | vector inDegree(numCourses, 0); 19 | for (auto item : prerequisites) 20 | { 21 | graph[item[1]].emplace_back(item[0]); 22 | inDegree[item[0]]++; 23 | } 24 | 25 | stack s; 26 | for (int i = 0; i < numCourses; ++i) 27 | { 28 | if (inDegree[i] == 0) 29 | s.push(i); 30 | } 31 | 32 | int visited = 0; 33 | vector order; 34 | while (!s.empty()) 35 | { 36 | int cur = s.top(); 37 | s.pop(); 38 | order.push_back(cur); 39 | visited++; 40 | for (auto node : graph[cur]) 41 | { 42 | if (--inDegree[node] == 0) 43 | s.push(node); 44 | } 45 | } 46 | if (visited != numCourses) 47 | { 48 | vector ret; 49 | return ret; 50 | } 51 | return order; 52 | } 53 | 54 | private: 55 | vector> buildGraph(int numCourses, vector> &prerequisites) 56 | { 57 | vector> graph; 58 | for (auto item : prerequisites) 59 | { 60 | graph[item[0]].emplace_back(item[1]); 61 | } 62 | return graph; 63 | } 64 | }; 65 | 66 | int main() 67 | { 68 | system("pause"); 69 | return 0; 70 | } -------------------------------------------------------------------------------- /leetcode/213.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | int robI(vector nums) 11 | { 12 | if (nums.size() == 1) 13 | return nums[0]; 14 | vector dp(nums.size(), 0); 15 | dp[0] = nums[0]; 16 | dp[1] = max(nums[0], nums[1]); 17 | for (int i = 2; i < nums.size(); ++i) 18 | { 19 | dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]); 20 | } 21 | return dp.back(); 22 | } 23 | 24 | public: 25 | int rob(vector &nums) 26 | { 27 | if (nums.empty()) 28 | return 0; 29 | if (nums.size() == 1) 30 | return nums[0]; 31 | vector a = vector(nums.begin() + 1, nums.end()); 32 | vector b = vector(nums.begin(), nums.end() - 1); 33 | return max(robI(a), robI(b)); 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | system("pause"); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /leetcode/215.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Solution 6 | { 7 | private: 8 | int partition(vector &nums, int begin, int end) 9 | { 10 | if (begin >= end) 11 | return 0; 12 | int pivot = nums[begin]; 13 | int i = begin, j = end; 14 | while (i < j) 15 | { 16 | while (nums[j] >= pivot && i < j) 17 | j--; 18 | while (nums[i] <= pivot && i < j) 19 | i++; 20 | if (i == j) 21 | break; 22 | swap(nums[i], nums[j]); 23 | } 24 | swap(nums[i], nums[begin]); 25 | return i; 26 | } 27 | 28 | public: 29 | int findKthLargest(vector &nums, int k) 30 | { 31 | int begin=0; 32 | int end = nums.size()-1; 33 | int index = nums.size() - k ; 34 | while(beginindex) 41 | end = p - 1; 42 | } 43 | return nums[index]; 44 | } 45 | }; 46 | 47 | int main() 48 | { 49 | vector inputs = {3, 2, 1, 5, 6, 4}; 50 | Solution solution; 51 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Solution { 10 | public: 11 | bool containsDuplicate(vector& nums) { 12 | unordered_set visited; 13 | for(auto num:nums){ 14 | if(visited.count(num)) 15 | return true; 16 | visited.insert(num); 17 | } 18 | return false; 19 | } 20 | }; 21 | int main() 22 | { 23 | system("pause"); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /leetcode/22.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | private: 9 | void buildStr(vector &res, string str, int leftParenthesisCount, int rightParenthesisCount) 10 | { 11 | if (leftParenthesisCount > rightParenthesisCount) 12 | return; 13 | if (leftParenthesisCount == 0 && rightParenthesisCount == 0) 14 | { 15 | res.push_back(str); 16 | return; 17 | } 18 | if (leftParenthesisCount > 0) 19 | buildStr(res, str + "(", leftParenthesisCount - 1, rightParenthesisCount); 20 | if (rightParenthesisCount > 0) 21 | buildStr(res, str + ")", leftParenthesisCount, rightParenthesisCount - 1); 22 | } 23 | 24 | public: 25 | vector generateParenthesis(int n) 26 | { 27 | vector res; 28 | if (n <= 0) 29 | return res; 30 | buildStr(res, "", n, n); 31 | return res; 32 | } 33 | }; 34 | int main() 35 | { 36 | system("pause"); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /leetcode/221.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | class Solution 8 | { 9 | public: 10 | int maximalSquare(vector> &matrix) 11 | { 12 | if (matrix.empty() || matrix[0].empty()) 13 | return 0; 14 | int m = matrix.size(), n = matrix[0].size(); 15 | vector> dp(m, vector(n, 0)); 16 | int len = 0; 17 | for (int i = 0; i < m; ++i) 18 | { 19 | if (matrix[i][0] == '1') 20 | { 21 | dp[i][0] = 1; 22 | len = 1; 23 | } 24 | } 25 | for (int i = 0; i < n; ++i) 26 | { 27 | if (matrix[0][i] == '1') 28 | { 29 | dp[0][i] = 1; 30 | len = 1; 31 | } 32 | } 33 | 34 | for (int i = 1; i < m; ++i) 35 | { 36 | for (int j = 1; j < n; ++j) 37 | { 38 | if (matrix[i][j] == '0') 39 | dp[i][j] = 0; 40 | else 41 | { 42 | dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1; 43 | len = max(len, dp[i][j]); 44 | } 45 | } 46 | } 47 | return len * len; 48 | } 49 | }; 50 | 51 | int main() 52 | { 53 | system("pause"); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /leetcode/227.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Solution 6 | { 7 | public: 8 | int calculate(string s) 9 | { 10 | int sum = 0; 11 | if (s.size() < 1) 12 | return sum; 13 | int i = 0; 14 | int last = 0, last_result = 1; 15 | char last_operator = '+'; 16 | int sign = 1; 17 | while (i < s.size()) 18 | { 19 | if (s[i] == ' ') 20 | { 21 | ++i; 22 | continue; 23 | } 24 | if (s[i] == '+' || s[i] == '-') 25 | { 26 | sum += last_result * sign; 27 | sign = s[i++] == '+' ? 1 : -1; 28 | last_result = 1; 29 | last_operator = '+'; 30 | } 31 | else if (s[i] == '/' || s[i] == '*') 32 | { 33 | last_operator = s[i]; 34 | ++i; 35 | } 36 | if (isdigit(s[i])) 37 | { 38 | last = 0; 39 | while (i < s.size() && isdigit(s[i])) 40 | { 41 | last = last * 10 + (s[i++] - '0'); 42 | } 43 | 44 | if (last_operator == '*') 45 | last_result *= last; 46 | else if (last_operator == '/') 47 | last_result /= last; 48 | else 49 | last_result = last; 50 | } 51 | } 52 | sum += last_result * sign; 53 | return sum; 54 | } 55 | }; 56 | int main() 57 | { 58 | 59 | system("pause"); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /leetcode/230.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct TreeNode 7 | { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | private: 17 | int visit(TreeNode *node, int &k) 18 | { 19 | if (node == nullptr) 20 | return 0; 21 | int val = visit(node->left, k); 22 | if (k == 0) 23 | return val; 24 | k--; 25 | if (k == 0) 26 | return node->val; 27 | return visit(node->right, k); 28 | } 29 | 30 | public: 31 | int kthSmallest(TreeNode *root, int k) 32 | { 33 | int val = visit(root, k); 34 | return val; 35 | } 36 | }; 37 | 38 | int main() 39 | { 40 | system("pause"); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /leetcode/234.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct ListNode 7 | { 8 | int val; 9 | ListNode *next; 10 | ListNode(int x) : val(x), next(NULL) {} 11 | }; 12 | 13 | class Solution 14 | { 15 | private: 16 | ListNode *reverse(ListNode *node) 17 | { 18 | ListNode *pre = nullptr; 19 | ListNode *next = nullptr; 20 | ListNode *cur = node; 21 | while (cur != nullptr) 22 | { 23 | next = cur->next; 24 | cur->next = pre; 25 | pre = cur; 26 | cur = next; 27 | } 28 | return pre; 29 | } 30 | 31 | public: 32 | bool isPalindrome(ListNode *head) 33 | { 34 | if (head == nullptr || head->next == nullptr) 35 | return true; 36 | auto slow = head; 37 | auto fast = head->next; 38 | while (fast != nullptr && fast->next != nullptr) 39 | { 40 | fast = fast->next->next; 41 | slow = slow->next; 42 | } 43 | auto node = reverse(slow->next); 44 | while (node != nullptr) 45 | { 46 | if (head->val != node->val) 47 | return false; 48 | head = head->next; 49 | node = node->next; 50 | } 51 | return true; 52 | } 53 | }; 54 | 55 | int main() 56 | { 57 | system("pause"); 58 | return 0; 59 | } -------------------------------------------------------------------------------- /leetcode/236.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct TreeNode 7 | { 8 | int val; 9 | TreeNode *left; 10 | TreeNode *right; 11 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 12 | }; 13 | 14 | class Solution 15 | { 16 | public: 17 | TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) 18 | { 19 | if (root == nullptr||root==p||root==q) 20 | return root; 21 | auto left = lowestCommonAncestor(root->left, p, q); 22 | auto right = lowestCommonAncestor(root->right, p, q); 23 | if(left!=nullptr&&right!=nullptr) 24 | return root; 25 | return left == nullptr ? right : left; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | system("pause"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /leetcode/240.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | bool searchMatrix(vector> &matrix, int target) 10 | { 11 | int m = matrix.size(); 12 | if (m == 0) 13 | return false; 14 | int n = matrix.front().size(); 15 | int i = 0, j = n - 1; 16 | while (i < m && j >= 0) 17 | { 18 | if (matrix[i][j] == target) 19 | return true; 20 | matrix[i][j] > target ? j-- : i++; 21 | } 22 | return false; 23 | } 24 | }; 25 | int main() 26 | { 27 | system("pause"); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /leetcode/26.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Solution 6 | { 7 | public: 8 | int removeDuplicates(vector &nums) 9 | { 10 | if (nums.size() < 2) 11 | return nums.size(); 12 | int pos = 0; 13 | for (int i = 0; i < nums.size() - 1; ++i) 14 | { 15 | if (nums[i] != nums[i + 1]) 16 | nums[pos++] = nums[i]; 17 | } 18 | nums[pos++] = nums[nums.size() - 1]; 19 | return pos; 20 | } 21 | }; 22 | 23 | int main() 24 | { 25 | system("pause"); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/264.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Solution 11 | { 12 | public: 13 | int nthUglyNumber(int n) 14 | { 15 | if(n<=0) 16 | return 0; 17 | vector dp(n, 1); 18 | int t1 = 0, t2 = 0, t3 = 0; 19 | for (int i = 1; i < n; ++i) 20 | { 21 | dp[i] = min(dp[t1] * 2,min(dp[t2] * 3, dp[t3] * 5)); 22 | if (dp[i] == dp[t1] * 2) 23 | t1++; 24 | if (dp[i] == dp[t2] * 3) 25 | t2++; 26 | if (dp[i] == dp[t3] * 5) 27 | t3++; 28 | } 29 | return dp[n - 1]; 30 | } 31 | }; 32 | 33 | int main() 34 | { 35 | system("pause"); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /leetcode/268.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | //in-place 算法 9 | class Solution 10 | { 11 | public: 12 | int missingNumber(vector &nums) 13 | { 14 | int n = nums.size(); 15 | for (int i = 0; i < n; ++i) 16 | { 17 | while (nums[i] != i && nums[i] != n) 18 | swap(nums[i], nums[nums[i]]); 19 | } 20 | for (int i = 0; i < n; ++i) 21 | { 22 | if (nums[i] != i) 23 | return i; 24 | } 25 | return n; 26 | } 27 | }; 28 | 29 | //位运算 30 | class Solution1 31 | { 32 | public: 33 | int missingNumber(vector &nums) 34 | { 35 | int res = 0, n = nums.size(); 36 | for (int i = 0; i < n; ++i) 37 | { 38 | res = res ^ i ^ nums[i]; 39 | } 40 | return res ^ n; 41 | } 42 | }; 43 | 44 | int main() 45 | { 46 | system("pause"); 47 | return 0; 48 | } -------------------------------------------------------------------------------- /leetcode/27.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Solution { 11 | public: 12 | int removeElement(vector& nums, int val) { 13 | int i=0,n=nums.size(); 14 | while(i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int numSquares(int n) 11 | { 12 | if (n < 1) 13 | return 0; 14 | static vector dp = {0}; 15 | while (dp.size() <= n) 16 | { 17 | int cur = INT_MAX; 18 | int i = dp.size(); 19 | for (int j = 1; j * j <= i; ++j) 20 | { 21 | cur = min(cur, dp[i - j * j] + 1); 22 | } 23 | dp.emplace_back(cur); 24 | } 25 | return dp[n]; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | system("pause"); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /leetcode/28-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int strStr(string haystack, string needle) 10 | { 11 | if (needle.empty()) 12 | return 0; 13 | if (haystack.size() < needle.size()) 14 | return -1; 15 | int n = haystack.size(), m = needle.size(); 16 | int j = 0; 17 | int pos = 0; 18 | for (int i = 0; i <= n-m; ++i) 19 | { 20 | pos = i; 21 | while (haystack[i] == needle[j] && i < n && j < m) 22 | { 23 | if (j == m - 1) 24 | return i - j; 25 | i++; 26 | j++; 27 | } 28 | i = pos; 29 | j = 0; 30 | } 31 | return -1; 32 | } 33 | }; 34 | int main() 35 | { 36 | Solution solution; 37 | solution.strStr("mississippi", "issip"); 38 | system("pause"); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /leetcode/28-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int strStr(string haystack, string needle) 10 | { 11 | if (needle.empty()) 12 | return 0; 13 | if (haystack.size() < needle.size()) 14 | return -1; 15 | vector next(needle.size(), 0); 16 | for (int i = 2;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Solution 11 | { 12 | public: 13 | void moveZeroes(vector &nums) 14 | { 15 | if (nums.empty() || nums.size() == 1) 16 | return; 17 | int p1 = 0, p2 = 0; 18 | while (p2 < nums.size() && p1 < nums.size()) 19 | { 20 | while (p1 < nums.size() && nums[p1] != 0) 21 | { 22 | p1++; 23 | } 24 | if (p1 >= nums.size() - 1) 25 | break; 26 | p2 = p1 + 1; 27 | while (p2 < nums.size() && nums[p2] == 0) 28 | { 29 | p2++; 30 | } 31 | if (p2 >= nums.size()) 32 | break; 33 | swap(nums[p1], nums[p2]); 34 | p1++; 35 | p2++; 36 | } 37 | } 38 | }; 39 | 40 | class Solution2 41 | { 42 | public: 43 | void moveZeroes(vector &nums) 44 | { 45 | int j=0; 46 | for(int i=0;i 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution { 8 | public: 9 | int findDuplicate(vector& nums) { 10 | unordered_set set; 11 | int result = 0; 12 | for(auto num:nums){ 13 | if(set.find(num)!=set.end()){ 14 | result = num; 15 | break; 16 | }else{ 17 | set.insert(num); 18 | } 19 | 20 | } 21 | return result; 22 | } 23 | }; 24 | 25 | int main(){ 26 | system("pause"); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /leetcode/287-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int findDuplicate(vector &nums) 10 | { 11 | int low = 1; 12 | int high = nums.size() - 1; 13 | do 14 | { 15 | int count = 0; 16 | int mid = (low + high) / 2; 17 | for (auto num:nums) 18 | { 19 | if (num <= mid) 20 | count++; 21 | } 22 | if (count > mid) 23 | high = mid; 24 | else 25 | low = mid+1; 26 | } while (low < high); 27 | return low; 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | system("pause"); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /leetcode/287-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int findDuplicate(vector &nums) 10 | { 11 | int slow = 0; 12 | int fast = 0; 13 | int finder = 0; 14 | while (true) 15 | { 16 | slow = nums[slow]; 17 | fast = nums[nums[fast]]; 18 | if (slow == fast) 19 | break; 20 | } 21 | while (true) 22 | { 23 | slow = nums[slow]; 24 | finder = nums[finder]; 25 | if (slow == finder) 26 | return slow; 27 | } 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | system("pause"); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /leetcode/289.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | class Solution 10 | { 11 | public: 12 | void gameOfLife(vector> &board) 13 | { 14 | int m = board.size(), n = board[0].size(); 15 | 16 | for (int i = 0; i < m; i++) 17 | { 18 | for (int j = 0; j < n; j++) 19 | { 20 | int count = 0; 21 | for (int I = max(0, i - 1); I < min(i + 2, m); I++) 22 | { 23 | for (int J = max(0, j - 1); J < min(j + 2, n); J++) 24 | { 25 | count += board[I][J] > 1 ? board[I][J] - 2 : board[I][J]; 26 | } 27 | } 28 | if (count == 3 || count - board[i][j] == 3) 29 | board[i][j] += 2; 30 | } 31 | } 32 | 33 | for (int i = 0; i < m; i++) 34 | for (int j = 0; j < n; j++) 35 | board[i][j] = board[i][j] > 1 ? 1 : 0; 36 | } 37 | }; 38 | 39 | int main() 40 | { 41 | 42 | system("pause"); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /leetcode/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int lengthOfLongestSubstring(string s) 11 | { 12 | if (s.size() < 2) 13 | return s.size(); 14 | unordered_map indexs; 15 | indexs[s[0]] = 0; 16 | int dp[s.size()] = {1}; 17 | int length = 1; 18 | dp[0] = 1; 19 | for (int i = 1; i < s.size(); ++i) 20 | { 21 | if (indexs.find(s[i]) == indexs.end() || i - indexs[s[i]] > dp[i - 1]) 22 | { 23 | dp[i] = dp[i - 1] + 1; 24 | } 25 | else 26 | { 27 | dp[i] = i - indexs[s[i]]; 28 | } 29 | indexs[s[i]] = i; 30 | length = max(length, dp[i]); 31 | } 32 | 33 | return length; 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | Solution solution; 40 | solution.lengthOfLongestSubstring("abcabcbb"); 41 | system("pause"); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /leetcode/300.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int lengthOfLIS(vector &nums) 11 | { 12 | if (nums.size() < 2) 13 | return nums.size(); 14 | int n = nums.size(); 15 | int length = 1; 16 | vector tails(n, 0); 17 | tails[0] = nums[0]; 18 | for (int i = 0; i < n; ++i) 19 | { 20 | if (tails[0] > nums[i]) 21 | tails[0] = nums[i]; 22 | else if (nums[i] > tails[length - 1]) 23 | tails[length++] = nums[i]; 24 | else 25 | { 26 | auto it = std::lower_bound(tails.begin(), tails.begin() + length, nums[i]); 27 | *it = nums[i]; 28 | } 29 | } 30 | return length; 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | system("pause"); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /leetcode/303.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class NumArray 9 | { 10 | private: 11 | vector sums; 12 | 13 | public: 14 | NumArray(vector &nums) 15 | { 16 | sums.emplace_back(0); 17 | int res = 0; 18 | for (auto num : nums) 19 | { 20 | res += num; 21 | sums.emplace_back(res); 22 | } 23 | } 24 | 25 | int sumRange(int i, int j) 26 | { 27 | return sums[j + 1] - sums[i]; 28 | } 29 | }; 30 | 31 | int main() 32 | { 33 | system("pause"); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /leetcode/304.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class NumMatrix 9 | { 10 | public: 11 | NumMatrix(vector> &matrix) 12 | { 13 | int m = matrix.size(); 14 | int n = m > 0 ? matrix[0].size() : 0; 15 | sums = vector>(m + 1, vector(n + 1, 0)); 16 | for (int i = 0; i < m; ++i) 17 | { 18 | for (int j = 0; j < n; ++j) 19 | { 20 | sums[i + 1][j + 1] = matrix[i][j] + sums[i][j + 1] + sums[i + 1][j] - sums[i][j]; 21 | } 22 | } 23 | } 24 | 25 | int sumRegion(int row1, int col1, int row2, int col2) 26 | { 27 | return sums[row2 + 1][col2 + 1] - sums[row2 + 1][col1] - sums[row1][col2 + 1] + sums[row1][col1]; 28 | } 29 | 30 | private: 31 | vector> sums; 32 | }; 33 | 34 | int main() 35 | { 36 | system("pause"); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /leetcode/309.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int maxProfit(vector &prices) 12 | { 13 | if (prices.size() < 2) 14 | return 0; 15 | int n = prices.size(); 16 | vector> dp(n, vector(2, 0)); 17 | dp[0][1] = -prices[0]; 18 | dp[1][0] = max(dp[0][0], dp[0][1] + prices[1]); 19 | dp[1][1] = max(dp[0][1], -prices[1]); 20 | for (int i = 2; i < n; ++i) 21 | { 22 | dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]); 23 | dp[i][1] = max(dp[i - 1][1], dp[i - 2][0] - prices[i]); 24 | } 25 | return dp[n - 1][0]; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | system("pause"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /leetcode/312.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int maxCoins(vector &nums) 12 | { 13 | int n = nums.size(); 14 | nums.insert(nums.begin(), 1); 15 | nums.push_back(1); 16 | vector> dp(nums.size(), vector(nums.size(), 0)); 17 | for (int len = 1; len <= n; ++len) 18 | for (int left = 1; left <= n - len + 1; ++left) 19 | { 20 | int right = left + len - 1; 21 | for (int k = left; k <= right; ++k) 22 | dp[left][right] = max(dp[left][right], nums[left - 1] * nums[k] * nums[right + 1] + dp[left][k - 1] + dp[k + 1][right]); 23 | } 24 | return dp[1][n]; 25 | } 26 | }; 27 | 28 | int main() 29 | { 30 | vector inputs = {3, 1, 5, 8}; 31 | Solution s; 32 | s.maxCoins(inputs); 33 | system("pause"); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /leetcode/32.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int minDistance(string word1, string word2) 12 | { 13 | int m = word1.length(), n = word2.length(); 14 | vector> dp(m + 1, vector(n + 1, 0)); 15 | for (int i = 1; i <= m; ++i) 16 | { 17 | dp[i][0] = i; 18 | } 19 | for (int i = 1; i <= n; ++i) 20 | { 21 | dp[0][i] = i; 22 | } 23 | for (int i = 1; i <= m; ++i) 24 | { 25 | for (int j = 1; j <= n; ++j) 26 | { 27 | if (word1[i - 1] == word2[j - 1]) 28 | dp[i][j] = dp[i - 1][j - 1]; 29 | else 30 | dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1; 31 | } 32 | } 33 | return dp[m][n]; 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | string str = "()(())"; 40 | Solution s; 41 | s.longestValidParentheses(str); 42 | system("pause"); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /leetcode/322.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | // dp solution 9 | class Solution 10 | { 11 | public: 12 | int coinChange(vector &coins, int amount) 13 | { 14 | vector dp(amount + 1, amount + 1); 15 | dp[0] = 0; 16 | for (int coin : coins) 17 | { 18 | for (int i = coin; i <= amount; i++) 19 | { 20 | dp[i] = min(dp[i], dp[i - coin] + 1); 21 | } 22 | } 23 | return dp[amount] > amount ? -1 : dp[amount]; 24 | } 25 | }; 26 | 27 | //dfs solution 28 | class Solution2 29 | { 30 | private: 31 | void change(vector &coins, int pos, int amount, int &answer, int curCount) 32 | { 33 | if (pos == coins.size() - 1) 34 | { 35 | if (amount % coins[pos] == 0) 36 | { 37 | answer = min(answer, curCount + amount / coins[pos]); 38 | } 39 | } 40 | else 41 | { 42 | for (int i = amount / coins[pos]; i >= 0&&count+k &coins, int amount) 51 | { 52 | sort(coins.rbegin(), coins.rend()); 53 | int answer = INT_MAX; 54 | change(coins, 0, amount, answer, 0); 55 | return answer == INT_MAX ? -1 : answer; 56 | } 57 | }; 58 | 59 | int main() 60 | { 61 | system("pause"); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /leetcode/326-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Solution 6 | { 7 | public: 8 | bool isPowerOfThree(int n) 9 | { 10 | if (n <= 0) 11 | return false; 12 | while (n != 1) 13 | { 14 | if (n % 3 != 0) 15 | return false; 16 | n = n / 3; 17 | } 18 | return true; 19 | } 20 | }; 21 | 22 | int main() 23 | { 24 | Solution solution; 25 | cout << solution.isPowerOfThree(19684) << endl; 26 | system("pause"); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /leetcode/326-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | class Solution 7 | { 8 | public: 9 | bool isPowerOfThree(int n) 10 | { 11 | if (n <= 0) 12 | return false; 13 | auto result = log10(n) / log10(3); 14 | return floor(result)==result; 15 | } 16 | }; 17 | 18 | int main() 19 | { 20 | Solution solution; 21 | cout << solution.isPowerOfThree(19684) << endl; 22 | system("pause"); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /leetcode/328.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | struct ListNode 7 | { 8 | int val; 9 | ListNode *next; 10 | ListNode(int x) : val(x), next(NULL) {} 11 | }; 12 | 13 | class Solution 14 | { 15 | public: 16 | ListNode *oddEvenList(ListNode *head) 17 | { 18 | if (head == nullptr) 19 | return nullptr; 20 | ListNode *firstOdd = head; 21 | ListNode *firstEven = firstOdd->next; 22 | ListNode *even = firstEven; 23 | ListNode *odd = firstOdd; 24 | while (even != nullptr) 25 | { 26 | if (even->next == nullptr) 27 | break; 28 | odd->next = even->next; 29 | odd = odd->next; 30 | 31 | even->next = odd->next; 32 | even = even->next; 33 | } 34 | if (even != nullptr) 35 | node->next = nullptr; 36 | odd->next = even; 37 | return head; 38 | } 39 | }; 40 | int main() 41 | { 42 | system("pause"); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /leetcode/329.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const vector dx = {1, 0, 0, -1}; 7 | const vector dy = {0, 1, -1, 0}; 8 | 9 | class Solution 10 | { 11 | bool check(int m, int n, int i, int j) 12 | { 13 | return i >= 0 && i < m && j >= 0 && j < n; 14 | } 15 | int dfs(vector> &matrix, vector> &dist, int x, int y, int last) 16 | { 17 | if (!check(matrix.size(), matrix[0].size(), x, y)) 18 | return 0; 19 | if (matrix[x][y] <= last) 20 | return 0; 21 | if (dist[x][y] != 0) 22 | return dist[x][y]; 23 | for (int i = 0; i < 4; ++i) 24 | { 25 | int x1 = x + dx[i], y1 = y + dy[i]; 26 | dist[x][y] = max(dist[x][y], dfs(matrix, dist, x1, y1, matrix[x][y]) + 1); 27 | } 28 | return dist[x][y]; 29 | } 30 | 31 | public: 32 | int longestIncreasingPath(vector> &matrix) 33 | { 34 | if (matrix.empty()) 35 | return 0; 36 | int m = matrix.size(), n = matrix[0].size(); 37 | vector> dist(m, vector(n, 0)); 38 | int res = 0; 39 | for (int i = 0; i < m; ++i) 40 | { 41 | for (int j = 0; j < n; ++j) 42 | { 43 | int cur = dfs(matrix, dist, i, j, INT_MIN); 44 | res = max(res, cur); 45 | } 46 | } 47 | return res; 48 | } 49 | }; 50 | int main() 51 | { 52 | system("pause"); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /leetcode/334.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | bool increasingTriplet(vector &nums) 10 | { 11 | int first = INT_MAX; 12 | int second = INT_MAX; 13 | for (auto item : nums) 14 | { 15 | if(item<=first){ 16 | first = item; 17 | }else if(item<=second){ 18 | second = item; 19 | }else{ 20 | return true; 21 | } 22 | } 23 | return false; 24 | } 25 | }; 26 | 27 | int main() 28 | { 29 | return 0; 30 | } -------------------------------------------------------------------------------- /leetcode/337.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct TreeNode 9 | { 10 | int val; 11 | TreeNode *left; 12 | TreeNode *right; 13 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 14 | }; 15 | 16 | class Solution 17 | { 18 | pair dfs(TreeNode *node) 19 | { 20 | if (node == nullptr) 21 | return make_pair(0, 0); 22 | auto l = dfs(node->left); 23 | auto r = dfs(node->right); 24 | auto i = max(l.first, l.second) + max(r.first, r.second); 25 | auto j = l.first + r.first + node->val; 26 | return make_pair(i, j); 27 | } 28 | 29 | public: 30 | int rob(TreeNode *root) 31 | { 32 | auto res = dfs(root); 33 | return max(res.first, res.second); 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | system("pause"); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /leetcode/338.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | vector countBits(int num) 12 | { 13 | vector dp(num + 1, 0); 14 | int index = 1; 15 | for (int i = 1; i <= num; ++i) 16 | { 17 | if ((i & 1) == 1) 18 | { 19 | dp[i] = dp[i - 1] + 1; 20 | } 21 | else if ((i == (1 << index))) 22 | { 23 | dp[i] = 1; 24 | index++; 25 | } 26 | else 27 | { 28 | dp[i] = dp[i - 1]; 29 | } 30 | } 31 | return dp; 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | system("pause"); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /leetcode/34.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | private: 9 | void getRange(vector &nums, int index, vector &ret) 10 | { 11 | int pos = index; 12 | int target = nums[index]; 13 | while ((pos - 1) >= 0 && nums[pos - 1] == target) 14 | pos--; 15 | ret[0] = pos; 16 | pos = index; 17 | while ((pos + 1) < nums.size() && nums[pos + 1] == target) 18 | pos++; 19 | ret[1] = pos; 20 | } 21 | 22 | public: 23 | vector searchRange(vector &nums, int target) 24 | { 25 | vector ret = {-1, -1}; 26 | if (nums.empty()) 27 | return ret; 28 | int low = 0; 29 | int high = nums.size() - 1; 30 | while(low 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Solution 11 | { 12 | public: 13 | void reverseString(vector &s) 14 | { 15 | int i = 0, j = s.size() - 1; 16 | while (i < j) 17 | { 18 | swap(s[i++], s[j--]); 19 | } 20 | } 21 | }; 22 | 23 | int main() 24 | { 25 | system("pause"); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /leetcode/349.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | //hash table 10 | class Solution 11 | { 12 | public: 13 | vector intersection(vector &nums1, vector &nums2) 14 | { 15 | unordered_set keys(nums1.begin(), nums1.end()); 16 | vector res; 17 | for (auto num : nums2) 18 | { 19 | if (keys.count(num)) 20 | { 21 | res.emplace_back(num); 22 | keys.erase(num); 23 | } 24 | } 25 | return res; 26 | } 27 | }; 28 | 29 | //two pointer & sort 30 | class Solution1 31 | { 32 | public: 33 | vector intersection(vector &nums1, vector &nums2) 34 | { 35 | sort(nums1.begin(), nums1.end()); 36 | sort(nums2.begin(), nums2.end()); 37 | vector res; 38 | int i = 0, j = 0; 39 | while (i < nums1.size() && j < nums2.size()) 40 | { 41 | if (nums1[i] > nums2[j]) 42 | { 43 | j++; 44 | } 45 | else if (nums1[i] < nums2[j]) 46 | { 47 | i++; 48 | } 49 | else 50 | { 51 | if (res.empty() || nums1[i] != res.back()) 52 | { 53 | res.emplace_back(nums1[i]); 54 | } 55 | i++; 56 | j++; 57 | } 58 | } 59 | return res; 60 | } 61 | }; 62 | 63 | int main() 64 | { 65 | system("pause"); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /leetcode/350.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | class Solution 9 | { 10 | public: 11 | vector intersect(vector &nums1, vector &nums2) 12 | { 13 | unordered_map dict; 14 | for(auto num:nums1){ 15 | dict[num]++; 16 | } 17 | vector res; 18 | for(auto num:nums2){ 19 | if(dict.count(num)){ 20 | res.emplace_back(num); 21 | if(--dict[num]==0) 22 | dict.erase(num); 23 | } 24 | if(dict.empty()) 25 | break; 26 | } 27 | return res; 28 | } 29 | }; 30 | int main() 31 | { 32 | system("pause"); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /leetcode/357.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int countNumbersWithUniqueDigits(int n) 12 | { 13 | if (n == 0) 14 | return 1; 15 | vector dp(n + 1, 0); 16 | dp[0] = 1; 17 | for (int i = 1; i <= n; ++i) 18 | { 19 | if (i > 10) 20 | { 21 | dp[n - 1] = dp[i - 1]; 22 | break; 23 | } 24 | int cnt = 9, cur = i, mul = 9; 25 | while (--cur) 26 | { 27 | cnt *= mul--; 28 | } 29 | dp[i] = dp[i - 1] + cnt; 30 | } 31 | return dp[n]; 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | system("pause"); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /leetcode/36.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | bool isValidSudoku(vector> &board) 12 | { 13 | vector> visited1(9, vector(9, false)), visited2(9, vector(9, false)), visited3(9, vector(9, false)); 14 | 15 | for (int i = 0; i < 9; ++i) 16 | for (int j = 0; j < 9; ++j) 17 | if (board[i][j] != '.') 18 | { 19 | int num = board[i][j] - '0' - 1, k = i / 3 * 3 + j / 3; 20 | if (visited1[i][num] || visited2[j][num] || visited3[k][num]) 21 | return false; 22 | visited1[i][num] = visited2[j][num] = visited3[k][num] = true; 23 | } 24 | 25 | return true; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | system("pause"); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /leetcode/368.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | vector largestDivisibleSubset(vector &nums) 12 | { 13 | if (nums.size() < 2) 14 | return nums; 15 | sort(nums.begin(), nums.end(), greater()); 16 | int n = nums.size(); 17 | vector dp(n, 1); 18 | vector pre(n, 0); 19 | int m = 1, pos = 0; 20 | for (int i = 1; i < n; ++i) 21 | { 22 | for (int j = 0; j < i; ++j) 23 | { 24 | if (nums[j] % nums[i] == 0) 25 | { 26 | if (dp[i] < dp[j] + 1) 27 | { 28 | dp[i] = dp[j] + 1; 29 | pre[i] = j; 30 | if (dp[i] > m) 31 | { 32 | m = dp[i]; 33 | pos = i; 34 | } 35 | } 36 | } 37 | } 38 | } 39 | vector res; 40 | for (int i = 0; i < m; ++i) 41 | { 42 | res.emplace_back(nums[pos]); 43 | pos = pre[pos]; 44 | } 45 | return res; 46 | } 47 | }; 48 | 49 | int main() 50 | { 51 | system("pause"); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /leetcode/371.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int getSum(int a, int b) { 9 | 10 | } 11 | }; 12 | int main() 13 | { 14 | system("pause"); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /leetcode/38.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | string countAndSay(int n) 10 | { 11 | if (n == 1) 12 | return "1"; 13 | string str = "1"; 14 | while (--n) 15 | { 16 | string cur = ""; 17 | int count = 1; 18 | for (int i = 0; i < str.size() - 1; ++i) 19 | { 20 | if (str[i] == str[i + 1]) 21 | count++; 22 | else 23 | { 24 | cur += to_string(count) + str[i]; 25 | count = 1; 26 | } 27 | } 28 | cur += to_string(count) + str.back(); 29 | str = cur; 30 | } 31 | return str; 32 | } 33 | }; 34 | int main() 35 | { 36 | Solution solution; 37 | solution.countAndSay(4); 38 | system("pause"); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /leetcode/380.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | class RandomizedSet 8 | { 9 | private: 10 | unordered_map map; 11 | vector vec; 12 | std::default_random_engine generator; 13 | 14 | public: 15 | /** Initialize your data structure here. */ 16 | RandomizedSet() 17 | { 18 | } 19 | 20 | /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ 21 | bool insert(int val) 22 | { 23 | if (map.find(val) != map.end()) 24 | return false; 25 | vec.emplace_back(val); 26 | map[val] = vec.size() - 1; 27 | return true; 28 | } 29 | 30 | /** Removes a value from the set. Returns true if the set contained the specified element. */ 31 | bool remove(int val) 32 | { 33 | if (map.find(val) == map.end()) 34 | return false; 35 | int index = map[val]; 36 | map[vec.back()] = index; 37 | exchange(vec[index], vec[vec.size() - 1]); 38 | vec.pop_back(); 39 | map.erase(val); 40 | return true; 41 | } 42 | 43 | /** Get a random element from the set. */ 44 | int getRandom() 45 | { 46 | int n = vec.size(); 47 | int index = generator() % n; 48 | return vec[index]; 49 | } 50 | }; 51 | 52 | int main() 53 | { 54 | system("pause"); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /leetcode/387.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Solution 6 | { 7 | public: 8 | int firstUniqChar(string s) 9 | { 10 | int list[256] = {0}; 11 | for (auto i : s) 12 | list[i]++; 13 | for (int i = 0; i < s.length(); i++) 14 | if (list[s[i]] == 1) 15 | return i; 16 | return -1; 17 | } 18 | }; 19 | int main() 20 | { 21 | system("pause"); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /leetcode/39.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | void build(vector &candidates, int pos, int target, vector cur, vector>& res) 11 | { 12 | if (pos == candidates.size()) 13 | return; 14 | if (candidates[pos] > target) 15 | build(candidates, pos + 1, target, cur, res); 16 | else if(candidates[pos]==target) 17 | { 18 | cur.emplace_back(candidates[pos]); 19 | res.emplace_back(cur); 20 | cur.clear(); 21 | build(candidates, pos + 1,target - candidates[pos], cur, res); 22 | }else{ 23 | cur.emplace_back(candidates[pos]); 24 | build(candidates, pos, target-candidates[pos], cur, res); 25 | } 26 | } 27 | 28 | public: 29 | vector> combinationSum(vector &candidates, int target) 30 | { 31 | vector> res; 32 | sort(candidates.begin(), candidates.end(), greater()); 33 | if(candidates.empty()||candidates.back() cur; 36 | build(candidates, 0, target, cur, res); 37 | return res; 38 | } 39 | }; 40 | int main() 41 | { 42 | system("pause"); 43 | return 0; 44 | } -------------------------------------------------------------------------------- /leetcode/41.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | class Solution 7 | { 8 | public: 9 | int firstMissingPositive(vector &nums) 10 | { 11 | if (nums.empty()) 12 | return 1; 13 | int n = nums.size(); 14 | int res = n + 1; 15 | for (int i = 0; i < n; ++i) 16 | { 17 | while (nums[i] <= n && nums[i] > 0 && nums[i] != nums[nums[i] - 1]) 18 | { 19 | swap(nums[i], nums[nums[i] - 1]); 20 | } 21 | } 22 | for (int i = 0; i < n; ++i) 23 | { 24 | if (nums[i] != i + 1) 25 | { 26 | res = i + 1; 27 | break; 28 | } 29 | } 30 | return res; 31 | } 32 | }; 33 | int main() 34 | { 35 | system("pause"); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /leetcode/42-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Solution 6 | { 7 | public: 8 | int trap(vector &height) 9 | { 10 | int n = height.size(); 11 | if (n < 3) 12 | return 0; 13 | int ret = 0; 14 | int left = 0, right = n - 1; 15 | int maxLeft = 0, maxRight = 0; 16 | while(leftmaxLeft) 19 | maxLeft = height[left]; 20 | else 21 | ret += maxLeft - height[left]; 22 | left++; 23 | }else{ 24 | if(height[right]>maxRight) 25 | maxRight = height[right]; 26 | else 27 | ret += maxRight - height[right]; 28 | right--; 29 | } 30 | } 31 | return ret; 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | Solution solution; 38 | vector inputs = {5, 4, 1, 2}; 39 | cout << solution.trap(inputs) << endl; 40 | system("pause"); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /leetcode/42-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | class Solution 7 | { 8 | public: 9 | int trap(vector &height) 10 | { 11 | int n = height.size(); 12 | if (n < 3) 13 | return 0; 14 | int ret = 0; 15 | stack s; 16 | for (int i = 0; i < n; ++i) 17 | { 18 | while (!s.empty() && height[i] > height[s.top()]) 19 | { 20 | int low = height[s.top()]; 21 | s.pop(); 22 | if (!s.empty()) 23 | { 24 | int width = i - s.top() - 1; 25 | int h = min(height[i], height[s.top()]) - low; 26 | ret += (width * h); 27 | } 28 | } 29 | s.push(i); 30 | } 31 | return ret; 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | Solution solution; 38 | vector inputs = {5, 4, 1, 2}; 39 | cout << solution.trap(inputs) << endl; 40 | system("pause"); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /leetcode/44.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | class Solution 7 | { 8 | public: 9 | bool isMatch(string s, string p) 10 | { 11 | int i = 0, j = 0; 12 | int starI = -1, starJ = -1; 13 | int m = s.size(), n = p.size(); 14 | while (i < m) 15 | { 16 | if (j >= n) 17 | { 18 | if (starJ != -1) 19 | { 20 | j = starJ + 1; 21 | i = ++starI; 22 | } 23 | else 24 | { 25 | return false; 26 | } 27 | } 28 | else 29 | { 30 | if (p[j] == '*') 31 | { 32 | starJ = j++; 33 | starI = i; 34 | } 35 | else if (p[j] == '?' || s[i] == p[j]) 36 | { 37 | j++; 38 | i++; 39 | } 40 | else if (starJ != -1) 41 | { 42 | i = ++starI; 43 | j = starJ + 1; 44 | } 45 | else 46 | { 47 | return false; 48 | } 49 | } 50 | } 51 | while (j < n && p[j] == '*') 52 | { 53 | j++; 54 | } 55 | return j == n; 56 | } 57 | }; 58 | 59 | int main() 60 | { 61 | system("pause"); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /leetcode/45.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int jump(vector &nums) 11 | { 12 | if (nums.empty() || nums.size() == 1) 13 | return 0; 14 | int lastIndex = nums.size() - 1; 15 | int end = 0, start = 0, step = 0, maxIndex = 0; 16 | while (end < lastIndex) 17 | { 18 | step++; 19 | for (int i = start; i <= end; ++i) 20 | { 21 | if (i + nums[i] >= lastIndex) 22 | return step; 23 | maxIndex = max(i + nums[i], maxIndex); 24 | } 25 | start = end + 1; 26 | end = maxIndex; 27 | } 28 | return step; 29 | } 30 | }; 31 | int main() 32 | { 33 | vector inputs = {2, 3, 1, 1, 4}; 34 | Solution solution; 35 | return solution.jump(inputs); 36 | system("pause"); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /leetcode/454.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int fourSumCount(vector &A, vector &B, vector &C, vector &D) 11 | { 12 | unordered_map abSum; 13 | int res = 0, sum = 0; 14 | for (auto a : A) 15 | { 16 | for (auto b : B) 17 | { 18 | sum = a + b; 19 | if (abSum.find(sum) != abSum.end()) 20 | abSum[sum]++; 21 | else 22 | abSum.insert({sum, 1}); 23 | } 24 | } 25 | for (auto c : C) 26 | { 27 | for (auto d : D) 28 | { 29 | int key = -c - d; 30 | if (abSum.find(key) != abSum.end()) 31 | res += abSum[key]; 32 | } 33 | } 34 | return res; 35 | } 36 | }; 37 | 38 | int main() 39 | { 40 | system("pause"); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /leetcode/46.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | 8 | private: 9 | void getOrder(vector>& result,vector currentNums,int low,int high){ 10 | if(low==high){ 11 | result.push_back(currentNums); 12 | return; 13 | } 14 | for(int i=0;i<=low;++i){ 15 | swap(currentNums[i],currentNums[low]); 16 | getOrder(result,currentNums,low+1,high); 17 | swap(currentNums[i],currentNums[low]); 18 | } 19 | } 20 | public: 21 | 22 | vector> permute(vector& nums) { 23 | vector> result; 24 | getOrder(result,nums,1,nums.size()); 25 | return result; 26 | } 27 | }; 28 | 29 | int main(){ 30 | Solution solution; 31 | vector input={1,2,3}; 32 | auto result=solution.permute(input); 33 | for(auto vec:result){ 34 | for(auto item:vec){ 35 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Solution 11 | { 12 | public: 13 | void rotate(vector> &matrix) 14 | { 15 | int n = matrix.size(); 16 | int a = 0; 17 | int b = n - 1; 18 | while (a < b) 19 | { 20 | for (int i = 0; i < (b - a); ++i) 21 | { 22 | swap(matrix[a][a + i], matrix[a + i][b]); 23 | swap(matrix[a][a + i], matrix[b][b - i]); 24 | swap(matrix[a][a + i], matrix[b - i][a]); 25 | } 26 | ++a; 27 | --b; 28 | } 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | system("pause"); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /leetcode/5-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string longestPalindrome(string s) { 8 | if (s.empty()) return ""; 9 | int dp[s.size()][s.size()] = {0}, left = 0, right = 0, len = 0; 10 | for (int i = 0; i < s.size(); ++i) { 11 | for (int j = 0; j < i; ++j) { 12 | dp[j][i] = (s[i] == s[j] && (i - j < 2 || dp[j + 1][i - 1])); 13 | if (dp[j][i] && len < i - j + 1) { 14 | len = i - j + 1; 15 | left = j; 16 | right = i; 17 | } 18 | } 19 | dp[i][i] = 1; 20 | } 21 | return s.substr(left, right - left + 1); 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | 28 | system("pause"); 29 | } -------------------------------------------------------------------------------- /leetcode/5-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | string longestPalindrome(string s) 9 | { 10 | if (s.empty()) 11 | return ""; 12 | string prep = "#"; 13 | for (auto ch : s) 14 | { 15 | prep += ch; 16 | prep += "#"; 17 | } 18 | const int size_p = prep.size(); 19 | vector dp(size_p, 0); 20 | int center = 0, bCur = 0; 21 | for (int i = 0; i < size_p; i++) 22 | { 23 | int mirror = center - (i - center); 24 | dp[i] = bCur <= i ? 0 : min(bCur - i, dp[mirror]); 25 | int start = i - dp[i], end = i + dp[i]; 26 | while (start - 1 >= 0 && end + 1 < size_p && prep[start - 1] == prep[end + 1]) 27 | { 28 | --start; 29 | ++end; 30 | ++dp[i]; 31 | } 32 | if (i + dp[i] > bCur) 33 | { 34 | bCur = i + dp[i]; 35 | center = i; 36 | } 37 | } 38 | center = max_element(dp.begin(), dp.end()) - dp.begin(); 39 | return s.substr((center - dp[center]) / 2, dp[center]); 40 | } 41 | }; 42 | int main() 43 | { 44 | string s = "cbbd"; 45 | Solution solution; 46 | cout << solution.longestPalindrome(s) << endl; 47 | system("pause"); 48 | } -------------------------------------------------------------------------------- /leetcode/5291.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Solution { 12 | public: 13 | int findNumbers(vector& nums) { 14 | int res=0; 15 | for(auto num:nums){ 16 | auto str=to_string(num); 17 | if(str.size()%2==0) 18 | res++; 19 | } 20 | return res; 21 | } 22 | }; 23 | 24 | int main() 25 | { 26 | system("pause"); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /leetcode/5292.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Solution 12 | { 13 | bool check(vector &nums, int begin, int end) 14 | { 15 | int last = nums[begin]; 16 | for (int i = begin + 1; i < end; ++i) 17 | { 18 | if (last == nums[i] || last + 1 == nums[i]) 19 | { 20 | last = nums[i]; 21 | } 22 | else 23 | return false; 24 | } 25 | return true; 26 | } 27 | 28 | public: 29 | bool isPossibleDivide(vector &nums, int k) 30 | { 31 | if (nums.size() % k != 0 || k < 1) 32 | return false; 33 | if (k == 1) 34 | return true; 35 | sort(nums.begin(), nums.end()); 36 | int begin = 0, end = k; 37 | while (end <= nums.size()) 38 | { 39 | if (!check(nums, begin, end)) 40 | return false; 41 | end += k; 42 | begin += k; 43 | } 44 | return true; 45 | } 46 | }; 47 | 48 | int main() 49 | { 50 | vector in = {16, 5, 15, 15, 20, 16, 20, 14, 21, 20, 19, 20, 12, 17, 13, 15, 11, 17, 18, 18, 11, 13, 13, 14, 14, 9, 20, 18, 10, 4, 4, 6, 15, 19, 8, 15, 7, 17, 15, 9, 24, 2, 23, 22, 26, 8, 21, 22, 14, 13, 16, 2, 25, 23, 17, 19, 17, 3, 22, 23, 19, 12, 21, 12, 16, 27, 28, 10, 13, 8, 24, 3, 22, 6, 10, 9, 14, 7, 11, 22, 11, 5, 16, 19, 21, 2, 8, 24, 16, 21, 7, 29, 18, 9, 10, 18, 6, 17, 21, 20}; 51 | Solution s; 52 | s.isPossibleDivide(in, 10); 53 | system("pause"); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /leetcode/5293.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | hash g_hash_func; 12 | 13 | class Solution 14 | { 15 | 16 | public: 17 | int maxFreq(string s, int maxLetters, int minSize, int maxSize) 18 | { 19 | int res = 0; 20 | int i = 0, start = 0; 21 | unordered_map count; 22 | unordered_map ocurs; 23 | while (i < minSize) 24 | { 25 | count[s[i++]]++; 26 | } 27 | for (; i < s.length(); ++i) 28 | { 29 | start = i - minSize; 30 | if (count.size() <= maxLetters) 31 | { 32 | 33 | size_t hash_data = g_hash_func(s.substr(start, minSize)); 34 | ocurs[hash_data]++; 35 | res = max(res, ocurs[hash_data]); 36 | } 37 | count[s[start]]--; 38 | if (count[s[start]] == 0) 39 | count.erase(s[start]); 40 | count[s[i]]++; 41 | 42 | } 43 | return res; 44 | } 45 | }; 46 | 47 | int main() 48 | { 49 | system("pause"); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /leetcode/53.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int maxSubArray(vector &nums) 10 | { 11 | vector dp(nums.size(), 0); 12 | int i = 0,ret=nums[0]; 13 | dp[0] = nums[0]; 14 | for (int i = 1; i < nums.size();++i){ 15 | dp[i] = dp[i-1] > 0 ? dp[i-1] + nums[i] : nums[i]; 16 | if(ret 2 | #include 3 | 4 | using namespace std; 5 | class Solution 6 | { 7 | public: 8 | vector spiralOrder(vector> &matrix) 9 | { 10 | vector res; 11 | int m = matrix.size(); 12 | if (m == 0) 13 | return res; 14 | int n = matrix.front().size(); 15 | if (n == 0) 16 | return res; 17 | res.resize(m * n); 18 | int pos = 0; 19 | int beginCol = 0, endCol = n - 1; 20 | int beginRow = 0, endRow = m - 1; 21 | while (endRow >= beginRow && endCol >= beginCol) 22 | { 23 | 24 | for (int i = beginCol; i <= endCol; ++i) 25 | res[pos++] = matrix[beginRow][i]; 26 | if (++beginRow > endRow) 27 | break; 28 | for (int i = beginRow; i <= endRow; ++i) 29 | res[pos++] = matrix[i][endCol]; 30 | if (--endCol < beginCol) 31 | break; 32 | for (int i = endCol; i >= beginCol; --i) 33 | res[pos++] = matrix[endRow][i]; 34 | if (--endRow < beginRow) 35 | break; 36 | for (int i = endRow; i >= beginRow; --i) 37 | res[pos++] = matrix[i][beginCol]; 38 | if (beginCol++ > endCol) 39 | break; 40 | } 41 | return res; 42 | } 43 | }; 44 | int main() 45 | { 46 | vector> inputs = { 47 | {1, 2, 3}, 48 | {4, 5, 6}, 49 | {7, 8, 9}}; 50 | Solution solution; 51 | solution.spiralOrder(inputs); 52 | system("pause"); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /leetcode/55.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | bool canJump(vector &nums) 11 | { 12 | if (nums.empty()) 13 | return true; 14 | int lastIndex = nums.size() - 1; 15 | int maxIndex = 0; 16 | for (int i = 0; i < nums.size(); ++i) 17 | { 18 | if (maxIndex < i) 19 | return false; 20 | maxIndex = max(i + nums[i], maxIndex); 21 | } 22 | return maxIndex >= lastIndex; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | system("pause"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /leetcode/56.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | vector> merge(vector> &intervals) 11 | { 12 | 13 | vector> res; 14 | int n = intervals.size(); 15 | if (n < 1) 16 | return res; 17 | sort(intervals.begin(), intervals.end(), [](const vector &a, const vector &b) { return a[0] < b[0]; }); 18 | 19 | for (int i = 1; i < n; ++i) 20 | { 21 | if (intervals[i][0] <= intervals[i - 1][1]) 22 | { 23 | intervals[i][0] = intervals[i - 1][0]; 24 | intervals[i][1] = max(intervals[i][1], intervals[i - 1][1]); 25 | } 26 | else 27 | { 28 | res.push_back(intervals[i - 1]); 29 | } 30 | } 31 | res.push_back(intervals[n - 1]); 32 | return res; 33 | } 34 | }; 35 | 36 | int main() 37 | { 38 | system("pause"); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /leetcode/567.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | bool checkInclusion(string s1, string s2) { 9 | 10 | } 11 | }; 12 | 13 | int main() 14 | { 15 | system("pause"); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /leetcode/568.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | bool checkInclusion(string s1, string s2) { 9 | 10 | } 11 | }; 12 | 13 | int main() 14 | { 15 | system("pause"); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /leetcode/62.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | public: 9 | int uniquePaths(int m, int n) 10 | { 11 | if (m < 1 || n < 1) 12 | return 0; 13 | if (m == 1 || n == 1) 14 | return 1; 15 | vector dp(n, 1); 16 | for (int i = 1; i < m; ++i) 17 | { 18 | for (int j = 1; j < n; ++j) 19 | { 20 | dp[j] = dp[j - 1] + dp[j]; 21 | } 22 | } 23 | return dp[n - 1]; 24 | } 25 | }; 26 | 27 | int main() 28 | { 29 | system("pause"); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /leetcode/63.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int uniquePathsWithObstacles(vector> &obstacleGrid) 12 | { 13 | if (obstacleGrid.empty() || obstacleGrid[0].empty()) 14 | return 0; 15 | int m = obstacleGrid.size(), n = obstacleGrid[0].size(); 16 | 17 | vector> dp(m, vector(n, 0)); 18 | for (int i = 0; i < m; ++i) 19 | { 20 | if (obstacleGrid[i][0] == 1) 21 | break; 22 | else 23 | dp[i][0] = 1; 24 | } 25 | for (int i = 0; i < n; ++i) 26 | { 27 | if (obstacleGrid[0][i] == 1) 28 | break; 29 | else 30 | dp[0][i] = 1; 31 | } 32 | for (int i = 1; i < m; ++i) 33 | { 34 | for (int j = 1; j < n; ++j) 35 | { 36 | if (obstacleGrid[i][j] == 0) 37 | { 38 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 39 | } 40 | } 41 | } 42 | return dp[m - 1][n - 1]; 43 | } 44 | }; 45 | 46 | int main() 47 | { 48 | system("pause"); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /leetcode/64.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int minPathSum(vector> &grid) 12 | { 13 | int m = grid.size(), n = grid[0].size(); 14 | vector> dp(m, vector(n, 0)); 15 | dp[0][0] = grid[0][0]; 16 | for (int i = 1; i < m; ++i) 17 | { 18 | dp[i][0] = dp[i - 1][0] + grid[i][0]; 19 | } 20 | for (int i = 1; i < n; ++i) 21 | { 22 | dp[0][i] = dp[0][i - 1] + grid[0][i]; 23 | } 24 | for (int i = 1; i < m; ++i) 25 | { 26 | for (int j = 1; j < n; ++j) 27 | { 28 | dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; 29 | } 30 | } 31 | return dp[m - 1][n - 1]; 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | system("pause"); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /leetcode/647.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * @Author: yangpan 3 | * @Date: 2020-02-08 17:37:48 4 | * @LastEditors : yangpan 5 | * @LastEditTime : 2020-02-08 17:38:35 6 | * @Description: file content 7 | * @FilePath: \leetcode\647.cpp 8 | */ 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | using namespace std; 15 | 16 | 17 | class Solution { 18 | public: 19 | int countSubstrings(string s) { 20 | int n=s.length(),result=0; 21 | if(n<2) return n; 22 | for(int i=0;i=0&&r=0&&r 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | vector plusOne(vector& digits) { 9 | int pos = digits.size() - 1; 10 | while(pos>=0){ 11 | int digit = digits[pos] + 1; 12 | if(digit==10){ 13 | digits[pos] = 0; 14 | pos--; 15 | }else{ 16 | digits[pos] = digit; 17 | break; 18 | } 19 | } 20 | if(pos==-1) 21 | digits.insert(digits.begin(), 1); 22 | return digits; 23 | } 24 | }; 25 | int main() 26 | { 27 | system("pause"); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /leetcode/687.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct TreeNode 9 | { 10 | int val; 11 | TreeNode *left; 12 | TreeNode *right; 13 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 14 | }; 15 | 16 | class Solution 17 | { 18 | private: 19 | // get the longest path in current tree 20 | int visit(TreeNode *node, int &longest) 21 | { 22 | //get longest in left subtree 23 | int left = node->left != nullptr ? visit(node->left, longest) : 0; 24 | int right = node->right != nullptr ? visit(node->right, longest) : 0; 25 | int resLeft = node->left && node->left->val == node->val ? left + 1 : 0; 26 | int resRight = node->right && node->right->val == node->val ? right + 1 : 0; 27 | longest = max(longest, resLeft + resRight); 28 | return max(resLeft, resRight); 29 | } 30 | 31 | public: 32 | int longestUnivaluePath(TreeNode *root) 33 | { 34 | if (root == nullptr) 35 | return 0; 36 | int res = 0; 37 | visit(root, res); 38 | return res; 39 | } 40 | }; 41 | 42 | int main() 43 | { 44 | system("pause"); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /leetcode/69.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int mySqrt(int x) 9 | { 10 | if (x <= 1) 11 | return x; 12 | long r = x; 13 | while (r * r > x) 14 | r = (r + x / r) / 2; 15 | return r; 16 | } 17 | }; 18 | 19 | int main() 20 | { 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /leetcode/695.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const vector dx = {0, 1, -1, 0}; 9 | const vector dy = {1, 0, 0, -1}; 10 | 11 | class Solution 12 | { 13 | bool check(int m, int n, int x, int y) 14 | { 15 | return x >= 0 && x < m && y >= 0 && y < n; 16 | } 17 | void dfs(vector> &grid, int x, int y, int &res) 18 | { 19 | res++; 20 | grid[x][y] = 0; 21 | for (int i = 0; i < 4; ++i) 22 | { 23 | int x1 = x + dx[i], y1 = y + dy[i]; 24 | if (check(grid.size(), grid[0].size(), x1, y1) && grid[x1][y1] == 1) 25 | dfs(grid, x1, y1, res); 26 | } 27 | } 28 | 29 | public: 30 | int maxAreaOfIsland(vector> &grid) 31 | { 32 | if (grid.empty() || grid[0].empty()) 33 | return 0; 34 | int m = grid.size(), n = grid[0].size(); 35 | int res = 0; 36 | for (int i = 0; i < m; ++i) 37 | { 38 | for (int j = 0; j < n; ++j) 39 | { 40 | if (grid[i][j] == 1) 41 | { 42 | int cur = 0; 43 | dfs(grid, i, j, cur); 44 | res = max(cur, res); 45 | } 46 | } 47 | } 48 | return res; 49 | } 50 | }; 51 | 52 | int main() 53 | { 54 | system("pause"); 55 | return 0; 56 | } -------------------------------------------------------------------------------- /leetcode/7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int reverse(int x) 11 | { 12 | long result = 0; 13 | while (x!= 0) 14 | { 15 | result *= 10; 16 | result += x % 10; 17 | x = x / 10; 18 | } 19 | if (result > INT32_MAX || result < INT32_MIN) 20 | return 0; 21 | return result; 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | Solution solution; 28 | solution.reverse(123); 29 | system("pause"); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /leetcode/70.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | int climbStairs(int n) { 9 | if(n<=2) 10 | return n; 11 | vector dp(n+1,1); 12 | dp[2] = 2; 13 | for (int i = 3; i <= n;++i){ 14 | dp[i] = dp[i - 1] + dp[i - 2]; 15 | } 16 | return dp[n]; 17 | } 18 | }; 19 | 20 | int main() 21 | { 22 | 23 | system("pause"); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /leetcode/744.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | char nextGreatestLetter(vector &letters, char target) 12 | { 13 | int min = INT_MAX; 14 | char res; 15 | for (auto letter : letters) 16 | { 17 | int cur = letter > target ? letter - target : 'z' - target + (letter - 'a') + 1; 18 | if (cur < min) 19 | { 20 | min = cur; 21 | res = letter; 22 | } 23 | } 24 | return res; 25 | } 26 | }; 27 | 28 | int main() 29 | { 30 | system("pause"); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /leetcode/75.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Solution 11 | { 12 | public: 13 | void sortColors(vector &nums) 14 | { 15 | if (nums.size() <= 1) 16 | return; 17 | int low = 0, mid = 0, high = nums.size() - 1; 18 | while(mid<=high){ 19 | if(nums[mid]==0) 20 | { 21 | swap(nums[low], nums[mid]); 22 | mid++; 23 | low++; 24 | }else if(nums[mid]==2){ 25 | swap(nums[mid], nums[high]); 26 | mid++; 27 | high--; 28 | }else{ 29 | mid++; 30 | } 31 | } 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | system("pause"); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /leetcode/78.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | class Solution { 6 | public: 7 | vector> subsets(vector& nums) { 8 | vector> res; 9 | auto n=nums.size(); 10 | if(nums.empty()) 11 | return res; 12 | for(int i=0;i<(1< sub; 14 | for(int j=0;j 2 | #include 3 | 4 | using namespace std; 5 | const vector dx = {1, 0, -1, 0}; 6 | const vector dy = {0, 1, 0, -1}; 7 | 8 | class Solution 9 | { 10 | bool check(int m, int n, int i, int j) 11 | { 12 | return i >= 0 && i < m && j >= 0 && j < n; 13 | } 14 | bool dfs(vector> &board, string &word, int pos, int x, int y) 15 | { 16 | int m = board.size(), n = board[0].size(); 17 | if (word[pos] != board[x][y]) 18 | return false; 19 | if (pos == word.size() - 1) 20 | return true; 21 | auto tmp = board[x][y]; 22 | board[x][y] = '#'; 23 | for (int i = 0; i < 4; ++i) 24 | { 25 | int x1 = x + dx[i], y1 = y + dy[i]; 26 | if (check(m, n, x1, y1)) 27 | if (dfs(board, word, pos + 1, x1, y1)) 28 | return true; 29 | } 30 | board[x][y] = tmp; 31 | return false; 32 | } 33 | 34 | public: 35 | bool exist(vector> &board, string word) 36 | { 37 | int m = board.size(), n = board[0].size(); 38 | for (int i = 0; i < m; ++i) 39 | { 40 | for (int j = 0; j < n; ++j) 41 | { 42 | if (board[i][j] == word[0]) 43 | { 44 | if (dfs(board, word, 0, i, j)) 45 | return true; 46 | } 47 | } 48 | } 49 | return false; 50 | } 51 | }; 52 | int main() 53 | { 54 | system("pause"); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /leetcode/8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int myAtoi(string str) 11 | { 12 | int sign = 1; 13 | long long result = 0; 14 | for (int i = 0; i < str.size(); ++i) 15 | { 16 | auto pos = str.find_first_not_of(' '); 17 | if(pos==string::npos) 18 | return 0; 19 | i=pos; 20 | if (str[i] == '-' || str[i] == '+') 21 | sign = str[i++] == '-' ? -1 : 1; 22 | while (i < str.size() && '0' <= str[i] && str[i] <= '9') 23 | { 24 | result = result * 10 + str[i++] - '0'; 25 | if (result * sign > INT_MAX) 26 | return INT_MAX; 27 | if (result * sign < INT_MIN) 28 | return INT_MIN; 29 | } 30 | break; 31 | } 32 | return result * sign; 33 | } 34 | }; 35 | 36 | int main() 37 | { 38 | string str = "20000000000000000000"; 39 | Solution solution; 40 | cout << solution.myAtoi(str) << endl; 41 | system("pause"); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /leetcode/84.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | int largestRectangleArea(vector &heights) 11 | { 12 | if (heights.empty()) 13 | return 0; 14 | if (heights.size() == 1) 15 | return heights[0]; 16 | heights.emplace_back(-1); 17 | int n = heights.size(); 18 | int maxArea = 0, i = 0; 19 | stack indexs; 20 | while (i < n) 21 | { 22 | if (indexs.empty() || heights[i] >= heights[indexs.top()]) 23 | indexs.push(i++); 24 | else 25 | { 26 | int j = indexs.top(); 27 | indexs.pop(); 28 | int cur = heights[j] * (indexs.empty() ? i : (i - indexs.top() - 1)); 29 | maxArea = max(maxArea, cur); 30 | } 31 | } 32 | return maxArea; 33 | } 34 | }; 35 | 36 | int main() 37 | { 38 | vector input = {4, 2, 0, 3, 2, 5}; 39 | Solution solution; 40 | solution.largestRectangleArea(input); 41 | system("pause"); 42 | return 0; 43 | } -------------------------------------------------------------------------------- /leetcode/85.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution { 8 | public: 9 | int maximalRectangle(vector>& matrix) { 10 | if(matrix.empty()) 11 | return 0; 12 | int m=matrix.size(), n=matrix[0].size(); 13 | vector cur(n+1,0); 14 | cur[n]=-1; 15 | int res=0; 16 | for(int i=0;i indexs; 22 | while (k <= n) 23 | { 24 | if (indexs.empty() || cur[k] >= cur[indexs.top()]) 25 | indexs.push(k++); 26 | else 27 | { 28 | int t = indexs.top(); 29 | indexs.pop(); 30 | int area = cur[t] * (indexs.empty() ? k : (k - indexs.top() - 1)); 31 | res= max(res, area); 32 | } 33 | } 34 | } 35 | return res; 36 | } 37 | }; 38 | int main() 39 | { 40 | system("pause"); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /leetcode/87.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | class Solution 8 | { 9 | public: 10 | bool isScramble(string s1, string s2) 11 | { 12 | int m = s1.length(), n = s2.length(); 13 | if (m != n) 14 | return false; 15 | if (m == 1) 16 | return s1 == s2; 17 | vector>> dp(n + 1, vector>(n, vector(n, false))); 18 | //初始化,边界情况 19 | for (int i = 0; i < n; ++i) 20 | { 21 | for (int j = 0; j < n; ++j) 22 | { 23 | dp[1][i][j] = s1[i] == s2[j]; 24 | } 25 | } 26 | //枚举 27 | for (int l = 2; l <= n; ++l) 28 | { 29 | for (int i = 0; i + l <= n; ++i) 30 | { 31 | for (int j = 0; j + l <= n; ++j) 32 | { 33 | for (int k = 1; k <= l; ++k) 34 | { 35 | dp[l][i][j] = (dp[k][i][j] && dp[l - k][i + k][j + k]) || (dp[k][i][j + l - k] && dp[l - k][i + k][j]); 36 | if (dp[l][i][j]) 37 | break; 38 | } 39 | } 40 | } 41 | } 42 | return dp[n][0][0]; 43 | } 44 | }; 45 | 46 | int main() 47 | { 48 | system("pause"); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /leetcode/88.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution 8 | { 9 | public: 10 | void merge(vector &nums1, int m, vector &nums2, int n) 11 | { 12 | int k = m + n - 1, i = m - 1, j = n - 1; 13 | while (k >= 0) 14 | { 15 | if (j < 0) 16 | break; 17 | if (i < 0) 18 | nums1[k--] = nums2[j--]; 19 | else 20 | nums1[k--] = nums1[i] > nums2[j] ? nums1[i--] : nums2[j--]; 21 | } 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | system("pause"); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /leetcode/91.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Solution 7 | { 8 | private: 9 | bool isValid(char first, char second) 10 | { 11 | return first == '1' || (first == '2' && second <= '6'); 12 | } 13 | 14 | public: 15 | int numDecodings(string s) 16 | { 17 | if (s.empty() || s[0] == '0') 18 | return 0; 19 | if (s.size() == 1) 20 | return 1; 21 | int cur = 0, f_1 = 1, f_2 = 1; 22 | for (int i = 1; i < s.size(); ++i) 23 | { 24 | int temp = f_1; 25 | if (s[i] == '0') 26 | { 27 | if (!isValid(s[i - 1], s[i])) 28 | return 0; 29 | cur = f_2; 30 | } 31 | else 32 | { 33 | if (isValid(s[i - 1], s[i])) 34 | cur = f_1 + f_2; 35 | else 36 | cur = f_1; 37 | } 38 | f_1 = cur; 39 | f_2 = temp; 40 | } 41 | return cur; 42 | } 43 | }; 44 | 45 | int main() 46 | { 47 | system("pause"); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /leetcode/94.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | 8 | struct TreeNode { 9 | int val; 10 | TreeNode *left; 11 | TreeNode *right; 12 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 13 | }; 14 | 15 | class Solution { 16 | public: 17 | vector inorderTraversal(TreeNode* root) { 18 | vector res; 19 | stack s; 20 | auto node=root; 21 | while(node!=nullptr||!s.empty()){ 22 | while(node!=nullptr){ 23 | s.push(node); 24 | node=node->left; 25 | } 26 | if(!s.empty()){ 27 | node=s.top(); 28 | res.emplace_back(node->val); 29 | s.pop(); 30 | node=node->right; 31 | } 32 | } 33 | return res; 34 | } 35 | }; 36 | int main() 37 | { 38 | system("pause"); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /leetcode/96.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | int numTrees(int n) 12 | { 13 | 14 | vector dp(n + 1, 0); 15 | dp[0] = 1; 16 | dp[1] = 1; 17 | for (int i = 2; i <= n; ++i) 18 | { 19 | for (int j = 1; j <= i; ++j) 20 | { 21 | dp[i] += dp[j - 1] * dp[i - j]; 22 | } 23 | } 24 | return dp[n]; 25 | } 26 | }; 27 | 28 | int main() 29 | { 30 | system("pause"); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /leetcode/97.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | bool isInterleave(string s1, string s2, string s3) 12 | { 13 | int m = s1.length(), n = s2.length(), l = s3.length(); 14 | if (m + n != l) 15 | return false; 16 | vector> dp(m + 1, vector(n + 1, false)); 17 | dp[0][0] = true; 18 | for (int i = 1; i <= m; ++i) 19 | { 20 | if (s3[i - 1] != s1[i - 1]) 21 | break; 22 | dp[i][0] = true; 23 | } 24 | for (int i = 1; i <= n; ++i) 25 | { 26 | if (s3[i - 1] != s2[i - 1]) 27 | break; 28 | dp[0][i] = true; 29 | } 30 | for (int i = 1; i <= m; ++i) 31 | { 32 | for (int j = 1; j <= n; ++j) 33 | { 34 | dp[i][j] = (s1[i - 1] == s3[i + j - 1] && dp[i - 1][j]) || (s2[j - 1] == s3[i + j - 1] && dp[i][j - 1]); 35 | } 36 | } 37 | return dp[m][n]; 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | system("pause"); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /leetcode/98.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | struct TreeNode 12 | { 13 | int val; 14 | TreeNode *left; 15 | TreeNode *right; 16 | TreeNode(int x) : val(x), left(NULL), right(NULL) {} 17 | }; 18 | 19 | class Solution 20 | { 21 | void inOrder(TreeNode *node, vector &res) 22 | { 23 | if (node == nullptr) 24 | return; 25 | inOrder(node->left, res); 26 | res.emplace_back(node->val); 27 | inOrder(node->right, res); 28 | } 29 | 30 | public: 31 | bool isValidBST(TreeNode *root) 32 | { 33 | vector res; 34 | inOrder(root, res); 35 | for (int i = 1; i < res.size(); ++i) 36 | { 37 | if (res[i] <= res[i - 1]) 38 | return false; 39 | } 40 | return true; 41 | } 42 | }; 43 | 44 | class Solution1 45 | { 46 | bool inOrder(TreeNode *node, TreeNode *pre) 47 | { 48 | if (node == nullptr) 49 | return true; 50 | if (pre != nullptr && pre->val >= node->val) 51 | return false; 52 | return inOrder(node->left, node)&&inOrder(node->right, node); 53 | } 54 | 55 | public: 56 | bool isValidBST(TreeNode *root) 57 | { 58 | TreeNode *pre = nullptr; 59 | return inOrder(root, pre); 60 | } 61 | }; 62 | 63 | int main() 64 | { 65 | system("pause"); 66 | return 0; 67 | } -------------------------------------------------------------------------------- /leetcode/980.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | const vector dx = {-1, 0, 0, 1}; 9 | const vector dy = {0, 1, -1, 0}; 10 | 11 | class Solution 12 | { 13 | 14 | bool check(int i, int j, int m, int n) { return (i < m && i >= 0) && (j < n && j >= 0); } 15 | 16 | void dfs(vector> &grid, int x, int y, int cnt, int &res) 17 | { 18 | int m = grid.size(), n = grid[0].size(); 19 | if (grid[x][y] == 2 && cnt == 0) 20 | { 21 | res++; 22 | return; 23 | } 24 | if (grid[x][y] == -1 || grid[x][y] == 2) 25 | return; 26 | cnt--; 27 | auto tmp = grid[x][y]; 28 | grid[x][y] = -1; 29 | for (int i = 0; i < 4; ++i) 30 | { 31 | auto x1 = x + dx[i], y1 = y + dy[i]; 32 | if (check(x1, y1, m, n)) 33 | dfs(grid, x1, y1, cnt, res); 34 | } 35 | grid[x][y] = tmp; 36 | } 37 | 38 | public: 39 | int uniquePathsIII(vector> &grid) 40 | { 41 | int m = grid.size(), n = grid[0].size(); 42 | int res = 0, cnt = 1; 43 | pair start; 44 | for (int i = 0; i < m; ++i) 45 | { 46 | for (int j = 0; j < n; ++j) 47 | { 48 | if (grid[i][j] == 1) 49 | { 50 | start = make_pair(i, j); 51 | } 52 | else if (grid[i][j] == 0) 53 | cnt++; 54 | } 55 | } 56 | dfs(grid, start.first, start.second, cnt, res); 57 | return res; 58 | } 59 | }; 60 | 61 | int main() 62 | { 63 | system("pause"); 64 | return 0; 65 | } -------------------------------------------------------------------------------- /sample/union_find.cpp: -------------------------------------------------------------------------------- 1 | 2 | class UnionFind { 3 | private: 4 | std::vector _parents; 5 | int _len; 6 | 7 | public: 8 | UnionFind(int n) { 9 | _len = n; 10 | _parents.resize(n); 11 | for (int i = 0; i < n; ++i) { 12 | _parents[i] = i; 13 | } 14 | } 15 | 16 | int find(int u) { 17 | if (_parents[u] != u) { 18 | _parents[u] = find(_parents[u]); 19 | } 20 | return _parents[u]; 21 | } 22 | 23 | bool is_connected(int u, int v) { return find(u) == find(v); } 24 | 25 | // false - 代表图有环路 26 | bool unin(int u, int v) { 27 | int fau = find(u); 28 | int fav = find(v); 29 | if (fau == fav) return false; 30 | _parents[fav] = fau; 31 | return true; 32 | } 33 | 34 | int get_connected_component() { 35 | int cnt = -1; 36 | for (int i = 0; i < _len; ++i) { 37 | if (i == _parents[i]) cnt++; 38 | } 39 | return cnt; 40 | } 41 | }; --------------------------------------------------------------------------------