├── LeetCode ├── LC427.cpp ├── LC292.cpp ├── LC231.cpp ├── LC319.cpp ├── LC237.cpp ├── LC441.cpp ├── LC326.cpp ├── LC172.cpp ├── LC390.cpp ├── LC521.cpp ├── LC709.cpp ├── LC190.cpp ├── LC191.cpp ├── LC344.cpp ├── LC70.cpp ├── LC268.cpp ├── LC342.cpp ├── LC261.cpp ├── LC343.cpp ├── LC458.cpp ├── LC217.cpp ├── LC371.cpp ├── LC5788.cpp ├── LC274.cpp ├── LC476.cpp ├── LC810.cpp ├── LC575.cpp ├── LC171.cpp ├── LC775.cpp ├── LC509.cpp ├── LC201.cpp ├── LC628.cpp ├── LC162.cpp ├── LC392.cpp ├── LC495.cpp ├── LC461.cpp ├── LC55.cpp ├── LC45.cpp ├── LC453.cpp ├── LC633.cpp ├── LC69.cpp ├── LC242.cpp ├── LC27.cpp ├── LC492.cpp ├── LC561.cpp ├── LC58.cpp ├── LC189.cpp ├── LC278.cpp ├── LC598.cpp ├── LC9.cpp ├── LC226.cpp ├── LC754.cpp ├── LC769.cpp ├── LC693.cpp ├── LC7.cpp ├── LC50.cpp ├── LC941.cpp ├── LC26.cpp ├── LC283.cpp ├── LC537.cpp ├── LC541.cpp ├── LC462.cpp ├── LC518.cpp ├── LC53.cpp ├── LC717.cpp ├── LC771.cpp ├── LC779.cpp ├── LC11.cpp ├── LC35.cpp ├── LC409.cpp ├── LC921.cpp ├── LC942.cpp ├── LC367.cpp ├── LC387.cpp ├── LC908.cpp ├── LC169.cpp ├── LC746.cpp ├── LC806.cpp ├── LC896.cpp ├── LC20.cpp ├── LC650.cpp ├── LC724.cpp ├── LC829.cpp ├── LC238.cpp ├── LC442.cpp ├── LC470.cpp ├── LC852.cpp ├── LC868.cpp ├── LC887.cpp ├── LC594.cpp ├── LC791.cpp ├── LC168.cpp ├── LC187.cpp ├── LC75.cpp ├── LC235.cpp ├── LC334.cpp ├── LC922.cpp ├── LC657.cpp ├── LC836.cpp ├── LC932.cpp ├── LC944.cpp ├── LC405.cpp ├── LC451.cpp ├── LC520.cpp ├── LC881.cpp ├── LC230.cpp ├── LC303.cpp ├── LC365.cpp ├── LC481.cpp ├── LC5789.cpp ├── LC780.cpp ├── LC945.cpp ├── LC258.cpp ├── LC38.cpp ├── LC704.cpp ├── LC905.cpp ├── LC940.cpp ├── LC946.cpp ├── LC357.cpp ├── LC551.cpp ├── LC832.cpp ├── LC858.cpp ├── LC898.cpp ├── LC204.cpp ├── LC781.cpp ├── LC643.cpp ├── LC762.cpp ├── LC330.cpp ├── LC383.cpp ├── LC434.cpp ├── LC674.cpp ├── LC747.cpp ├── LC14.cpp ├── LC386.cpp ├── LC41.cpp ├── LC477.cpp ├── LC507.cpp ├── LC540.cpp ├── LC557.cpp ├── LC667.cpp ├── LC179.cpp ├── LC219.cpp ├── LC5802.cpp ├── LC848.cpp ├── LC141.cpp ├── LC448.cpp ├── LC899.cpp ├── LC22.cpp ├── LC397.cpp ├── LC738.cpp ├── LC744.cpp ├── LC94.cpp ├── LC416.cpp ├── LC678.cpp ├── LC867.cpp ├── LC926.cpp ├── LC933.cpp ├── LC167.cpp ├── LC349.cpp ├── LC49.cpp ├── LC566.cpp ├── LC830.cpp ├── LC844.cpp ├── LC914.cpp ├── LC930.cpp ├── LC240.cpp ├── LC48.cpp ├── LC485.cpp ├── LC739.cpp ├── LC915.cpp ├── LC917.cpp ├── LC239.cpp ├── LC31.cpp ├── LC350.cpp ├── LC455.cpp ├── LC611.cpp ├── LC483.cpp ├── LC920.cpp ├── LC153.cpp ├── LC205.cpp ├── LC789.cpp ├── LC869.cpp ├── LC209.cpp ├── LC459.cpp ├── LC525.cpp ├── LC66.cpp ├── LC345.cpp ├── LC42.cpp ├── LC888.cpp ├── LC154.cpp ├── LC532.cpp ├── LC696.cpp ├── LC728.cpp ├── LC646.cpp ├── LC413.cpp ├── LC456.cpp ├── LC904.cpp ├── LC910.cpp ├── LC275.cpp ├── LC414.cpp ├── LC419.cpp ├── LC949.cpp ├── LC396.cpp ├── LC412.cpp ├── LC925.cpp ├── LC950.cpp ├── LC400.cpp ├── LC553.cpp ├── LC645.cpp ├── LC912.CPP ├── LC228.cpp ├── LC556.cpp ├── LC60.cpp ├── LC682.cpp ├── LC713.cpp ├── LC554.cpp ├── LC766.cpp ├── LC504.cpp ├── LC672.cpp ├── LC856.cpp ├── LC883.cpp ├── LC482.cpp ├── LC560.cpp ├── LC849.cpp ├── LC147.cpp ├── LC214.cpp ├── LC375.cpp ├── LC389.cpp ├── LC893.cpp ├── LC144.cpp ├── LC668.cpp ├── LC784.cpp ├── LC821.cpp ├── LC825.cpp ├── LC12.cpp ├── LC377.cpp ├── LC528.cpp ├── LC714.cpp ├── LC846.cpp ├── LC891.cpp ├── LC13.cpp ├── LC67.cpp ├── LC740.cpp ├── LC837.cpp ├── LC929.cpp ├── LC948.cpp ├── LC324.cpp ├── LC96.cpp ├── LC6.cpp ├── LC316.cpp ├── LC372.cpp ├── LC795.cpp ├── LC797.cpp ├── LC216.cpp ├── LC236.cpp ├── LC335.cpp ├── LC347.cpp ├── LC479.cpp ├── LC565.cpp ├── LC763.cpp ├── LC793.cpp ├── LC260.cpp ├── LC328.cpp ├── LC398.cpp ├── LC452.cpp ├── LC841.cpp ├── LC199.cpp ├── LC28.cpp ├── LC539.cpp ├── LC684.cpp ├── LC735.cpp ├── LC918.cpp ├── LC402.cpp ├── LC454.cpp ├── LC822.cpp ├── LC875.cpp ├── LC16.cpp ├── LC424.cpp ├── LC630.cpp ├── LC697.cpp ├── LC732.cpp ├── LC860.cpp ├── LC877.cpp ├── LC768.cpp ├── LC203.cpp ├── LC374.cpp ├── LC435.cpp ├── LC486.cpp ├── LC649.cpp ├── LC680.cpp ├── LC777.cpp ├── LC953.cpp ├── LC332.cpp ├── LC647.cpp ├── LC581.cpp ├── LC753.cpp ├── LC799.cpp ├── LC8.cpp ├── LC438.cpp ├── LC474.cpp ├── LC900.cpp ├── LC559.cpp ├── LC756.cpp ├── LC787.cpp ├── LC17.cpp ├── LC790.cpp ├── LC871.cpp ├── lc57.cpp ├── LC503.cpp ├── LC524.cpp ├── LC813.cpp ├── LC845.cpp ├── LC876.cpp ├── LC879.cpp ├── LC165.cpp ├── LC206.cpp ├── LC299.cpp ├── LC1337.cpp ├── LC59.cpp ├── LC804.cpp ├── LC931.cpp ├── LC467.cpp ├── LC526.cpp ├── LC664.cpp ├── LC500.cpp ├── LC516.cpp ├── LC318.cpp ├── LC64.cpp ├── LC220.cpp ├── LC222.cpp ├── LC331.cpp ├── LC34.cpp ├── LC765.cpp ├── LC901.cpp ├── LC44.cpp ├── LC547.cpp ├── LC629.cpp ├── LC719.cpp ├── LC731.cpp ├── LC807.cpp ├── LC835.cpp ├── LC937.cpp ├── LC583.cpp ├── LC690.cpp ├── LC788.cpp ├── LC502.cpp ├── LC812.cpp ├── LC890.cpp └── LC207.cpp ├── README.md ├── .gitattributes ├── .gitignore ├── ACwing ├── ac801.cpp ├── ac900.cpp ├── ac77.cpp ├── ac2_01.cpp ├── ac3.cpp ├── coin.cpp ├── ac2816.cpp ├── ac898.cpp ├── CMakeLists.txt ├── ac104.cpp ├── ac790.cpp ├── ac1611.cpp ├── ac1453.cpp ├── ac4.cpp ├── ac1455.cpp ├── ac52.cpp ├── ac896.cpp ├── ac830.cpp ├── ac9.cpp ├── ac895.cpp ├── ac875.cpp ├── ac897.cpp ├── ohter.cpp ├── ac800.cpp ├── ac842.cpp ├── ac1612.cpp ├── ac19.cpp ├── ac906.cpp ├── ac799.cpp ├── ac902.cpp ├── ac795.cpp ├── ac61.cpp └── ac841.cpp └── daily └── 104.cpp /LeetCode/LC427.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 刷的leetcode题目和一些基础竞赛题目 2 | -------------------------------------------------------------------------------- /LeetCode/LC292.cpp: -------------------------------------------------------------------------------- 1 | bool canWinNim(int n) { 2 | return n % 4; 3 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.txt linguist-language=c++ 2 | * Makefile linguist-language=c++ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.exe 3 | ACwing/.idea/ 4 | LeetCode/.idea/ 5 | ACwing/cmake-build-debug/ 6 | .vscode 7 | -------------------------------------------------------------------------------- /LeetCode/LC231.cpp: -------------------------------------------------------------------------------- 1 | 2 | // lowbit运算 3 | bool isPowerOfTwo(int n){ 4 | return (n > 0) ? ((n & -n) == n) : false; 5 | } -------------------------------------------------------------------------------- /LeetCode/LC319.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bulbSwitch(int n) { 4 | return sqrt(n); 5 | } 6 | }; -------------------------------------------------------------------------------- /LeetCode/LC237.cpp: -------------------------------------------------------------------------------- 1 | 2 | void deleteNode(ListNode* node) { 3 | node->val = node->next->val; 4 | node->next = node->next->next; 5 | } -------------------------------------------------------------------------------- /LeetCode/LC441.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrangeCoins(int n) { 4 | return (-1 + sqrt(1 + 8.0 * n)) / 2; 5 | } 6 | }; -------------------------------------------------------------------------------- /LeetCode/LC326.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | return n > 0 && 1162261467 % n == 0; 5 | } 6 | }; -------------------------------------------------------------------------------- /LeetCode/LC172.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | int trailingZeros(int n){ 5 | int res = 0; 6 | while(n){ 7 | res += n / 5; 8 | n /= 5; 9 | } 10 | return res; 11 | } -------------------------------------------------------------------------------- /LeetCode/LC390.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lastRemaining(int n) { 4 | if (n == 1) return 1; 5 | return 2 * (n/2 + 1 - lastRemaining(n / 2)); 6 | } 7 | }; -------------------------------------------------------------------------------- /LeetCode/LC521.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLUSlength(string a, string b) { 4 | if (a == b) return -1; 5 | return max(a.size(), b.size()); 6 | } 7 | }; -------------------------------------------------------------------------------- /LeetCode/LC709.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string toLowerCase(string str) { 4 | string res; 5 | for(auto c : str) res += tolower(c); 6 | return res; 7 | } 8 | }; -------------------------------------------------------------------------------- /LeetCode/LC190.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | uint32_t reverseBits(uint32_t n) { 4 | uint32_t res = 0; 5 | for(int i = 0; i < 32; i++) { 6 | res = (res << 1) + (n >> i & 1); 7 | } 8 | return res; 9 | } -------------------------------------------------------------------------------- /LeetCode/LC191.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | int hammingWeight(uint32_t n) { 6 | int res = 0; 7 | for(int i = 0; i < 32; i++){ 8 | res += (n >> i & 1); 9 | } 10 | return res; 11 | } -------------------------------------------------------------------------------- /LeetCode/LC344.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void reverseString(vector& s) { 4 | int n = s.size(); 5 | for(int i = 0, j = n - 1; i < j; i++, j--) swap(s[i], s[j]); 6 | } 7 | }; -------------------------------------------------------------------------------- /LeetCode/LC70.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | int a = 1, b = 1; 5 | while(--n) { 6 | int c = a + b; 7 | a = b, b = c; 8 | } 9 | return b; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC268.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | int missingNumber(vector& nums) { 4 | int n = nums.size(); 5 | int res = n * (n + 1) / 2; 6 | int sum = 0; 7 | for(auto i : nums) sum += i; 8 | return res - sum; 9 | } -------------------------------------------------------------------------------- /LeetCode/LC342.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfFour(int n) { 4 | if(n <= 0) return false; 5 | int t = sqrt(n); 6 | return (t * t) == n && ((1 << 30) % n == 0); 7 | } 8 | }; -------------------------------------------------------------------------------- /LeetCode/LC261.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | bool isUgly(int num) { 4 | if(num <= 0) return false; 5 | while(num % 2 == 0) num /= 2; 6 | while(num % 3 == 0) num /= 3; 7 | while(num % 5 == 0) num /= 5; 8 | return num == 1; 9 | } -------------------------------------------------------------------------------- /LeetCode/LC343.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int integerBreak(int n) { 4 | if(n <= 3) return 1 * (n - 1); 5 | int r = 1; 6 | while(n >= 5) n -= 3, r *= 3; 7 | return r * n; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/LC458.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int poorPigs(int buckets, int minutesToDie, int minutesToTest) { 4 | int k = minutesToTest / minutesToDie; 5 | return ceil(log(buckets) / log(k+1)); 6 | } 7 | }; -------------------------------------------------------------------------------- /LeetCode/LC217.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | bool containsDuplicate(vector& nums) { 4 | unordered_map hash; 5 | for(auto a : nums){ 6 | hash[a]++; 7 | if(hash[a] > 1) return true; 8 | } 9 | return false; 10 | } -------------------------------------------------------------------------------- /LeetCode/LC371.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getSum(int a, int b) { 4 | if(a == 0) return b; 5 | int sum = a ^ b; 6 | int carry = (unsigned)(a & b) << 1; 7 | return getSum(carry, sum); 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/LC5788.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestOddNumber(string num) { 4 | int k = num.size() - 1; 5 | while(k >= 0 && num[k] % 2 == 0) k--; 6 | if(k < 0) return ""; 7 | return num.substr(0, k+1); 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/LC274.cpp: -------------------------------------------------------------------------------- 1 | 2 | int hIndex(vector &citations) { 3 | sort(citations.begin(), citations.end(), greater()); 4 | for(int h = citations.size(); h; h--){ 5 | if(citations[h-1] >= h) return h; 6 | } 7 | return 0; 8 | } -------------------------------------------------------------------------------- /LeetCode/LC476.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) { 4 | if(!num) return 1; 5 | int cnt = 0; 6 | for(int x = num; x; x>>=1) cnt++; 7 | return ~num & ((1ll << cnt) -1 ); 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/LC810.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool xorGame(vector& nums) { 4 | int tot = 0; 5 | for(int x : nums) tot ^= x; 6 | if(tot == 0) return true; 7 | return nums.size() % 2 == 0; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/LC575.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int distributeCandies(vector& candyType) { 4 | unordered_set s; 5 | for(auto c : candyType) s.insert(c); 6 | return min(candyType.size() / 2, s.size()); 7 | } 8 | }; -------------------------------------------------------------------------------- /LeetCode/LC171.cpp: -------------------------------------------------------------------------------- 1 | 2 | int titleToNumber(string s) { 3 | int n = s.size(); 4 | int a = 0; 5 | for(long long i = 0, p = 26; i < n-1; i++, p *= 26) a += p; 6 | int b = 0; 7 | for(auto c : s) b = b * 26 + c - 'A'; 8 | return a + b + 1; 9 | } -------------------------------------------------------------------------------- /LeetCode/LC775.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isIdealPermutation(vector& nums) { 4 | for(int i = 0; i < nums.size(); i++) { 5 | if(abs(nums[i] - i) > 1) return false; 6 | } 7 | return true; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/LC509.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | int a=0, b=1, c=0; 5 | while(n--){ 6 | c = a + b; 7 | a = b; 8 | b = c; 9 | } 10 | return a; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC201.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | int rangeBitwiseAnd(int m, int n) { 5 | int res = 0; 6 | for (int i = 30; i >= 0; i -- ) { 7 | if ((m >> i & 1) != (n >> i & 1)) break; 8 | if (m >> i & 1) res += 1 << i; 9 | } 10 | return res; 11 | } -------------------------------------------------------------------------------- /LeetCode/LC628.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumProduct(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | int n = nums.size(); 6 | return max(nums[0] * nums[1] * nums[n-1], nums[n-1] * nums[n-2] * nums[n-3]); 7 | } 8 | }; -------------------------------------------------------------------------------- /LeetCode/LC162.cpp: -------------------------------------------------------------------------------- 1 | 2 | int findPeakElement(vector &nums) { 3 | int l = 0, r = nums.size()-1; 4 | while (l < r){ 5 | int mid = (l + r) / 2; 6 | if(nums[mid] > nums[mid+1]) r = mid; 7 | else l = mid + 1; 8 | } 9 | return r; 10 | } -------------------------------------------------------------------------------- /LeetCode/LC392.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSubsequence(string s, string t) { 4 | int k = 0; 5 | for(auto c : t){ 6 | if(k < s.size() && c == s[k]) k++; 7 | } 8 | return k == s.size(); 9 | } 10 | 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC495.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPoisonedDuration(vector& t, int d) { 4 | int res = 0; 5 | for(int i = 1; i < t.size(); i++) res += min(t[i]-t[i-1], d); 6 | if(t.size()) res += d; 7 | return res; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/LC461.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingDistance(int x, int y) { 4 | int res = 0; 5 | x = x ^ y; 6 | while(x) { 7 | res++; 8 | x = x & (x - 1); 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC55.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canJump(vector& nums) { 4 | int n = nums.size(); 5 | for(int i = 1, j = 0; i < n; i++) { 6 | while(j < i && j + nums[j] < i) j++; 7 | if(j == i) return false; 8 | } 9 | return true; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC45.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector& nums) { 4 | int n = nums.size(); 5 | vector f(n); 6 | for(int i = 1, j = 0; i < n; i++) { 7 | while(j + nums[j] < i) j++; 8 | f[i] = f[j] + 1; 9 | } 10 | return f[n-1]; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC453.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves(vector& nums) { 4 | int minv = INT_MAX; 5 | for(auto x : nums) minv = min(minv, x); 6 | int res = 0; 7 | for(auto x : nums) res += x - minv; 8 | return res; 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/LC633.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool judgeSquareSum(int c) { 4 | for(long long i = 0; i * i <= c; i++) { 5 | int j = sqrt(c - i * i); 6 | if(j * j + i * i == c) return true; 7 | } 8 | return false; 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/LC69.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mySqrt(int x) { 4 | if(x <= 1) return x; 5 | int l = 0, r = x; 6 | while(l < r) { 7 | int mid = l + 1ll + r >> 1; 8 | if(mid <= x / mid) l = mid; 9 | else r = mid - 1; 10 | } 11 | return r; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC242.cpp: -------------------------------------------------------------------------------- 1 | 2 | bool isAnagram(string s, string t) { 3 | unordered_map hash; 4 | for(auto c : s) hash[c-'a']++; 5 | for(auto c : t) hash[c-'a']--; 6 | for(int i = 0; i < 26; i ++){ 7 | if(hash[i] != 0) return false; 8 | } 9 | return true; 10 | } -------------------------------------------------------------------------------- /LeetCode/LC27.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeElement(vector& nums, int val) { 4 | int n = nums.size(); 5 | int k = 0; 6 | for(int i = 0; i < n; i++) { 7 | if(nums[i] != val) { 8 | nums[k++] = nums[i]; 9 | } 10 | } 11 | return k; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC492.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector constructRectangle(int area) { 4 | for(int i = sqrt(area); ; i--) { 5 | if(area % i == 0) { 6 | return {area / i, i}; 7 | } 8 | } 9 | return {}; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC561.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrayPairSum(vector& nums) { 4 | int n = nums.size(); 5 | sort(nums.begin(), nums.end()); 6 | int res = 0; 7 | for(int i = 0; i < n; i += 2) res += nums[i]; 8 | return res; 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/LC58.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLastWord(string s) { 4 | for(int i = s.size() - 1; i >= 0; i--) { 5 | if(s[i] == ' ') continue; 6 | int j = i - 1; 7 | while(j >= 0 && s[j] != ' ') j--; 8 | return i - j; 9 | } 10 | return 0; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC189.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | void rotate(vector& nums, int k) { 4 | int n = nums.size(); 5 | k %= n; 6 | int l = n - k; 7 | reverse(nums.begin(), nums.begin() + l); 8 | reverse(nums.begin()+l, nums.end()); 9 | reverse(nums.begin(), nums.end()); 10 | return; 11 | } -------------------------------------------------------------------------------- /LeetCode/LC278.cpp: -------------------------------------------------------------------------------- 1 | 2 | // bool isBadVersion(int version); 3 | 4 | int firstBadVersion(int n) { 5 | int l = 1, r = n; 6 | while(l < r) { 7 | int mid = (l + 0ll + r) / 2; 8 | if(isBadVersion(mid)) r = mid; 9 | else l = mid + 1; 10 | } 11 | return l; 12 | } -------------------------------------------------------------------------------- /LeetCode/LC598.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxCount(int m, int n, vector>& ops) { 4 | int x = m, y = n; 5 | for(auto p : ops) { 6 | x = min(x, p[0]); 7 | y = min(y, p[1]); 8 | } 9 | return x * y; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC9.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPalindrome(int x) { 4 | if(x < 0) return false; 5 | int r = 0, k = x; 6 | while(k) { 7 | if(r > (INT_MAX - k % 10) / 10) return false; 8 | r = r * 10 + k % 10; 9 | k /= 10; 10 | } 11 | return r == x; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC226.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | TreeNode* invertTree(TreeNode* root) { 5 | if(root == nullptr) return nullptr; 6 | TreeNode *left = invertTree(root->left); 7 | TreeNode *right = invertTree(root->right); 8 | root->left = right; 9 | root->right = left; 10 | return root; 11 | } -------------------------------------------------------------------------------- /LeetCode/LC754.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reachNumber(int target) { 4 | target = abs(target); 5 | int n = 0, sum = 0; 6 | while(sum < target || (sum - target) % 2) { 7 | n++; 8 | sum += n; 9 | } 10 | return n; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC769.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxChunksToSorted(vector& arr) { 4 | int res = 0; 5 | for(int i = 0, x = 0; i < arr.size(); i++) { 6 | x = max(arr[i], x); 7 | if(x == i) res++; 8 | } 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC693.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasAlternatingBits(int n) { 4 | for(int i = 1; 1ll << i <= n; i++) { 5 | int a = n >> i - 1 & 1; 6 | int b = n >> i & 1; 7 | if (a == b) return false; 8 | } 9 | return true; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC7.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reverse(int x) { 4 | int r; 5 | while(x) { 6 | if(r > 0 && r > (INT_MAX - x % 10) / 10 ) return 0; 7 | if(r < 0 && r < (INT_MIN - x % 10) / 10) return 0; 8 | r = r * 10 + x % 10; 9 | x /= 10; 10 | } 11 | return r; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC50.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | typedef long long LL; 4 | 5 | double myPow(double x, int n) { 6 | double res = 1, p = x; 7 | LL t = abs((LL)(n)); 8 | for(; t; t >>= 1) { 9 | if(t & 1) res = res * p; 10 | p = p * p; 11 | } 12 | return n > 0 ? res : 1 / res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC941.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validMountainArray(vector& arr) { 4 | int k = 0, n = arr.size(); 5 | while(k + 1 < n && arr[k] < arr[k+1]) k++; 6 | if(k+1 == n || k == 0) return false; 7 | while(k + 1 < n && arr[k] > arr[k+1]) k++; 8 | return k + 1 == n; 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/LC26.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int removeDuplicates(vector& nums) { 4 | int n = nums.size(); 5 | if(n < 1) return 0; 6 | int k = 0; 7 | for(int i = 1; i < n; i++) { 8 | if(nums[i] != nums[k]) { 9 | nums[++k] = nums[i]; 10 | } 11 | } 12 | return k + 1; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC283.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | void moveZeroes(vector &nums) { 4 | int n = nums.size(); 5 | int j = 0; 6 | for(int i = 0; i < n; i++){ 7 | if(nums[i] != 0){ 8 | nums[j++] = nums[i]; 9 | } 10 | } 11 | for(int i = j; i < n; i++){ 12 | nums[i] = 0; 13 | } 14 | } -------------------------------------------------------------------------------- /LeetCode/LC537.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string complexNumberMultiply(string x, string y) { 4 | int a, b, c, d; 5 | sscanf(x.c_str(), "%d+%di", &a, &b); 6 | sscanf(y.c_str(), "%d+%di", &c, &d); 7 | return to_string(a * c - b * d) + "+" + to_string(a * d + b * c) + "i"; 8 | } 9 | }; -------------------------------------------------------------------------------- /LeetCode/LC541.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseStr(string s, int k) { 4 | int n = s.size(); 5 | for(int i = 0; i < n; i += 2 * k) { 6 | int l = i, r = min(i + k, n); 7 | reverse(s.begin()+l, s.begin() + r); 8 | } 9 | return s; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC462.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves2(vector& nums) { 4 | int res = 0; 5 | int n = nums.size(); 6 | sort(nums.begin(), nums.end()); 7 | for(auto x : nums) { 8 | res += abs(x - nums[n/2]); 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC518.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int change(int amount, vector& coins) { 4 | vector f(amount+1, 0); 5 | f[0] = 1; 6 | for(auto x : coins) { 7 | for(int i = x; i <= amount; i++) f[i] += f[i - x]; 8 | } 9 | return f[amount]; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC53.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | int res = INT_MIN; 5 | for(int i = 0, last = 0; i < nums.size(); i++) { 6 | last = nums[i] + max(last, 0); 7 | res = max(res, last); 8 | } 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC717.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isOneBitCharacter(vector& bits) { 4 | int n = bits.size(); 5 | for(int i = 0; i < n; i++) { 6 | if(i == n-1 && !bits[i]) return true; 7 | if(bits[i]) i++; 8 | } 9 | return false; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC771.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numJewelsInStones(string jewels, string stones) { 4 | unordered_set S(jewels.begin(), jewels.end()); 5 | int res = 0; 6 | for(auto x : stones) { 7 | if(S.count(x)) res++; 8 | } 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC779.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kthGrammar(int N, int K) { 4 | int res = 0; 5 | for(; N > 1; N--) { 6 | if(K > (1 << N - 2)) { 7 | res ^= 1; 8 | K -= 1 << N - 2; 9 | } 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC11.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int res = 0, n = height.size(); 5 | for(int i = 0, j = n - 1; i <= j;) { 6 | res = max(res, min(height[i], height[j]) * (j - i)); 7 | if(height[i] < height[j]) i++; 8 | else j--; 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC35.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector& nums, int target) { 4 | if(nums.empty()) return 0; 5 | int l = 0, r = nums.size(); 6 | while(l < r) { 7 | int mid = l + r >> 1; 8 | if(nums[mid] >= target) r = mid; 9 | else l = mid + 1; 10 | } 11 | return l; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC409.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindrome(string s) { 4 | unordered_map hash; 5 | for(auto c : s) hash[c]++; 6 | int res = 0; 7 | for(auto [c, k] : hash) res += k / 2 * 2; 8 | if (res < s.size()) res++; 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC921.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minAddToMakeValid(string s) { 4 | stack st; 5 | int res = 0; 6 | for(auto c : s) { 7 | if(c == '(') st.push(0); 8 | else { 9 | if(st.empty()) res++; 10 | else st.pop(); 11 | } 12 | } 13 | res += st.size(); 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC942.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector diStringMatch(string s) { 4 | int n = s.size(); 5 | int l = 0, r = n; 6 | vector res; 7 | for(auto c : s) { 8 | if(c == 'I') res.push_back(l++); 9 | else res.push_back(r--); 10 | } 11 | res.push_back(l); 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC367.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPerfectSquare(int num) { 4 | int l = 0, r = num; 5 | while(l < r){ 6 | int mid = l + 1ll + r >> 1; 7 | if(mid <= num / mid) l = mid; 8 | else r = mid - 1; 9 | } 10 | return r * r == num; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC387.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstUniqChar(string s) { 4 | unordered_map hash; 5 | for(auto c : s) hash[c]++; 6 | int n = s.size(); 7 | for(int i = 0; i < n; i++){ 8 | if(hash[s[i]] == 1) return i; 9 | } 10 | return -1; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC908.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestRangeI(vector& nums, int k) { 4 | int minw = INT_MAX, maxw = INT_MIN; 5 | for (auto x: nums) { 6 | minw = min(minw, x); 7 | maxw = max(maxw, x); 8 | } 9 | return max(0, maxw - minw - k * 2); 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC169.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | int majorityElement(vector &nums){ 6 | int cnt = 0, candidate = 0; 7 | int n = nums.size(); 8 | for(int i = 0; i < n; i++){ 9 | if (cnt == 0) candidate = nums[i]; 10 | if(nums[i] == candidate) cnt++; 11 | else cnt --; 12 | } 13 | return candidate; 14 | } -------------------------------------------------------------------------------- /LeetCode/LC746.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostClimbingStairs(vector& cost) { 4 | int n = cost.size(); 5 | vector f(n); 6 | f[0] = cost[0], f[1] = cost[1]; 7 | for(int i = 2; i < n; i++) f[i] = min(f[i-1], f[i-2]) + cost[i]; 8 | return min(f[n-1], f[n-2]); 9 | } 10 | }; -------------------------------------------------------------------------------- /LeetCode/LC806.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector numberOfLines(vector& widths, string s) { 4 | int r = 0, c = 0; 5 | for(auto& x : s){ 6 | int w = widths[x-'a']; 7 | if(c+w > 100) r++, c = 0; 8 | c += w; 9 | } 10 | return {r+1, c}; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC896.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMonotonic(vector& nums) { 4 | bool x = true, y = true; 5 | for(int i = 1; i < nums.size(); i++) { 6 | if(nums[i-1] > nums[i]) x = false; 7 | if(nums[i-1] < nums[i]) y = false; 8 | } 9 | return x || y; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC20.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValid(string s) { 4 | stack st; 5 | for(auto &c : s) { 6 | if(c == '(' || c == '[' || c == '{') st.push(c); 7 | else { 8 | if(st.size() && abs(st.top() - c) <= 2) st.pop(); 9 | else return false; 10 | } 11 | } 12 | return st.size() == 0; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC650.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSteps(int n) { 4 | int res = 0; 5 | int i = 2; 6 | while(n > 1) { 7 | while(n % i == 0) { 8 | n /= i; 9 | res += i; 10 | } 11 | i++; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC724.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int pivotIndex(vector& nums) { 4 | int sum = accumulate(nums.begin(), nums.end(),0); 5 | for(int i = 0, s = 0; i < nums.size(); i++) { 6 | if(s == sum - s - nums[i]) return i; 7 | s += nums[i]; 8 | } 9 | return -1; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC829.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int consecutiveNumbersSum(int n) { 4 | n *= 2; 5 | int res = 0; 6 | for(int b = 1; b * b <= n; b++) { 7 | if(n % b == 0) { 8 | if((n / b - b +1) % 2 == 0) res++; 9 | } 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC238.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | vector productExceptSelf(vector& nums) { 6 | int n = nums.size(); 7 | vector ret(n, 1); 8 | for(int i = 1; i < n; i++) ret[i] = ret[i-1] * nums[i-1]; 9 | for(int i = n-1, s = 1; i >= 0; i--){ 10 | ret[i] *= s; 11 | s *= nums[i]; 12 | } 13 | return ret; 14 | } -------------------------------------------------------------------------------- /LeetCode/LC442.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDuplicates(vector& nums) { 4 | vector res; 5 | for(auto x : nums) { 6 | int idx = abs(x) - 1; 7 | nums[idx] *= -1; 8 | if(nums[idx] > 0) res.push_back(abs(x)); 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC470.cpp: -------------------------------------------------------------------------------- 1 | // The rand7() API is already defined for you. 2 | // int rand7(); 3 | // @return a random integer in the range 1 to 7 4 | 5 | class Solution { 6 | public: 7 | int rand10() { 8 | int t = (rand7()-1) * 7 + rand7(); // 生成1-49之间的数 9 | if(t > 40) return rand10(); 10 | return (t-1) % 10 + 1; 11 | } 12 | }; -------------------------------------------------------------------------------- /ACwing/ac801.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int lowbit(int x) { 5 | int res = 0; 6 | for(int i = x; i; i = i&(i-1)) res++; 7 | return res; 8 | } 9 | 10 | int main() { 11 | int n; 12 | scanf("%d", &n); 13 | while(n--) { 14 | int x, s; 15 | scanf("%d", &x); 16 | printf("%d ", lowbit(x)); 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /LeetCode/LC852.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int peakIndexInMountainArray(vector& arr) { 4 | int l = 1, r = arr.size() - 2; 5 | while(l < r){ 6 | int mid = (l + r + 1) >> 1; 7 | if(arr[mid-1] < arr[mid]) l = mid; 8 | else r = mid - 1; 9 | } 10 | return r; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC868.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int binaryGap(int n) { 4 | int res = 0, last = -1; 5 | for(int i = 0; i < 30; i++){ 6 | if(n >> i & 1) { 7 | if(last != -1) res = max(res, i - last); 8 | last = i; 9 | } 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC887.cpp: -------------------------------------------------------------------------------- 1 | int f[10010][110]; 2 | class Solution { 3 | public: 4 | int superEggDrop(int k, int n) { 5 | for(int i = 1; i <= n; i++) { 6 | for(int j = 1; j <= k; j++) 7 | f[i][j] = f[i-1][j-1] + 1 + f[i-1][j]; 8 | if(f[i][k] >= n) return i; 9 | } 10 | return -1; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC594.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLHS(vector& nums) { 4 | unordered_map hash; 5 | for(auto n : nums) hash[n]++; 6 | int res = 0; 7 | for(auto [c, n] : hash) { 8 | if(hash.count(c+1)) res = max(res, hash[c] + hash[c+1]); 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC791.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string customSortString(string S, string T) { 4 | unordered_map hash; 5 | for(int i = 0; i < S.size(); i++) hash[S[i]] = i; 6 | sort(T.begin(), T.end(), [&](char a, char b) { 7 | return hash[a] < hash[b]; 8 | }); 9 | return T; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC168.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | string convertToTile(int n) { 5 | int k = 1; 6 | for(long long p = 26; n > p; p *= 26){ 7 | n -= p; 8 | k++; 9 | } 10 | string res; 11 | n--; 12 | while(k--) { 13 | res += n%26 + 'A'; 14 | n /= 26; 15 | } 16 | reverse(res.begin(), res.end()); 17 | return res; 18 | } -------------------------------------------------------------------------------- /LeetCode/LC187.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | vector findRepeatedSequence(string s){ 5 | vector ret; 6 | unordered_map hash; 7 | int n = s.size(); 8 | for(int i = 0; i < n; i++){ 9 | string tmp = s.substr(i, 10); 10 | if(hash[tmp] == 1) ret.push_back(tmp); 11 | hash[tmp]++; 12 | } 13 | return ret; 14 | } -------------------------------------------------------------------------------- /LeetCode/LC75.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void sortColors(vector& nums) { 4 | int n = nums.size(); 5 | for(int i = 0, j = 0, k = n-1; i <= k;){ 6 | if(nums[i] == 0) swap(nums[i++], nums[j++]); 7 | else if(nums[i] == 2) swap(nums[i], nums[k--]); 8 | else i++; 9 | } 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC235.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 4 | if(p->val > q->val) swap(p, q); 5 | if(p->val <= root->val && q->val >= root->val) return root; 6 | if(p->val <= root->val && q->val <= root->val) return lowestCommonAncestor(root->left, p, q); 7 | else return lowestCommonAncestor(root->right, p, q); 8 | } -------------------------------------------------------------------------------- /LeetCode/LC334.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool increasingTriplet(vector& nums) { 4 | vector q(2, INT_MAX); 5 | for(auto a : nums){ 6 | int k = 2; 7 | while(k > 0 && q[k-1] >= a) k--; 8 | if(k == 2) return true; 9 | q[k] = a; 10 | } 11 | return false; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC922.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParityII(vector& nums) { 4 | int n = nums.size(); 5 | for(int i = 0, j = 1; i < n; i += 2, j += 2) { 6 | while(i < n && nums[i] % 2 == 0) i += 2; 7 | while(j < n && nums[j] % 2 == 1) j += 2; 8 | if(i < n) swap(nums[i], nums[j]); 9 | } 10 | return nums; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC657.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool judgeCircle(string moves) { 4 | int h = 0, w = 0; 5 | for(auto c : moves) { 6 | if(c == 'U') h++; 7 | else if (c == 'D') h--; 8 | else if (c == 'L') w++; 9 | else if (c == 'R') w--; 10 | } 11 | return h == 0 && w == 0; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC836.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(int a, int b, int c, int d) { 4 | return a < b && c < d && b > c && d > a; 5 | } 6 | 7 | bool isRectangleOverlap(vector& rec1, vector& rec2) { 8 | return check(rec1[0], rec1[2], rec2[0], rec2[2]) 9 | && check(rec1[1], rec1[3], rec2[1], rec2[3]); 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC932.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector beautifulArray(int n) { 4 | if(n == 1) return {1}; 5 | auto left = beautifulArray((n+1) / 2); 6 | auto right = beautifulArray(n / 2); 7 | 8 | vector res; 9 | for(auto x : left) res.push_back(x * 2 - 1); 10 | for(auto y : right) res.push_back(y * 2); 11 | 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC944.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDeletionSize(vector& strs) { 4 | int n = strs.size(), m = strs[0].size(); 5 | int res = 0; 6 | for(int i = 0; i < m; i++) { 7 | for(int j = 1; j < n; j++) { 8 | if(strs[j][i] < strs[j-1][i]) { 9 | res++; 10 | break; 11 | }; 12 | } 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC405.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string toHex(unsigned int num) { 4 | if(!num) return "0"; 5 | string res; 6 | string t = "0123456789abcdef"; 7 | while(num) { 8 | res += t[num & 0xf]; 9 | num >>= 4; 10 | } 11 | reverse(res.begin(), res.end()); 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC451.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string frequencySort(string s) { 4 | unordered_map hash; 5 | for(auto c : s) hash[c]++; 6 | sort(s.begin(), s.end(), [&](char a, char b) { 7 | if(hash[a] != hash[b]) return hash[a] > hash[b]; 8 | return a < b; 9 | }); 10 | return s; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC520.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(char c){ 4 | return c >= 'A' && c <= 'Z'; 5 | } 6 | 7 | bool detectCapitalUse(string word) { 8 | int cnt = 0; 9 | for(auto c : word) { 10 | if(check(c)) cnt++; 11 | } 12 | return cnt == word.size() || !cnt || cnt == 1 && check(word[0]); 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC881.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRescueBoats(vector& people, int limit) { 4 | int res = 0; 5 | sort(people.begin(), people.end()); 6 | for(int i = 0, j = people.size()-1; i <= j; j--) { 7 | if(people[i] + people[j] <= limit) i++; 8 | res++; 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /ACwing/ac900.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | const int N = 1010, MOD = 1e9+7; 7 | int f[N]; 8 | int n; 9 | 10 | int main() { 11 | scanf("%d", &n); 12 | f[0] = 1; 13 | for(int i = 1; i <= n; i++) { 14 | for(int j = i; j <= n; j++) 15 | f[j] = (f[j] + f[j-i]) % MOD; 16 | } 17 | cout << f[n] << endl; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /LeetCode/LC230.cpp: -------------------------------------------------------------------------------- 1 | int n = 0; 2 | int ret = 0; 3 | void inorder(TreeNode *root, int k){ 4 | if(root == nullptr) return; 5 | inorder(root->left, k); 6 | if(++n == k){ 7 | ret = root->val; 8 | return; 9 | } 10 | inorder(root->right, k); 11 | } 12 | 13 | int kthSmallest(TreeNode* root, int k) { 14 | inorder(root, k); 15 | return ret; 16 | } -------------------------------------------------------------------------------- /LeetCode/LC303.cpp: -------------------------------------------------------------------------------- 1 | 2 | class NumArray { 3 | public: 4 | vector sums; 5 | NumArray(vector& nums) { 6 | int n = nums.size(); 7 | sums = vector (n+1, 0); 8 | for(int i = 1; i <= n; i++) sums[i] = sums[i-1] + nums[i-1]; 9 | } 10 | 11 | int sumRange(int i, int j) { 12 | return sums[j+1] - sums[i]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LeetCode/LC365.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int gcd(int x, int y) { 4 | return y ? gcd(y, x % y) : x; 5 | } 6 | bool canMeasureWater(int x, int y, int z) { 7 | if(x == 0 && y == 0){ 8 | return z == 0; 9 | } 10 | if(z > x + y) return false; 11 | auto g = gcd(x, y); 12 | return z % g == 0; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC481.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int magicalString(int n) { 4 | string s = "122"; 5 | for(int i = 2, k = 1; s.size() < n; i++, k = 3 - k) { 6 | for(int j = 0; j < s[i]-'0'; j++) s += to_string(k); 7 | } 8 | int res = 0; 9 | for(int i = 0; i < n; i++) res += s[i] == '1'; 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC5789.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int get(string s) { 4 | int h, m; 5 | sscanf(s.c_str(), "%d:%d", &h, &m); 6 | return h * 60 + m; 7 | } 8 | int numberOfRounds(string startTime, string finishTime) { 9 | int x = get(startTime), y = get(finishTime); 10 | if(x > y) y += 24 * 60; 11 | x = (x + 14) / 15, y /= 15; 12 | return y - x; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC780.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool reachingPoints(int sx, int sy, int tx, int ty) { 4 | while(tx > sx && ty > sy) { 5 | if(tx < ty) ty %= tx; 6 | else tx %= ty; 7 | } 8 | if(tx < sx || ty < sy) return false; 9 | if(sx == tx) return (ty-sy) % sx == 0; 10 | return (tx-sx) % sy == 0; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC945.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minIncrementForUnique(vector& nums) { 4 | int n = nums.size(), cur = 0, res = 0; 5 | sort(nums.begin(), nums.end()); 6 | for(int i = 0; i < n; i++) { 7 | if(nums[i] < cur) { 8 | res += cur - nums[i]; 9 | } else { 10 | cur = nums[i]; 11 | } 12 | cur++; 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC258.cpp: -------------------------------------------------------------------------------- 1 | 2 | int addDigits(int num) { 3 | while(num >= 10){ 4 | int t = 0; 5 | while(num > 0){ 6 | t = t + num % 10; 7 | num /= 10; 8 | } 9 | num = t; 10 | } 11 | return num; 12 | } 13 | 14 | int addDigits(int num){ 15 | if(num == 0) return num; 16 | if(num % 9) return num % 9; 17 | return 9; 18 | } -------------------------------------------------------------------------------- /LeetCode/LC38.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) { 4 | string s = "1"; 5 | for(int i = 1; i < n; i++) { 6 | string t; 7 | for(int j = 0; j < s.size(); ) { 8 | int k = j + 1; 9 | while(k < s.size() && s[j] == s[k]) k++; 10 | t += to_string(k - j) + s[j]; 11 | j = k; 12 | } 13 | s = t; 14 | } 15 | return s; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC704.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int search(vector& nums, int target) { 4 | int l = 0, r = nums.size() -1; 5 | while(l < r) { 6 | int mid = (l + r) >> 1; 7 | if(nums[mid] >= target) r = mid; 8 | else l = mid + 1; 9 | } 10 | if(nums[r] != target) return -1; 11 | return r; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC905.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector sortArrayByParity(vector& nums) { 4 | int i = 0, j = nums.size() - 1; 5 | while(i < j) { 6 | while(i < j && nums[i] % 2 == 0) i++; 7 | while(i < j && nums[j] % 2 == 1) j--; 8 | if(i < j) swap(nums[i], nums[j]); 9 | } 10 | return nums; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC940.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int distinctSubseqII(string s) { 4 | const int MOD = 1e9 + 7; 5 | int f[26] = {}; 6 | for(auto c : s) { 7 | int x = c - 'a', s = 1; 8 | for(int i = 0; i < 26; i++) 9 | s = (s + f[i]) % MOD; 10 | f[x] = s; 11 | } 12 | int res = 0; 13 | for(int x : f) res = (res + x) % MOD; 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC946.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validateStackSequences(vector& pushed, vector& popped) { 4 | stack st; 5 | int k = 0; 6 | int n = popped.size(); 7 | for(auto x : pushed) { 8 | st.push(x); 9 | while(st.size() && k < n && st.top() == popped[k]) { 10 | st.pop(); 11 | k++; 12 | } 13 | } 14 | return k == n; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC357.cpp: -------------------------------------------------------------------------------- 1 | class solution{ 2 | public: 3 | int countNumbersWithUniqueDigits(int n) { 4 | int (!n) return 1; 5 | n = min(n, 10); 6 | vector f(n); 7 | f[0] = 9; 8 | for(int i = 1; i < n; i++) f[i] = f[i-1] * (10 - i); 9 | int res = 0; 10 | for(int i = 0; i < n; i++) res += f[i]; 11 | return res + 1; 12 | } 13 | } -------------------------------------------------------------------------------- /LeetCode/LC551.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkRecord(string s) { 4 | bool L = false; 5 | for(int i = 0, A = 0; i < s.size(); i++) { 6 | if(s[i] == 'A') A++; 7 | else if (i >= 2 && s[i] == 'L' && s[i-1] == 'L' && s[i-2] == 'L') L = true; 8 | if(A > 1 || L) return false; 9 | } 10 | return true; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC832.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> flipAndInvertImage(vector>& image) { 4 | int n = image.size(), m = image[0].size(); 5 | for(int i = 0; i < n; i++) { 6 | reverse(image[i].begin(), image[i].end()); 7 | for(int j = 0; j < m; j++) image[i][j] ^= 1; 8 | } 9 | return image; 10 | } 11 | }; -------------------------------------------------------------------------------- /LeetCode/LC858.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int gcd(int a, int b){ 4 | return b ? gcd(b, a % b) :a; 5 | } 6 | int mirrorReflection(int p, int q) { 7 | int Y = p * q / gcd(p, q); 8 | int x = Y / q, y = Y / p; 9 | if(x % 2) { 10 | if(y % 2) return 1; 11 | return 0; 12 | } 13 | return 2; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC898.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarrayBitwiseORs(vector& arr) { 4 | unordered_set res, f, g; 5 | for(auto x : arr) { 6 | g.insert(x); 7 | for(auto y : f) g.insert(x | y); 8 | for(auto y : g) res.insert(y); 9 | f = g, g.clear(); 10 | } 11 | return res.size(); 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC204.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | int countPrimes(int n){ 5 | vector primes; 6 | vector st(n+1); 7 | for(int i = 2; i < n; i++) { 8 | if(!st[i]) primes.push_back(i); 9 | for(int j = 0; i * primes[j] < n; j++) { 10 | st[i * primes[j]] = true; 11 | if(i % primes[j] == 0) break; 12 | } 13 | } 14 | return primes.size(); 15 | } -------------------------------------------------------------------------------- /LeetCode/LC781.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numRabbits(vector& answers) { 4 | unordered_map hash; 5 | for(int x : answers) hash[x]++; 6 | int res = 0; 7 | for(auto [k, v] : hash) { 8 | if(v % (k + 1) == 0) res += v; 9 | else res += (v / (k+1) + 1) * (k + 1); 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /ACwing/ac77.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | int n = s.size(); 5 | reverse(s.begin(), s.end()); 6 | for(int i = 0; i < n; i++) { 7 | int j = i; 8 | while(j < n && s[j] != ' ') j++; 9 | reverse(s.begin() + i, s.begin() + j); 10 | i = j; 11 | } 12 | return s; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC643.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double findMaxAverage(vector& nums, int k) { 4 | double res = -1e5; 5 | for(int i = 0, j = 0, sum = 0; i < nums.size(); i++) { 6 | sum += nums[i]; 7 | if(i - j + 1 > k) sum -= nums[j++]; 8 | if(i >= k-1) res = max(res, sum / (double)k); 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC762.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countPrimeSetBits(int L, int R) { 4 | unordered_set primes{2, 3, 5, 7, 11, 13, 17, 19}; 5 | int res = 0; 6 | for(int i = L; i <= R; i++) { 7 | int s = 0; 8 | for(int j = i; j; j >>= 1) s += j & 1; 9 | res += primes.count(s); 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC330.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minPatches(vector& nums, int n) { 4 | long long x = 1; 5 | int i = 0, res = 0; 6 | while(x <= n){ 7 | if(i < nums.size() && nums[i] <= x) x += nums[i++]; 8 | else { 9 | x += x; 10 | res++; 11 | } 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC383.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canConstruct(string ransomNote, string magazine) { 4 | vector hash(26, 0); 5 | for(auto c : magazine) { 6 | hash[c - 'a']++; 7 | } 8 | for(auto c : ransomNote){ 9 | if(hash[c-'a'] == 0) return false; 10 | hash[c-'a']--; 11 | } 12 | return true; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC434.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSegments(string s) { 4 | int res = 0; 5 | int n = s.size(); 6 | for(int i = 0; i < n; i++) { 7 | if(s[i] == ' ') continue; 8 | int j = i + 1; 9 | while(j < n && s[j] != ' ') j++; 10 | res++; 11 | i = j - 1; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC674.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLengthOfLCIS(vector& nums) { 4 | int maxl = 0, st = 0; 5 | int n = nums.size(); 6 | for(int i = 1; i < n; i++) { 7 | if(nums[i] <= nums[i-1]){ 8 | maxl = max(maxl, i - st); 9 | st = i; 10 | } 11 | } 12 | return max(maxl, n - st); 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC747.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int dominantIndex(vector& nums) { 4 | int n = nums.size(); 5 | int k = 0; 6 | for(int i = 1; i < n; i++) { 7 | if(nums[i] > nums[k]) k = i; 8 | } 9 | for(int i = 0; i < n; i++) { 10 | if(i != k && nums[k] < nums[i] * 2) return -1; 11 | } 12 | return k; 13 | } 14 | }; -------------------------------------------------------------------------------- /daily/104.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int N = 100010; 6 | 7 | 8 | int a[N]; 9 | int n; 10 | 11 | 12 | 13 | int main() { 14 | cin >> n; 15 | for(int i = 0; i < n; i++) cin >> a[i]; 16 | sort(a, a+n); 17 | int res = 0; 18 | for(int i = 0; i < n; i++) res += abs(a[i] - a[n / 2]); 19 | cout << res; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /LeetCode/LC14.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestCommonPrefix(vector& strs) { 4 | string res; 5 | if(strs.empty()) return res; 6 | int i = 0, n = strs[0].size(); 7 | while(i < n) { 8 | char c = strs[0][i]; 9 | for(auto str : strs) { 10 | if(str.size() < i || str[i] != c) return res; 11 | } 12 | res += c; 13 | i++; 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC386.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector res; 4 | vector lexicalOrder(int n) { 5 | for(int i = 1; i <= 9; i++) dfs(i, n); 6 | return res; 7 | } 8 | 9 | void dfs(int c , int n){ 10 | if(c <= n) res.push_back(c); 11 | else return; 12 | for(int i = 0; i <= 9; i++){ 13 | dfs(c* 10 + i, n); 14 | } 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC41.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& nums) { 4 | int n = nums.size(); 5 | for(int i = 0; i < n; i++) { 6 | while(nums[i] > 0 && nums[i] <= n && nums[nums[i]-1] != nums[i]) { 7 | swap(nums[nums[i]-1], nums[i]); 8 | } 9 | } 10 | for(int i = 0; i < n; i++) { 11 | if(nums[i] != i+1) return i+1; 12 | } 13 | return n + 1; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC477.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int totalHammingDistance(vector& nums) { 4 | int res = 0; 5 | for(int i = 0; i < 31; i++){ 6 | int x = 0, y = 0; 7 | for(auto n : nums) { 8 | if(n >> i & 1) y++; 9 | else x++; 10 | } 11 | res += x * y; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC507.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkPerfectNumber(int num) { 4 | if(num == 1) return false; 5 | int total = 0; 6 | for(int i = 2; i * i <= num; i++) { 7 | if(num % i == 0) { 8 | total += i; 9 | if(i * i != num) total += num / i; 10 | } 11 | } 12 | return total + 1 == num; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC540.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int singleNonDuplicate(vector& nums) { 4 | nums.push_back(nums.back()+1); 5 | int l = 0, r = nums.size() / 2 - 1; 6 | while(l < r) { 7 | int mid = l + r >> 1; 8 | if(nums[mid * 2] != nums[mid * 2 + 1]) r = mid; 9 | else l = mid + 1; 10 | } 11 | return nums[r * 2]; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC557.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseWords(string s) { 4 | int n = s.size(); 5 | for(int i = 0; i < n; i++) { 6 | if(s[i] == ' ') continue; 7 | int j = i ; 8 | while(j < n && s[j] != ' ') j++; 9 | reverse(s.begin() + i, s.begin() + j); 10 | i = j - 1; 11 | } 12 | return s; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC667.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector constructArray(int n, int k) { 4 | vector res(n); 5 | for(int i = 0; i < n - k - 1; i++) res[i] = i+1; 6 | int u = n - k -1 ; 7 | int i = n - k, j = n; 8 | while(u < n) { 9 | res[u++] = i++; 10 | if(u < n) res[u++] = j--; 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC179.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | string largesNumber(vector &nums) { 5 | sort(nums.begin(), nums.end(), [](int x, int y){ 6 | string a = to_string(x), b = to_string(y); 7 | return a + b > b + a; 8 | }); 9 | string res; 10 | for(auto n : nums) res += to_string(n); 11 | int k = 0, n = nums.size(); 12 | while(k+1 < n && res[k] == '0') k++; 13 | return res.substr(k); 14 | } -------------------------------------------------------------------------------- /LeetCode/LC219.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | bool containsNearbyDuplicate(vector& nums, int k) { 4 | unordered_map hash; 5 | int n = nums.size(); 6 | for(int i = 0; i < n; i++){ 7 | if(hash.count(nums[i]) >= 1){ 8 | if (abs(i - hash[nums[i]]) <= k) { 9 | return true; 10 | } 11 | } 12 | hash[nums[i]] = i; 13 | } 14 | return false; 15 | } -------------------------------------------------------------------------------- /LeetCode/LC5802.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | typedef long long LL; 4 | const int MOD = 1e9 + 7; 5 | 6 | LL qmi(LL a, LL b) { 7 | LL res = 1 % MOD; 8 | while(b) { 9 | if(b & 1) res = res * a % MOD; 10 | a = a * (LL)a % MOD; 11 | b >>= 1; 12 | } 13 | return res; 14 | } 15 | 16 | int countGoodNumbers(long long n) { 17 | return qmi(5, (n+1) / 2) * qmi(4, n / 2) % MOD; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC848.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string shiftingLetters(string s, vector& shifts) { 4 | int n = s.size(); 5 | string res; 6 | for(int i = n - 1, sum = 0; i >= 0; i--) { 7 | sum = (sum +shifts[i]) % 26; 8 | res += (s[i] - 'a' + sum) % 26 + 'a'; 9 | } 10 | reverse(res.begin(), res.end()); 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC141.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | bool hasCycle(ListNode *head){ 6 | if(head == nullptr || head->next == nullptr) return false; 7 | ListNode *slow = head, *fast = slow->next; 8 | while(slow != fast){ 9 | if(fast->next && fast->next->next) fast = fast->next->next; 10 | else{ 11 | return false; 12 | } 13 | slow = slow->next; 14 | } 15 | return true; 16 | } -------------------------------------------------------------------------------- /LeetCode/LC448.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findDisappearedNumbers(vector& nums) { 4 | for(auto x : nums) { 5 | x = abs(x); 6 | if(nums[x-1] > 0) nums[x-1] *= -1; 7 | } 8 | vector res; 9 | for(int i = 0; i < nums.size(); i++) { 10 | if(nums[i] > 0) res.push_back(i+1); 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC899.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string orderlyQueue(string s, int k) { 4 | if(k == 1) { 5 | string res = s; 6 | for(int i = 0; i < s.size(); i++) { 7 | s = s.substr(1) + s[0]; 8 | res = min(res, s); 9 | } 10 | return res; 11 | } 12 | sort(s.begin(), s.end()); 13 | return s; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC22.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector res; 4 | 5 | void dfs(int n, int ln, int rn, string path) { 6 | if(ln == n && rn == n) res.push_back(path); 7 | else { 8 | if(ln < n) dfs(n, ln+1, rn, path+'('); 9 | if(rn < n && rn < ln) dfs(n, ln, rn+1, path+')'); 10 | } 11 | } 12 | 13 | vector generateParenthesis(int n) { 14 | dfs(n, 0, 0, ""); 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC397.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | typedef long long LL; 4 | unordered_map hash; 5 | int integerReplacement(int n) { 6 | return dfs(n); 7 | } 8 | int dfs(LL n) { 9 | if(hash.count(n)) return hash[n]; 10 | if(n == 1) return 0; 11 | if(n % 2 == 0) return hash[n] = dfs(n / 2) + 1; 12 | return hash[n] = min(dfs(n+1), dfs(n-1)) + 1; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC738.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int monotoneIncreasingDigits(int N) { 4 | string str = to_string(N); 5 | int k = 0, n = str.size(); 6 | while(k+1 < n && str[k] <= str[k+1])k++; 7 | if(k == n-1) return N; 8 | while(k && str[k-1] == str[k]) k--; 9 | str[k]--; 10 | for(int i = k+1; i < n; i++) str[i] = '9'; 11 | return stoi(str); 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC744.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char nextGreatestLetter(vector& letters, char target) { 4 | int l = 0, r = letters.size() - 1; 5 | while(l < r) { 6 | int mid = l + r >> 1; 7 | if(letters[mid] > target) r = mid; 8 | else l = mid + 1; 9 | } 10 | if(letters[r] > target) return letters[r]; 11 | return letters[0]; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC94.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | vector inorderTraversal(TreeNode *root){ 4 | stack st; 5 | vector res; 6 | while(root || st.size()) { 7 | while(root) { 8 | st.push(root); 9 | root = root->left; 10 | } 11 | root = st.top(); 12 | st.pop(); 13 | res.push_back(root->val); 14 | root = root->right; 15 | } 16 | return res; 17 | } -------------------------------------------------------------------------------- /LeetCode/LC416.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canPartition(vector& nums) { 4 | int sum = 0; 5 | for(auto x : nums) sum += x; 6 | if (sum % 2) return false; 7 | sum /= 2; 8 | vector f(sum + 1); 9 | f[0] = 1; 10 | for(auto x : nums) { 11 | for(int j = sum; j >= x; j--) f[j] |= f[j-x]; 12 | } 13 | return f[sum]; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC678.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkValidString(string s) { 4 | int low = 0, high = 0; 5 | for (auto c: s) { 6 | if (c == '(') low ++, high ++ ; 7 | else if (c == ')') low -- , high -- ; 8 | else low --, high ++ ; 9 | low = max(low, 0); 10 | if (low > high) return false; 11 | } 12 | return !low; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC867.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> transpose(vector>& matrix) { 4 | int n = matrix.size(), m = matrix[0].size(); 5 | vector> res(m, vector(n, 0)); 6 | for(int i = 0; i s(n + 1); 6 | for (int i = 1; i <= n; i ++ ) 7 | s[i] = s[i - 1] + str[i - 1] - '0'; 8 | 9 | int res = n - s[n]; 10 | for (int i = 1; i <= n; i ++ ) 11 | res = min(res, s[i] + n - i - (s[n] - s[i])); 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC933.cpp: -------------------------------------------------------------------------------- 1 | class RecentCounter { 2 | public: 3 | queue q; 4 | RecentCounter() { 5 | 6 | } 7 | 8 | int ping(int t) { 9 | q.push(t); 10 | while(t - q.front() > 3000) q.pop(); 11 | return q.size(); 12 | } 13 | }; 14 | 15 | /** 16 | * Your RecentCounter object will be instantiated and called as such: 17 | * RecentCounter* obj = new RecentCounter(); 18 | * int param_1 = obj->ping(t); 19 | */ -------------------------------------------------------------------------------- /LeetCode/LC167.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | vector twoSum(vector &numbers, int target) { 4 | int n = numbers.size(); 5 | vector ret; 6 | for(int i = 0, j = n-1; i < j; i++) { 7 | while(numbers[i] + numbers[j] > target) j--; 8 | if(numbers[i] + numbers[j] == target){ 9 | ret.push_back(i+1); 10 | ret.push_back(j+1); 11 | break; 12 | } 13 | } 14 | return ret; 15 | } -------------------------------------------------------------------------------- /LeetCode/LC349.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersection(vector& nums1, vector& nums2) { 4 | unordered_set s; 5 | vector ret; 6 | for(auto n : nums1) s.insert(n); 7 | for(auto n : nums2){ 8 | if(s.count(n)){ 9 | ret.push_back(n); 10 | s.erase(n); 11 | } 12 | } 13 | return ret; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC49.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> groupAnagrams(vector& strs) { 4 | unordered_map> hash; 5 | for(auto &s : strs) { 6 | string nstr = s; 7 | sort(nstr.begin(), nstr.end()); 8 | hash[nstr].push_back(s); 9 | } 10 | 11 | vector> res; 12 | for(auto [k, v] : hash) { 13 | res.push_back(v); 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC566.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> matrixReshape(vector>& nums, int r, int c) { 4 | int n = nums.size(), m = nums[0].size(); 5 | if(n * m != r * c) return nums; 6 | vector> res(r, vector(c)); 7 | for(int i = 0; i < n * m; i++) { 8 | res[i / c][i % c] = nums[i / m][i % m]; 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /LeetCode/LC830.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> largeGroupPositions(string s) { 4 | vector> res; 5 | int n = s.size(); 6 | for(int i = 0; i < n; i++) { 7 | int j = i + 1; 8 | while(j < n && s[i] == s[j]) j++; 9 | if((j - i) >= 3) res.push_back({i, j-1}); 10 | i = j - 1; 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC844.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string get(string& s) { 4 | string res; 5 | for (auto c: s) 6 | if (c == '#') { 7 | if (res.size()) res.pop_back(); 8 | } else { 9 | res += c; 10 | } 11 | return res; 12 | } 13 | 14 | bool backspaceCompare(string s, string t) { 15 | return get(s) == get(t); 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC914.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int gcd(int a, int b) { 4 | return b ? gcd(b, a % b) : a; 5 | } 6 | 7 | bool hasGroupsSizeX(vector& deck) { 8 | unordered_map cnt; 9 | for(auto x : deck) cnt[x]++; 10 | int d = 0; 11 | for(auto [k, v] : cnt) d = gcd(d, v); 12 | return d >= 2; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC930.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSubarraysWithSum(vector& nums, int goal) { 4 | int n = nums.size(); 5 | vector sum(n+1, 0), f(n+1, 0); 6 | for(int i = 1; i <= n; i++) sum[i] = sum[i-1] + nums[i-1]; 7 | f[0] = 1; 8 | int res = 0; 9 | for(int i = 1; i <= n; i++) { 10 | int s = sum[i]; 11 | if(s >= goal) res += f[s - goal]; 12 | f[s]++; 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /ACwing/ac2_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | const int N = 10010; 7 | 8 | int n,m; 9 | int v[N], w[N]; 10 | int f[N]; 11 | 12 | 13 | int main() { 14 | cin >> n >> m; 15 | for(int i = 1; i <= n; i++) cin >> v[i] >> w[i]; 16 | 17 | for(int i = 1; i <= n; i++) { 18 | for(int j = m; j >= v[i]; j--) 19 | f[j] = max(f[j], f[j-v[i]] + w[i]); 20 | } 21 | cout << f[m] << endl; 22 | } -------------------------------------------------------------------------------- /LeetCode/LC240.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | bool searchMatrix(vector>& matrix, int target) { 4 | if (matrix.empty() || matrix[0].empty()) return false; 5 | int row = matrix.size(), col = matrix[0].size(); 6 | for(int i = 0, j = col-1; i < row && j >= 0;){ 7 | int t = matrix[i][j]; 8 | if (t == target) return true; 9 | else if (t > target) j -- ; 10 | else i ++ ; 11 | } 12 | return false; 13 | } -------------------------------------------------------------------------------- /LeetCode/LC48.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotate(vector>& matrix) { 4 | int n = matrix.size(); 5 | // 对角线对称 6 | for(int i = 0; i < n; i++) { 7 | for(int j = 0; j < i; j++) 8 | swap(matrix[i][j], matrix[j][i]); 9 | } 10 | // 竖直反转 11 | for(int i = 0; i < n; i++) { 12 | for(int j = 0, k = n - 1; j < k; j++, k--) 13 | swap(matrix[i][j], matrix[i][k]); 14 | } 15 | return; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC485.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxConsecutiveOnes(vector& nums) { 4 | int res = 0; 5 | int n = nums.size(); 6 | for(int i = 0; i < n; i++) { 7 | if(nums[i] == 0) continue; 8 | int j = i + 1; 9 | while(j < n && nums[j] == 1) j++; 10 | res = max(res, j - i); 11 | i = j; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC739.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector dailyTemperatures(vector& T) { 4 | int n = T.size(); 5 | stack stk; 6 | vector res(n); 7 | for(int i = n-1; i >= 0; i--) { 8 | while(stk.size() && T[i] >= T[stk.top()]) stk.pop(); 9 | if(stk.size()) res[i] = stk.top() - i; 10 | stk.push(i); 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC915.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int partitionDisjoint(vector& nums) { 4 | int n = nums.size(); 5 | vector r(n, nums[n-1]); 6 | for(int i = n - 2; i >= 0; i--) r[i] = min(nums[i], r[i+1]); 7 | 8 | for(int i = 0, l = INT_MIN; i < n-1; i++) { 9 | l = max(l, nums[i]); 10 | if(l <= r[i+1]) return i+1; 11 | } 12 | return -1; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC917.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string reverseOnlyLetters(string s) { 4 | for(int i = 0, j = s.size() - 1; i < j; i++, j--) { 5 | while(i < j && !(s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z')) i++; 6 | while(i < j && !(s[j] >= 'a' && s[j] <= 'z' || s[j] >= 'A' && s[j] <= 'Z')) j--; 7 | if(i < j) swap(s[i], s[j]); 8 | } 9 | return s; 10 | } 11 | }; -------------------------------------------------------------------------------- /ACwing/ac3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | const int N = 1010; 8 | int n, m; 9 | int v[N], w[N], f[N]; 10 | 11 | 12 | int main() { 13 | cin >> n >> m; 14 | for(int i = 1; i <= n; i++) cin >> v[i] >> w[i]; 15 | 16 | for(int i = 1; i <= n; i++) { 17 | for(int j = v[i]; j <= m; j++) 18 | f[j] = max(f[j], f[j-v[i]] + w[i]); 19 | } 20 | cout << f[m] << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /LeetCode/LC239.cpp: -------------------------------------------------------------------------------- 1 | 2 | vector maxSlidingWindow(vector& nums, int k) { 3 | deque q; 4 | vector ret; 5 | int n = nums.size(); 6 | for(int i = 0; i < n; i++){ 7 | if(q.size() && i - k + 1 > q.front()) q.pop_front(); 8 | while(q.size() && nums[i] >= nums[q.back()]) q.pop_back(); 9 | q.push_back(i); 10 | if(i >= k - 1) ret.push_back(nums[q.front()]); 11 | } 12 | return ret; 13 | } -------------------------------------------------------------------------------- /LeetCode/LC31.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void nextPermutation(vector& nums) { 4 | int k = nums.size() - 1; 5 | while(k > 0 && nums[k-1] >= nums[k]) k--; 6 | if(k <= 0) { 7 | reverse(nums.begin(), nums.end()); 8 | } else { 9 | int t = k; 10 | while(t < nums.size() && nums[t] > nums[k-1]) t++; 11 | swap(nums[t-1], nums[k-1]); 12 | reverse(nums.begin() + k, nums.end()); 13 | } 14 | return; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC350.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersect(vector& nums1, vector& nums2) { 4 | unordered_multiset s; 5 | vector res; 6 | for(auto n : nums1) s.insert(n); 7 | for(auto n : nums2){ 8 | if(s.count(n)){ 9 | res.push_back(n); 10 | s.erase(s.find(n)); 11 | } 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC455.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findContentChildren(vector& g, vector& s) { 4 | sort(g.begin(), g.end()); 5 | sort(s.begin(), s.end()); 6 | int res = 0; 7 | for(int i = 0, j = 0; i < g.size() && j < s.size(); j++) { 8 | if(g[i] <= s[j]) { 9 | res++; 10 | i++; 11 | } 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC611.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int triangleNumber(vector& nums) { 4 | sort(nums.begin(), nums.end()); 5 | int res = 0; 6 | for(int i = 0; i < nums.size(); i++) { 7 | for(int j = i-1, k = 0; j > k; j--) { 8 | while(j > k && nums[k] + nums[j] <= nums[i]) k++; 9 | res += j - k; 10 | } 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC483.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | #define LL long long 4 | string smallestGoodBase(string n) { 5 | LL N = stoll(n); 6 | for(int t = log2(N) + 1; t >= 3; t--) { 7 | LL k = pow(N, 1.0 / (t-1)); 8 | LL tot = 0; 9 | for(int i = 0; i < t; i++) tot = tot * k + 1; 10 | if(tot == N) return to_string(k); 11 | } 12 | return to_string(N-1); 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC920.cpp: -------------------------------------------------------------------------------- 1 | typedef long long LL; 2 | const int N = 210, MOD = 1e9 + 7; 3 | 4 | int f[N][N]; 5 | 6 | class Solution { 7 | public: 8 | int numMusicPlaylists(int n, int m, int k) { 9 | f[0][0] = 1; 10 | for(int i = 1; i <= m; i++) { 11 | for(int j = 1; j <= n && j <= i; j++) { 12 | f[i][j] = ((LL)f[i-1][j-1] * (n - j + 1) + 13 | (LL)f[i-1][j] * max(j-k, 0)) % MOD; 14 | } 15 | } 16 | return f[m][n]; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC153.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | 8 | int findMin(vector &nums) { 9 | int l = 0, r = nums.size()- 1; 10 | if(nums[r] > nums[l]) return nums[0]; 11 | while(l < r){ 12 | int mid = l + r >> 1; 13 | if(nums[mid] > nums[0]) { 14 | l = mid + 1; 15 | } else { 16 | r = mid; 17 | } 18 | } 19 | return nums[l]; 20 | } -------------------------------------------------------------------------------- /LeetCode/LC205.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | bool isIsomorphic(string s, string t) { 4 | int sn = s.size(), tn = t.size(); 5 | if(sn != tn) return false; 6 | unordered_map st, ts; 7 | for(int i = 0; i < sn; i++) { 8 | int a = s[i], b = t[i]; 9 | if(st.count(a) && st[a] != b) return false; 10 | st[a] = b; 11 | if(ts.count(b) && ts[b] != a) return false; 12 | ts[b] = a; 13 | } 14 | return true; 15 | } -------------------------------------------------------------------------------- /LeetCode/LC789.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // 曼哈顿距离 4 | int get_dist(int x1, int y1, int x2, int y2) { 5 | return abs(x1 - x2) + abs(y1 - y2); 6 | } 7 | bool escapeGhosts(vector>& ghosts, vector& target) { 8 | for(auto& g : ghosts) { 9 | if(get_dist(g[0], g[1], target[0], target[1]) <= abs(target[0]) + abs(target[1])) return false; 10 | } 11 | return true; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC869.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(int a, int b){ 4 | unordered_multiset s1, s2; 5 | for(auto c : to_string(a)) s1.insert(c); 6 | for(auto c : to_string(b)) s2.insert(c); 7 | return s1 == s2; 8 | } 9 | bool reorderedPowerOf2(int n) { 10 | for(int i = 1; i < 1e9; i *= 2){ 11 | if(check(i, n)) return true; 12 | } 13 | return false; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC209.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | int minSubArrayLen(int s, vector& nums) { 5 | int res = INT_MAX; 6 | int n = nums.size(); 7 | int sum = 0; 8 | for(int i = 0, j = 0; i < n; i++) { 9 | sum += nums[i]; 10 | while(sum - nums[j] >= s) { 11 | sum -= nums[j]; 12 | j++; 13 | } 14 | if(sum >= s) res = min(res, i - j + 1); 15 | } 16 | if(res == INT_MAX) return 0; 17 | return res; 18 | } -------------------------------------------------------------------------------- /LeetCode/LC459.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool repeatedSubstringPattern(string s) { 4 | int n = s.size(); 5 | s = ' ' + s; 6 | vector next(n+1); 7 | for(int i = 2, j = 0; i <= n; i++){ 8 | while(j && s[i] != s[j+1]) j = next[j]; 9 | if(s[i] == s[j+1]) j++; 10 | next[i] = j; 11 | } 12 | int t = n - next[n]; 13 | return t < n && n % t == 0; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC525.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxLength(vector& nums) { 4 | int n = nums.size(), ans = 0; 5 | unordered_map hash; 6 | hash[0] = 0; 7 | int x = 0; 8 | for(int i = 1; i <= n; i++) { 9 | x += (nums[i-1] == 1 ? 1 : -1); 10 | if(hash.count(x)) ans = max(ans, i - hash[x]); 11 | else hash[x] = i; 12 | } 13 | return ans; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC66.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector plusOne(vector& digits) { 4 | int n = digits.size(); 5 | if(n < 1) return {}; 6 | vector res; 7 | int t = 0; 8 | for(int i = n - 1; i >= 0; i--) { 9 | if(i == n - 1) t += digits[i] + 1; 10 | else t += digits[i]; 11 | res.push_back(t % 10); 12 | t /= 10; 13 | } 14 | if(t) res.push_back(t); 15 | reverse(res.begin(), res.end()); 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC345.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string s = "aeiou"; 4 | 5 | bool check(char c) { 6 | return s.find(tolower(c)) != -1; 7 | } 8 | 9 | string reverseVowels(string s) { 10 | for (int i = 0, j = s.size() - 1; i < j; i ++ , j -- ) { 11 | while (i < j && !check(s[i])) i ++ ; 12 | while (i < j && !check(s[j])) j -- ; 13 | swap(s[i], s[j]); 14 | } 15 | return s; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC42.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int trap(vector& height) { 4 | stack stk; 5 | int res = 0; 6 | for(int i = 0; i < height.size(); i++) { 7 | while(stk.size() && height[stk.top()] <= height[i]) { 8 | int t = stk.top(); 9 | stk.pop(); 10 | if(stk.empty()) break; 11 | res += (i - stk.top() - 1) * (min(height[stk.top()], height[i]) - height[t]); 12 | } 13 | stk.push(i); 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC888.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector fairCandySwap(vector& a, vector& b) { 4 | int s1 = accumulate(a.begin(), a.end(), 0); 5 | int s2 = accumulate(b.begin(), b.end(), 0); 6 | unordered_set S; 7 | for(auto x : b) S.insert(x); 8 | int t = (s1 - s2) / 2; 9 | for(auto x : a) { 10 | if(S.count(x - t)) return {x, x-t}; 11 | } 12 | return {}; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC154.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | 8 | 9 | int findMind(vector &nums) { 10 | int n = nums.size(); 11 | int l = 0, r = n - 1; 12 | while(l < r && nums[r] == nums[0]) r--; 13 | if(nums[r] > nums[l]) return nums[0]; 14 | while(l < r){ 15 | int mid = l + r >> 1; 16 | if (nums[mid] < nums[0]) r = mid; 17 | else l = mid + 1; 18 | } 19 | return nums[r]; 20 | } -------------------------------------------------------------------------------- /LeetCode/LC532.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findPairs(vector& nums, int k) { 4 | sort(nums.begin(), nums.end()); 5 | int n = nums.size(); 6 | int res = 0; 7 | for(int i = 0, j = 0; i < n;i++) { 8 | while(i + 1 < n && nums[i] == nums[i+1]) i++; 9 | while(j < i & nums[i] - nums[j] > k) j++; 10 | if(i > j && nums[i] - nums[j] == k) res++; 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC696.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBinarySubstrings(string s) { 4 | int res = 0; 5 | int last = 0; 6 | int n = s.size(); 7 | for(int i = 0; i < n; i++) { 8 | int j = i + 1; 9 | while(j < n && s[i] == s[j]) j++; 10 | int cur = j - i; 11 | i = j-1; 12 | res += min(last, cur); 13 | last = cur; 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC728.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(int num) { 4 | for(auto c : to_string(num)) { 5 | if(c == '0' || num % (c - '0')) return false; 6 | } 7 | return true; 8 | } 9 | vector selfDividingNumbers(int left, int right) { 10 | vector res; 11 | for(int i = left; i <= right; i++) { 12 | if(check(i)) res.push_back(i); 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC646.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLongestChain(vector>& pairs) { 4 | sort(pairs.begin(), pairs.end(), [](vector &a, vector &b) { 5 | return a[1] < b[1]; 6 | }); 7 | int res = 1, ed = pairs[0][1]; 8 | for(auto p : pairs) { 9 | if(p[0] > ed) { 10 | res++; 11 | ed = p[1]; 12 | } 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC413.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numberOfArithmeticSlices(vector& nums) { 4 | int n = nums.size(), res = 0; 5 | for(int i = n - 1; i > 0; i--) nums[i] -= nums[i-1]; 6 | for(int i = 1; i < n; i++) { 7 | int j = i; 8 | while(j < n && nums[j] == nums[i]) j++; 9 | int k = j - i; 10 | res += (k - 1) * k / 2; 11 | i = j - 1; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC456.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool find132pattern(vector& nums) { 4 | stack st; 5 | int two = INT_MIN; 6 | for(int i = nums.size()-1; i >= 0; i--) { 7 | if(nums[i] < two) return true; 8 | while(st.size() && nums[i] > st.top()) { 9 | two = st.top(); 10 | st.pop(); 11 | } 12 | st.push(nums[i]); 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC904.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int totalFruit(vector& tree) { 4 | int res = 0; 5 | unordered_map cnt; 6 | for(int i = 0, j = 0, s = 0; i < tree.size();i++) { 7 | if(++cnt[tree[i]] == 1) s++; 8 | while(s > 2) { 9 | if(--cnt[tree[j]] == 0) s--; 10 | j++; 11 | } 12 | res = max(res, i - j + 1); 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC910.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestRangeII(vector& nums, int k) { 4 | int n = nums.size(); 5 | sort(nums.begin(), nums.end()); 6 | int res = nums[n-1] - nums[0]; 7 | for(int i = 0; i < n- 1; i++) { 8 | int minw = min(nums[0] + k, nums[i+1] - k); 9 | int maxw = max(nums[i] + k, nums[n-1] - k); 10 | res = min(res, maxw - minw); 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC275.cpp: -------------------------------------------------------------------------------- 1 | 2 | int hIndex(vector& c) { 3 | int n = c.size(); 4 | for(int h = 0; h < n; h++){ 5 | if(n - h <= c[h]){ 6 | return n - h; 7 | } 8 | } 9 | return 0; 10 | } 11 | 12 | 13 | int hIndex(vector &c) { 14 | int n = c.size(); 15 | int l = 0, r = n; 16 | while(l < r) { 17 | int mid = l + r +1 >> 1; 18 | if(c[n-mid] >= mid) l = mid; 19 | else r = mid - 1; 20 | } 21 | return r; 22 | } -------------------------------------------------------------------------------- /LeetCode/LC414.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int thirdMax(vector& nums) { 4 | int cnt = 0; 5 | long long INF = 1e10; 6 | long long a = -INF, b = -INF, c = -INF; 7 | for(auto x : nums) { 8 | if(x > a) c = b, b = a, a = x, cnt++; 9 | else if (x > b && x < a) c = b, b = x, cnt++; 10 | else if (x > c && x < b) c = x, cnt++; 11 | } 12 | if (cnt < 3) return a; 13 | return c; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC419.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countBattleships(vector>& board) { 4 | int res = 0; 5 | for(int i = 0; i < board.size(); i++) { 6 | for(int j = 0; j < board[i].size(); j++) { 7 | if(i > 0 && board[i-1][j] == 'X') continue; 8 | if(j > 0 && board[i][j-1] == 'X') continue; 9 | if(board[i][j] == 'X') res++; 10 | } 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC949.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string largestTimeFromDigits(vector& arr) { 4 | sort(arr.begin(), arr.end()); 5 | string res; 6 | do { 7 | auto h = to_string(arr[0]) + to_string(arr[1]); 8 | auto m = to_string(arr[2]) + to_string(arr[3]); 9 | auto t = h + ':' + m; 10 | if(h <= "23" && m <= "59") { 11 | if(res.empty() || res < t) res = t; 12 | } 13 | } while(next_permutation(arr.begin(), arr.end())); 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC396.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxRotateFunction(vector& A) { 4 | typedef long long LL; 5 | LL sum = 0, cur = 0; 6 | for(auto c : A) sum += c; 7 | int n = A.size(); 8 | for(int i = 0; i < n; i++) cur += i * A[i]; 9 | LL res = cur; 10 | for(int i = n - 1; i >= 0; i --) { 11 | cur += sum - (LL)n * A[i]; 12 | res = max(res, cur); 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC412.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector fizzBuzz(int n) { 4 | vector res; 5 | for(int i = 1; i <= n; i++) { 6 | if(i % 3 == 0 && i % 5 == 0) { 7 | res.push_back("FizzBuzz"); 8 | } 9 | else if (i % 3 == 0) res.push_back("Fizz"); 10 | else if (i % 5 == 0) res.push_back("Buzz"); 11 | else res.push_back(to_string(i)); 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC925.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isLongPressedName(string name, string typed) { 4 | int i = 0, j = 0; 5 | int n = name.size(), m = typed.size(); 6 | while(i < n && j < m) { 7 | if(name[i] != typed[j]) return false; 8 | int x = i + 1, y = j + 1; 9 | while(x < n && name[x] == name[i]) x++; 10 | while(y < m && typed[y] == typed[j]) y++; 11 | if(x - i > y - j) return false; 12 | i = x, j = y; 13 | } 14 | return i == n && j == m; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC950.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector deckRevealedIncreasing(vector& deck) { 4 | int n = deck.size(); 5 | sort(deck.begin(), deck.end()); 6 | queue q; 7 | for(int i = 0; i < n; i++) q.push(i); 8 | vector res(n); 9 | int k = 0; 10 | while(q.size()){ 11 | auto t = q.front(); 12 | q.pop(); 13 | res[t] = deck[k++]; 14 | if(q.size()) { 15 | q.push(q.front()); 16 | q.pop(); 17 | } 18 | } 19 | return res; 20 | } 21 | }; -------------------------------------------------------------------------------- /ACwing/coin.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int N = 1010; 7 | 8 | int n, m; 9 | int v[N], w[N]; 10 | int f[N]; 11 | 12 | int main() 13 | { 14 | cin >> n >> m; 15 | 16 | for (int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i]; 17 | 18 | for (int i = 1; i <= n; i ++ ) 19 | for (int j = m; j >= v[i]; j -- ) 20 | f[j] = max(f[j], f[j - v[i]] + w[i]); 21 | 22 | cout << f[m] << endl; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /LeetCode/LC400.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findNthDigit(int n) { 4 | long long cnt = 1, st = 9, ten = 1; 5 | // 确定位于哪个区间,[1,9],[10-99],... 6 | while(n > cnt * st){ 7 | n -= cnt * st; 8 | cnt++; 9 | st *= 10, ten *= 10; 10 | } 11 | // 确定是区间里的哪个数, n/k下取整等于n-1/k - 1 12 | ten += (n + cnt - 1) / cnt -1; 13 | n = n % cnt ? n % cnt : cnt; 14 | return to_string(ten)[n-1]-'0'; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC553.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string optimalDivision(vector& nums) { 4 | int n = nums.size(); 5 | if(n < 1) return ""; 6 | if(n == 1) return to_string(nums[0]); 7 | if(n == 2) return to_string(nums[0]) + '/' + to_string(nums[1]); 8 | string res = to_string(nums[0]) + "/("; 9 | for(int i = 1; i < n-1; i++) res += to_string(nums[i])+'/'; 10 | res += to_string(nums.back()) + ')'; 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC645.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findErrorNums(vector& nums) { 4 | vector res(2); 5 | for(auto x : nums) { 6 | int k = abs(x); 7 | // 若是已有数为负,则该数为重复的数 8 | if(nums[k-1] < 0) res[0] = k; 9 | nums[k-1] *= -1; 10 | } 11 | for(int i = 0; i < nums.size(); i++) { 12 | if(nums[i] > 0 && i + 1 != res[0]) res[1] = i+1; 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC912.CPP: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void quick_sort(vector& q, int l, int r) { 4 | if(l >= r) return; 5 | int i = l - 1, j = r + 1, x = q[l+r >> 1]; 6 | while(i < j) { 7 | do i++; while(q[i] < x); 8 | do j--; while(q[j] > x); 9 | if(i < j) swap(q[i], q[j]); 10 | } 11 | quick_sort(q, l, j); 12 | quick_sort(q, j+1, r); 13 | } 14 | vector sortArray(vector& nums) { 15 | quick_sort(nums, 0, nums.size()-1); 16 | return nums; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC228.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | vector summaryRanges(vector &nums) { 4 | int n = nums.size(); 5 | vector ret; 6 | for(int i = 0; i < n; i++){ 7 | int j = i+1; 8 | while(j < n && nums[j-1]+1 == nums[j]) j++; 9 | // 不连续的情况 10 | if(j == i+1) ret.push_back(to_string(nums[i])); 11 | else { 12 | ret.push_back(to_string(nums[i]) + "->" + to_string(nums[j-1])); 13 | } 14 | i = j - 1; 15 | } 16 | return ret; 17 | } -------------------------------------------------------------------------------- /LeetCode/LC556.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int nextGreaterElement(int n) { 4 | string s = to_string(n); 5 | int k = s.size() - 1; 6 | while(k != 0 && s[k-1] >= s[k]) k--; 7 | if(!k) return -1; 8 | int t = k; 9 | while(t + 1 < s.size() && s[t+1] > s[k-1]) t++; 10 | swap(s[k-1], s[t]); 11 | reverse(s.begin()+k,s.end()); 12 | long long r = stoll(s); 13 | if(r > INT_MAX) return -1; 14 | return r; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC60.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string getPermutation(int n, int k) { 4 | string res; 5 | vector st(n); 6 | for(int i = 0; i < n; i++) { 7 | int fact = 1; 8 | for(int j = 1; j <= n - i - 1; j++) fact *= j; 9 | for(int j = 0; j < n; j++) { 10 | if(!st[j]) { 11 | if(fact < k) k -= fact; 12 | else { 13 | res += to_string(j+1); 14 | st[j] = true; 15 | break; 16 | } 17 | } 18 | } 19 | } 20 | return res; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/LC682.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int calPoints(vector& ops) { 4 | vector stk; 5 | for (auto& s: ops) { 6 | int p = stk.size() - 1; 7 | if (s == "+") stk.push_back(stk[p - 1] + stk[p]); 8 | else if (s == "D") stk.push_back(stk[p] * 2); 9 | else if (s == "C") stk.pop_back(); 10 | else stk.push_back(stoi(s)); 11 | } 12 | return accumulate(stk.begin(), stk.end(), 0); 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC713.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSubarrayProductLessThanK(vector& nums, int k) { 4 | int n = nums.size(); 5 | int product = 1; 6 | int res = 0; 7 | for(int i = 0, j = 0; i < n; i++) { 8 | product *= nums[i]; 9 | while(j <= i && product >= k) { 10 | product /= nums[j]; 11 | j++; 12 | } 13 | res += i - j + 1; 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /ACwing/ac2816.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | const int N = 100010; 6 | int a[N], b[N]; 7 | int n,m; 8 | 9 | 10 | int main() { 11 | scanf("%d%d", &n, &m); 12 | for(int i = 0; i < n; i++) scanf("%d", &a[i]); 13 | for(int i = 0; i < m; i++) scanf("%d", &b[i]); 14 | int i = 0, j = 0; 15 | while(i < n && j < m) { 16 | if(a[i] == b[j]) i++; 17 | j++; 18 | } 19 | if(i == n) printf("Yes\n"); 20 | else printf("No\n"); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /LeetCode/LC554.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int leastBricks(vector>& wall) { 4 | unordered_map hash; 5 | for(auto line : wall) { 6 | for(int i = 0, s = 0; i < (int)line.size()-1; i++) { 7 | s += line[i]; 8 | hash[s]++; 9 | } 10 | } 11 | int res = 0; 12 | for(auto [k,v] : hash){ 13 | res = max(res, v); 14 | } 15 | return wall.size() - res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC766.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | /* 4 | 1. 如果只能存储一行,也可以判断,因为只需要每次判断[i,j]与[i-1,j-1]的元素是否想相同,之后,每次遍历的时候 5 | 循环删除。这样只需要存储n+2个元素 6 | 2. 可以利用分治思想来做。 7 | */ 8 | bool isToeplitzMatrix(vector>& matrix) { 9 | for(int i = 1; i < matrix.size(); i++) { 10 | for(int j = 1; j < matrix[i].size(); j++) { 11 | if(matrix[i][j] != matrix[i-1][j-1]) return false; 12 | } 13 | } 14 | return true; 15 | } 16 | }; -------------------------------------------------------------------------------- /ACwing/ac898.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | const int N = 510; 6 | int f[N][N]; 7 | int n; 8 | 9 | int main() { 10 | scanf("%d", &n); 11 | for(int i = 1; i <= n; i++) { 12 | for(int j = 1; j <= i; j++) cin >> f[i][j]; 13 | } 14 | 15 | for(int i = n; i >= 1; i--) { 16 | for(int j = i; j >= 1; j--) { 17 | f[i][j] = max(f[i+1][j], f[i+1][j+1]) + f[i][j]; 18 | } 19 | } 20 | cout << f[1][1] << endl; 21 | return 0; 22 | } -------------------------------------------------------------------------------- /LeetCode/LC504.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convertToBase7(int num) { 4 | if(!num) return "0"; 5 | bool is_neg = false; 6 | if(num < 0) { 7 | is_neg = true; 8 | } 9 | num = abs(num); 10 | string res; 11 | while(num) { 12 | res += to_string(num % 7); 13 | num /= 7; 14 | } 15 | reverse(res.begin(), res.end()); 16 | if(is_neg) res = "-" + res; 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC672.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int flipLights(int n, int m) { 4 | if (n == 0) return 1; 5 | if (n == 1) { 6 | if (m == 0) return 1; 7 | return 2; 8 | } 9 | if (n == 2) { 10 | if (m == 0) return 1; 11 | if (m == 1) return 3; 12 | return 4; 13 | } 14 | 15 | if (m == 0) return 1; 16 | if (m == 1) return 4; 17 | if (m == 2) return 7; 18 | return 8; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC856.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scoreOfParentheses(string s) { 4 | stackstk; 5 | stk.push(0); 6 | for(auto c : s){ 7 | if(c == '('){ 8 | stk.push(0); 9 | } else { 10 | int t = stk.top(); 11 | stk.pop(); 12 | if(!t) t = 1; 13 | else t *= 2; 14 | stk.top() += t; 15 | } 16 | } 17 | return stk.top(); 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC883.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int projectionArea(vector>& grid) { 4 | int n = grid.size(); 5 | int res = 0; 6 | for(int i = 0; i < n; i++) { 7 | int r = 0, c = 0; 8 | for(int j = 0; j < n; j++) { 9 | if(grid[i][j]) res++; 10 | r = max(r, grid[i][j]); 11 | c = max(c, grid[j][i]); 12 | } 13 | res += r + c; 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /ACwing/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(Algorithm) 3 | 4 | set(CMAKE_CXX_STANDARD 11) 5 | 6 | add_executable(pufa pufa.cpp) 7 | add_executable(ac828 ac828.cpp) 8 | add_executable(ac842 ac842.cpp) 9 | add_executable(ac843 ac843.cpp) 10 | add_executable(ac831 ac831.cpp) 11 | add_executable(ac844 ac844.cpp) 12 | add_executable(ac789 ac789.cpp) 13 | add_executable(ac791 ac791.cpp) 14 | add_executable(ac792 ac792.cpp) 15 | add_executable(ac793 ac793.cpp) 16 | add_executable(ac61 ac61.cpp) 17 | -------------------------------------------------------------------------------- /LeetCode/LC482.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string licenseKeyFormatting(string S, int K) { 4 | string s; 5 | for(auto c : S) { 6 | if(c != '-') s += c; 7 | } 8 | string res; 9 | for(int i = 0; i < s.size() % K; i++) res += toupper(s[i]); 10 | for(int i = s.size() % K; i < s.size();) { 11 | if(res.size()) res += '-'; 12 | for(int j = 0; j < K; j++) res += toupper(s[i++]); 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC560.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int subarraySum(vector& nums, int k) { 4 | int n = nums.size(); 5 | vector S(n+1); 6 | for(int i = 1; i <= n; i++) { 7 | S[i] = S[i-1] + nums[i-1]; 8 | } 9 | int res = 0; 10 | unordered_map hash; 11 | hash[0] = 1; 12 | for(int i = 1; i <= n; i++) { 13 | res += hash[S[i] - k]; 14 | hash[S[i]]++; 15 | } 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC849.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDistToClosest(vector& seats) { 4 | int n = seats.size(); 5 | int res = 0; 6 | for(int i = 0; i < n; i++) { 7 | if(seats[i]) continue; 8 | int j = i + 1; 9 | while(j < n && !seats[j]) j++; 10 | if(!i || j == n) res = max(res, j - i); 11 | else { 12 | res = max(res, (j - i + 1) / 2); 13 | } 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC147.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | ListNode *insertionSortList(ListNode *head){ 7 | if (head == nullptr) return head; 8 | ListNode *dummy = new ListNode(-1); 9 | while(head){ 10 | ListNode *next = head->next; 11 | ListNode *cur = dummy; 12 | while(cur->next && cur->next->val <= head->val) cur = cur->next; 13 | head->next = cur->next; 14 | cur->next = head; 15 | head = next; 16 | } 17 | return dummy->next; 18 | } -------------------------------------------------------------------------------- /LeetCode/LC214.cpp: -------------------------------------------------------------------------------- 1 | 2 | string shortestPalindrome(string s){ 3 | int n = s.size(); 4 | string t(s.rbegin(), s.rend()); 5 | string str(s + "#" + t); 6 | 7 | vector nxt(2 * n + 1); 8 | 9 | int j = -1; 10 | nxt[0] = -1; 11 | 12 | for (int i = 1; i < 2 * n + 1; i++) { 13 | while (j > -1 && str[j + 1] != str[i]) j = nxt[j]; 14 | if (str[j + 1] == str[i]) 15 | j++; 16 | 17 | nxt[i] = j; 18 | } 19 | 20 | return t.substr(0, n - nxt[2 * n] - 1) + s; 21 | } -------------------------------------------------------------------------------- /LeetCode/LC375.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getMoneyAmount(int n) { 4 | vector> f(n + 2, vector(n + 2)); 5 | for (int len = 2; len <= n; len ++ ) 6 | for (int i = 1; i + len - 1 <= n; i ++ ) { 7 | int j = i + len - 1; 8 | f[i][j] = INT_MAX; 9 | for (int k = i; k <= j; k ++ ) 10 | f[i][j] = min(f[i][j], max(f[i][k - 1], f[k + 1][j]) + k); 11 | } 12 | return f[1][n]; 13 | } 14 | }; -------------------------------------------------------------------------------- /LeetCode/LC389.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char findTheDifference1(string s, string t) { 4 | unordered_map hash; 5 | for(auto c : t) hash[c]++; 6 | for(auto c : s) hash[c]--; 7 | for(auto c : t) { 8 | if(hash[c]) return c; 9 | } 10 | return -1; 11 | } 12 | char findTheDifference(string s, string t) { 13 | int x = 0; 14 | for(auto c : s) x ^= c; 15 | for(auto c : t) x ^= c; 16 | return x; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC893.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSpecialEquivGroups(vector& words) { 4 | unordered_set S; 5 | for (auto& w: words) { 6 | string a, b; 7 | for (int i = 0; i < w.size(); i += 2) { 8 | a += w[i]; 9 | b += w[i + 1]; 10 | } 11 | sort(a.begin(), a.end()); 12 | sort(b.begin(), b.end()); 13 | S.insert(a + b); 14 | } 15 | return S.size(); 16 | } 17 | }; -------------------------------------------------------------------------------- /ACwing/ac104.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | const int N = 100010; 7 | 8 | int arr[N]; 9 | int n; 10 | 11 | 12 | 13 | int main(){ 14 | scanf("%d", &n); 15 | for(int i = 0; i < n; i++) scanf("%d", &arr[i]); 16 | 17 | sort(arr, arr+n); 18 | 19 | int mid = arr[n/2]; 20 | 21 | int ans = 0; 22 | 23 | for(int i = 0; i < n; i++) ans += abs(arr[i] - mid); 24 | 25 | 26 | printf("%d\n", ans); 27 | 28 | 29 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /LeetCode/LC144.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | vector preorderTraversal(TreeNode *root) { 8 | vector res; 9 | stack stk; 10 | while(root || stk.size()) { 11 | while(root){ 12 | res.push_back(root->val); 13 | stk.push(root); 14 | root = root->left; 15 | } 16 | root = stk.top()->right; 17 | stk.pop(); 18 | } 19 | return res; 20 | } 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /LeetCode/LC668.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int get(int n, int m, int mid) { 4 | int tot = 0; 5 | for(int i = 1; i <= m; i++) { 6 | tot += min(n, mid / i); 7 | } 8 | return tot; 9 | } 10 | int findKthNumber(int m, int n, int k) { 11 | int l = 1, r = n * m; 12 | while(l < r) { 13 | int mid = (l + r) >> 1; 14 | if(k <= get(n, m, mid)) r = mid; 15 | else l = mid + 1; 16 | } 17 | return r; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC784.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector res; 4 | vector letterCasePermutation(string S) { 5 | int u = 0; 6 | dfs(S, u); 7 | return res; 8 | } 9 | void dfs(string &s, int u) { 10 | if(u == s.size()) { 11 | res.push_back(s); 12 | return; 13 | } 14 | dfs(s, u+1); 15 | if(!isdigit(s[u])) { 16 | s[u] ^= 32; 17 | dfs(s, u+1); 18 | s[u] ^= 32; 19 | } 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/LC821.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector shortestToChar(string s, char c) { 4 | int n = s.size(); 5 | vector res(n, INT_MAX); 6 | for (int i = 0, j = -1; i < n; i ++ ) { 7 | if (s[i] == c) j = i; 8 | if (j != -1) res[i] = i - j; 9 | } 10 | for (int i = n - 1, j = -1; i >= 0; i -- ) { 11 | if (s[i] == c) j = i; 12 | if (j != -1) res[i] = min(res[i], j - i); 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC825.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numFriendRequests(vector& ages) { 4 | int n = ages.size(); 5 | vector S(121); 6 | for(int i = 0; i < n; i++) S[ages[i]]++; 7 | int res = n * n; 8 | for(int i = 0; i <= 120; i++) { 9 | for(int j = 0; j <= 120; j++) { 10 | if(j <= 0.5 * i + 7 || j > i) res -= S[i] * S[j]; 11 | else if(i == j) res -= S[i]; 12 | } 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC12.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string intToRoman(int num) { 4 | int values[] = { 5 | 1000, 6 | 900, 500, 400, 100, 7 | 90, 50, 40, 10, 8 | 9, 5, 4, 1 9 | }; 10 | string reps[] = { 11 | "M", 12 | "CM", "D", "CD", "C", 13 | "XC", "L", "XL", "X", 14 | "IX", "V", "IV", "I", 15 | }; 16 | 17 | string res; 18 | for(int i = 0; i < 13; i++) { 19 | while(num >= values[i]) { 20 | num -= values[i]; 21 | res += reps[i]; 22 | } 23 | } 24 | return res; 25 | } 26 | }; -------------------------------------------------------------------------------- /LeetCode/LC377.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int combinationSum4(vector& nums, int target) { 4 | int n = nums.size(); 5 | const int MOD = INT_MAX; 6 | vector f(target + 1); 7 | f[0] = 1; 8 | for(int i = 1; i <= target; i++){ 9 | for(int j = 0; j < n; j++){ 10 | if(i >= nums[j]){ 11 | f[i] = (f[i] + f[i-nums[j]]) % MOD; 12 | } 13 | } 14 | } 15 | return f[target]; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC528.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector s; 4 | 5 | Solution(vector& w) { 6 | s = w; 7 | for(int i = 1; i < s.size(); i++) s[i] += s[i-1]; 8 | } 9 | 10 | int pickIndex() { 11 | int x = rand() % s.back() + 1; 12 | return lower_bound(s.begin(), s.end(), x) - s.begin(); 13 | } 14 | }; 15 | 16 | /** 17 | * Your Solution object will be instantiated and called as such: 18 | * Solution* obj = new Solution(w); 19 | * int param_1 = obj->pickIndex(); 20 | */ -------------------------------------------------------------------------------- /LeetCode/LC714.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices, int fee) { 4 | int n = prices.size(), INF = 1e8; 5 | vector> f(n+1, vector(2, -INF)); 6 | f[0][0] = 0; 7 | int res = 0; 8 | for(int i = 1; i <= n; i++) { 9 | f[i][0] = max(f[i-1][0], f[i-1][1]+prices[i-1]); 10 | f[i][1] = max(f[i-1][1], f[i-1][0] - prices[i-1] - fee); 11 | res = max(res, f[i][0]); 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC846.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isNStraightHand(vector& hand, int m) { 4 | int n = hand.size(); 5 | if(n % m != 0) return false; 6 | multiset S; 7 | for(auto x :hand) S.insert(x); 8 | while(S.size()){ 9 | int x = *S.begin(); 10 | for(int i = x; i < x + m; i++){ 11 | if(!S.count(i)) return false; 12 | else S.erase(S.find(i)); 13 | } 14 | } 15 | return true; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC891.cpp: -------------------------------------------------------------------------------- 1 | typedef long long LL; 2 | const int N = 20010, MOD = 1e9+7; 3 | 4 | int p[N]; 5 | 6 | class Solution { 7 | public: 8 | int sumSubseqWidths(vector& nums) { 9 | int n = nums.size(); 10 | p[0] = 1; 11 | for(int i = 1; i <= n; i++) p[i] = p[i-1] * 2 % MOD; 12 | int res = 0; 13 | sort(nums.begin(), nums.end()); 14 | for(int i = 0; i < n; i++) 15 | res = (res + (LL)nums[i] * p[i] - (LL)nums[i] * p[n-i-1]) % MOD; 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /ACwing/ac790.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by devinchang on 2020/3/22. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | // 浮点数二分 9 | // 需要注意的是浮点的边界判断 10 | 11 | 12 | int main(){ 13 | 14 | double x; 15 | cin >> x; 16 | 17 | double l = -100, r = 100; 18 | while(r - l > 1e-8){ 19 | double mid = (l + r) / 2; 20 | if(mid * mid * mid >= x) r = mid; 21 | else l = mid; 22 | } 23 | 24 | //cout << l << endl; 25 | printf("%.6lf", l); 26 | 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /LeetCode/LC13.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToInt(string s) { 4 | unordered_map hash; 5 | hash['I'] = 1; hash['V'] = 5; 6 | hash['X'] = 10; hash['L'] = 50; 7 | hash['C'] = 100; hash['D'] = 500; 8 | hash['M'] = 1000; 9 | 10 | int res = 0; 11 | for(int i = 0; i < s.size(); i++) { 12 | if(i+1 < s.size() && hash[s[i]] < hash[s[i+1]]) res -= hash[s[i]]; 13 | else res += hash[s[i]]; 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC67.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addBinary(string a, string b) { 4 | string res; 5 | int n = a.size(), m = b.size(); 6 | int t = 0; 7 | for(int i = n-1, j = m -1; i >= 0 || j >= 0;) { 8 | if(i >= 0) t += a[i--] - '0'; 9 | if(j >= 0) t += b[j--] - '0'; 10 | res += to_string(t % 2); 11 | t /= 2; 12 | } 13 | if(t) res += to_string(t); 14 | reverse(res.begin(), res.end()); 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC740.cpp: -------------------------------------------------------------------------------- 1 | const int N = 10010; 2 | int cnt[N], f[N][2]; 3 | class Solution { 4 | public: 5 | int deleteAndEarn(vector& nums) { 6 | memset(cnt, 0, sizeof cnt); 7 | memset(f, 0, sizeof f); 8 | int res = 0; 9 | for(auto x : nums) cnt[x]++; 10 | for(int i = 1; i < N; i++) { 11 | f[i][0] = max(f[i-1][0], f[i-1][1]); 12 | f[i][1] = f[i-1][0] + i * cnt[i]; 13 | res = max(res, max(f[i][0], f[i][1])); 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC837.cpp: -------------------------------------------------------------------------------- 1 | const int N = 20010; 2 | double f[N]; 3 | 4 | class Solution { 5 | public: 6 | double new21Game(int N, int K, int W) { 7 | if (!K) return 1; 8 | memset(f, 0, sizeof f); 9 | for (int i = K; i <= N && i < K + W; i ++ ) f[i] = 1; 10 | f[K - 1] = 0; 11 | for (int i = 1; i <= W; i ++ ) f[K - 1] += f[K - 1 + i] / (double)W; 12 | for (int i = K - 2; i >= 0; i -- ) 13 | f[i] = f[i + 1] + (f[i + 1] - f[i + W + 1]) / (double)W; 14 | return f[0]; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC929.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numUniqueEmails(vector& emails) { 4 | unordered_set S; 5 | for(auto email : emails) { 6 | string local, domain; 7 | int k = 0; 8 | while(email[k] != '@') k++; 9 | local = email.substr(0, k); 10 | domain = email.substr(k+1); 11 | string tmp; 12 | for(auto c : local) { 13 | if(c == '+') break; 14 | if(c == '.') continue; 15 | tmp += c; 16 | } 17 | S.insert(tmp + '@' + domain); 18 | } 19 | return S.size(); 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/LC948.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int bagOfTokensScore(vector& tokens, int power) { 4 | int n = tokens.size(), res = 0, grade = 0; 5 | sort(tokens.begin(), tokens.end()); 6 | for(int i = 0, j = n-1; i <= j; ) { 7 | while(i <= j && power >= tokens[i]) { 8 | power -= tokens[i++]; 9 | grade++; 10 | } 11 | res = max(res, grade); 12 | if(grade > 0 && i <= j) { 13 | power += tokens[j--]; 14 | grade--; 15 | } else { 16 | break; 17 | } 18 | } 19 | return res; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/LC324.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void wiggleSort(vector& nums) { 5 | #define A(i) nums[(i * 2 + 1) % (n | 1)] 6 | int n = nums.size(); 7 | auto midptr = nums.begin() + n / 2; 8 | nth_element(nums.begin(), midptr, nums.end()); 9 | int mid = *midptr; 10 | for(int i = 0, j = 0, k = n-1; i <= k;){ 11 | if(A(i) > mid) swap(A(i++), A(j++)); 12 | else if(A(i) < mid) swap(A(i), A(k--)); 13 | else i++; 14 | } 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC96.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by devinchang on 2019/12/20. 3 | // 4 | 5 | #include "util.h" 6 | 7 | 8 | 9 | int numTrees(int n){ 10 | if(n < 1) 11 | return 0; 12 | vector dp(n+1, 0); 13 | dp[0] = 1, dp[1] = 1; 14 | for(int i = 2; i <= n; i++){ 15 | for(int j = 1; j <= i; j++){ 16 | dp[i] += dp[j-1] * dp[i-j]; 17 | } 18 | } 19 | return dp[n]; 20 | } 21 | 22 | 23 | int main(){ 24 | int ret = numTrees(4); 25 | cout << ret << endl; 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /ACwing/ac1611.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /* 6 | 寻找峰值 7 | */ 8 | 9 | 10 | 11 | // Forward declaration of queryAPI. 12 | // int query(int x); 13 | // return int means nums[x]. 14 | 15 | class Solution { 16 | public: 17 | int findPeakElement(int n) { 18 | int l = 0, r = n-1; 19 | while(l < r) { 20 | int mid = l + r >> 1; 21 | if(query(mid) < query(mid+1)) l = mid+1; 22 | else r = mid; 23 | } 24 | return r; 25 | } 26 | }; -------------------------------------------------------------------------------- /LeetCode/LC6.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string convert(string s, int n) { 4 | string res; 5 | if(n == 1) return s; 6 | for(int i = 0; i < n; i++) { 7 | if(i == 0 || i == n-1) { 8 | for(int j = i; j < s.size(); j += 2* n - 2) { 9 | res += s[j]; 10 | } 11 | } else { 12 | for(int j = i, k = 2 * n - 2 - i; j < s.size() || k < s.size(); j += 2 * n - 2, k += 2 * n - 2) { 13 | if(j < s.size()) res += s[j]; 14 | if(k < s.size()) res += s[k]; 15 | } 16 | } 17 | } 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /ACwing/ac1453.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main() { 8 | string s; 9 | int k; 10 | cin >> s >> k; 11 | string res = "0"; 12 | for(int i = 0; i < s.size(); i++) { 13 | while(k && s[i] < res.back()) { 14 | res.pop_back(); 15 | k--; 16 | } 17 | res += s[i]; 18 | } 19 | while(k--) res.pop_back(); 20 | int i = 0; 21 | while(i < res.size() && res[i] == '0') i++; 22 | if(i == res.size()) puts("0"); 23 | else cout << res.substr(i) << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /LeetCode/LC316.cpp: -------------------------------------------------------------------------------- 1 | string removeDuplicateLetters(string s) { 2 | unordered_map ins; 3 | unordered_map last; 4 | string res; 5 | int n = s.size(); 6 | for(int i = 0; i < n; i++) last[s[i]] = i; 7 | for(int i = 0; i < n; i++){ 8 | if(ins[s[i]]) continue; 9 | while(res.size() && res.back() > s[i] && last[res.back()] > i){ 10 | res.pop_back(); 11 | ins[s[i]] = false; 12 | } 13 | res += s[i]; 14 | ins[s[i]] = true; 15 | } 16 | return res; 17 | } -------------------------------------------------------------------------------- /LeetCode/LC372.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | const int p = 1337; 4 | 5 | int qmi(int a, int b) { 6 | a %= p; 7 | int res = 1; 8 | while(b) { 9 | if(b & 1) res = res * a % p; 10 | a = a * a % p; 11 | b >>= 1; 12 | } 13 | return res; 14 | } 15 | 16 | int superPow(int a, vector& b) { 17 | if(b.empty()) return 1; 18 | int k = b.back(); 19 | b.pop_back(); 20 | return qmi(superPow(a, b), 10) * qmi(a, k) % p; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/LC795.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int calc(vector& A, int k) { 4 | int res = 0; 5 | for(int i = 0; i < A.size();i++) { 6 | if(A[i] > k) continue; 7 | int j = i + 1; 8 | while(j < A.size() && A[j] <= k) j++; 9 | int z = j - i; 10 | res += z * (z + 1) / 2; 11 | i = j; 12 | } 13 | return res; 14 | } 15 | int numSubarrayBoundedMax(vector& A, int L, int R) { 16 | return calc(A, R) - calc(A, L-1); 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC797.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> g; 4 | vector> res; 5 | vector path; 6 | int n; 7 | void dfs(int u) { 8 | path.push_back(u); 9 | if(u == n - 1) res.push_back(path); 10 | for(auto v : g[u]) dfs(v); 11 | path.pop_back(); 12 | } 13 | vector> allPathsSourceTarget(vector>& graph) { 14 | g = graph; 15 | n = graph.size(); 16 | int u = 0; 17 | dfs(u); 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC216.cpp: -------------------------------------------------------------------------------- 1 | 2 | vector> ret; 3 | vector path; 4 | 5 | vector> combinationSum3(int k, int n){ 6 | dfs(k, n, 1); 7 | return ret; 8 | } 9 | 10 | void dfs(int k, int n, int st) { 11 | if(!k){ 12 | if(!n){ 13 | ret.push_back(path); 14 | return; 15 | } 16 | } 17 | for(int i = st; i <= 10-k; i++){ 18 | if(n >= i){ 19 | path.push_back(i); 20 | dfs(k-1, n-i, i+1); 21 | path.pop_back(); 22 | } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /ACwing/ac4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int N = 110; 6 | int w[N], v[N], s[N]; 7 | int f[N][N]; 8 | int n,m; 9 | 10 | 11 | 12 | int main() { 13 | cin >> n >> m; 14 | for(int i = 1; i <= n; i++) cin >> v[i] >> w[i] >> s[i]; 15 | 16 | 17 | for(int i = 1; i <= n; i++) { 18 | for(int j = 0; j <= m; j++) { 19 | for(int k = 0; k <= s[i] && k * v[i] <= j; k++) 20 | f[i][j] = max(f[i][j], f[i-1][j-k*v[i]] + k * w[i]); 21 | } 22 | } 23 | 24 | cout << f[n][m] << endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /LeetCode/LC236.cpp: -------------------------------------------------------------------------------- 1 | TreeNode *res; 2 | 3 | TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { 4 | if(root == nullptr) return root; 5 | dfs(root, p, q); 6 | return res; 7 | } 8 | 9 | 10 | int dfs(TreeNode *root, TreeNode *p, TreeNode *q) { 11 | if(root == nullptr) return 0; 12 | int state = dfs(root->left, p, q); 13 | if(root == p) state |= 1; 14 | else if (root == q) state |= 2; 15 | state |= dfs(root->right, p, q); 16 | if(state == 3 && res == nullptr) res = root; 17 | return state; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /LeetCode/LC335.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isSelfCrossing(vector& x) { 4 | int n = x.size(); 5 | if(n <= 3) return false; 6 | for(int i = 3; i < n; i++){ 7 | if(x[i-1] <= x[i-3] && x[i] >= x[i-2]) return true; 8 | if(i >= 4 && x[i-3] == x[i-1] && x[i] + x[i-4] >= x[i-2]) return true; 9 | if (i >= 5 && x[i - 3] >= x[i - 1] && x[i - 1] + x[i - 5] >= x[i - 3] && x[i - 2] >= x[i - 4] && x[i - 4] + x[i] >= x[i - 2]) return true; 10 | } 11 | return false; 12 | } 13 | }; -------------------------------------------------------------------------------- /LeetCode/LC347.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | unordered_map hash; 5 | vector res; 6 | for (int x : nums) hash[x] ++; 7 | int n = nums.size(); 8 | vectors(n + 1, 0); 9 | for (auto &p : hash) s[p.second] ++ ; 10 | int i = n, t = 0; 11 | while (t < k) t += s[i -- ]; 12 | for (auto &p : hash) 13 | if (p.second > i) 14 | res.push_back(p.first); 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC479.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestPalindrome(int n) { 4 | typedef long long LL; 5 | if(n == 1) return 9; 6 | int maxv = pow(10, n) - 1; 7 | for(int i = maxv; ; i--) { 8 | auto a = to_string(i); 9 | auto b = a; 10 | reverse(b.begin(), b.end()); 11 | auto num = stoll(a + b); 12 | for(LL j = maxv; j * j >= num; j--) { 13 | if(num % j == 0) return num % 1337; 14 | } 15 | } 16 | return 0; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC565.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrayNesting(vector& nums) { 4 | int res = 0; 5 | for(int i = 0; i < nums.size(); i++) { 6 | if(nums[i] != -1) { 7 | int j = i, s = 0; 8 | while(nums[j] != -1) { 9 | s++; 10 | int next = nums[j]; 11 | nums[j] = -1; 12 | j = next; 13 | } 14 | res = max(res, s); 15 | } 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC763.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector partitionLabels(string S) { 4 | unordered_map last; 5 | int n = S.size(); 6 | for(int i = 0; i < n; i++) last[S[i]] = i; 7 | vector res; 8 | int start = 0, end = 0; 9 | for(int i = 0; i < n; i++) { 10 | end = max(end, last[S[i]]); 11 | if(i == end) { 12 | res.push_back(end - start + 1); 13 | start = end = i+1; 14 | } 15 | } 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC793.cpp: -------------------------------------------------------------------------------- 1 | typedef long long LL; 2 | 3 | class Solution { 4 | public: 5 | int preimageSizeFZF(int k) { 6 | return calc(k) - calc(k - 1); 7 | } 8 | 9 | LL f(LL x) { 10 | LL res = 0; 11 | while (x) res += x / 5, x /= 5; 12 | return res; 13 | } 14 | 15 | LL calc(int k) { 16 | LL l = -1, r = 1e18; 17 | while (l < r) { 18 | LL mid = l + r + 1 >> 1; 19 | if (f(mid) <= k) l = mid; 20 | else r = mid - 1; 21 | } 22 | return r; 23 | } 24 | }; -------------------------------------------------------------------------------- /ACwing/ac1455.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N = 1010; 5 | int n, m; 6 | int a[N]; 7 | 8 | /* 9 | 约瑟夫环公式: 10 | f(N, M) = (f(N-1, M) + M) % N 11 | -> 12 | f(i, M) = (f(i-1, a[(N-i) % M])) % i 13 | */ 14 | 15 | 16 | int main() { 17 | int T; 18 | scanf("%d", &T); 19 | while(T--) { 20 | scanf("%d%d", &n, &m); 21 | for(int i = 0; i < m; i++) scanf("%d", &a[i]); 22 | 23 | int res = 0; 24 | for(int i = 2; i <= n; i++) { 25 | res = (res + a[(n - i) % m]) % i; 26 | } 27 | printf("%d\n", res); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /LeetCode/LC260.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | vector singleNumber(vector& nums) { 4 | int sum = 0; 5 | int n = nums.size(); 6 | for(int i = 0; i < n; i++) { 7 | sum ^= nums[i]; 8 | } 9 | int pos = 0; 10 | for(int i = 0; i < 32; i ++){ 11 | if((sum >> i) & 1) { 12 | pos = i; 13 | break; 14 | } 15 | } 16 | int s1 = 0, s2 = 0; 17 | for(int i = 0; i < n; i++){ 18 | if((nums[i] >> pos) & 1) s1 ^= nums[i]; 19 | else s2 ^= nums[i]; 20 | } 21 | return vector{s1, s2}; 22 | } -------------------------------------------------------------------------------- /LeetCode/LC328.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* oddEvenList(ListNode* head) { 4 | if(head == nullptr || head->next == nullptr) return head; 5 | ListNode *odd = head, *even = head->next, *even_head = head->next; 6 | for(ListNode *p = even->next; p;){ 7 | odd->next = p, odd = p, p = p->next; 8 | if(p){ 9 | even->next = p, even = p, p = p->next; 10 | } 11 | } 12 | odd->next = even_head; 13 | even->next = nullptr; 14 | return head; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC398.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | unordered_map> hash; 4 | Solution(vector& nums) { 5 | int n = nums.size(); 6 | for(int i = 0; i < n; i++) { 7 | hash[nums[i]].push_back(i); 8 | } 9 | } 10 | 11 | int pick(int target) { 12 | return hash[target][rand() % hash[target].size()]; 13 | } 14 | }; 15 | 16 | /** 17 | * Your Solution object will be instantiated and called as such: 18 | * Solution* obj = new Solution(nums); 19 | * int param_1 = obj->pick(target); 20 | */ -------------------------------------------------------------------------------- /LeetCode/LC452.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMinArrowShots(vector>& points) { 4 | int n = points.size(); 5 | if(n < 1) return 0; 6 | sort(points.begin(), points.end(), [](vector a, vector b) { 7 | return a[1] < b[1]; 8 | }); 9 | int res = 1, r = points[0][1]; 10 | for(int i = 1; i < n; i++) { 11 | if(points[i][0] > r) { 12 | res++; 13 | r = points[i][1]; 14 | } 15 | } 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /ACwing/ac52.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | 8 | class Solution { 9 | public: 10 | int moreThanHalfNum_Solution(vector& nums) { 11 | int value = 0, count = 0; 12 | for(auto v : nums){ 13 | if(count == 0){ 14 | value = v; 15 | count++; 16 | } 17 | else{ 18 | if(v != value) count--; 19 | else count++; 20 | } 21 | } 22 | return value; 23 | } 24 | }; -------------------------------------------------------------------------------- /ACwing/ac896.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | const int N = 1000010; 6 | int n; 7 | int arr[N], q[N]; 8 | 9 | 10 | 11 | int main() { 12 | scanf("%d", &n); 13 | for(int i = 1; i <= n; i++) scanf("%d", &arr[i]); 14 | 15 | int len = 0; 16 | for(int i = 1; i <= n; i++) { 17 | int l = 0, r = len; 18 | while(l < r) { 19 | int mid = l + r + 1 >> 1; 20 | if(q[mid] < arr[i]) l = mid; 21 | else r = mid - 1; 22 | } 23 | len = max(len, r + 1); 24 | q[r+1] = arr[i]; 25 | } 26 | 27 | cout << len << endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /LeetCode/LC841.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> g; 4 | vector st; 5 | int n; 6 | 7 | void dfs(int u){ 8 | st[u] = true; 9 | for(auto v : g[u]){ 10 | if(!st[v]) dfs(v); 11 | } 12 | } 13 | 14 | bool canVisitAllRooms(vector>& rooms) { 15 | g = rooms; 16 | n = rooms.size(); 17 | st.resize(n); 18 | dfs(0); 19 | for(int i = 0; i < n; i++) { 20 | if(!st[i]) return false; 21 | } 22 | return true; 23 | } 24 | }; -------------------------------------------------------------------------------- /ACwing/ac830.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by devinchang on 2020/3/13. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | 9 | /* 单调队列 */ 10 | 11 | const int N = 100010; 12 | 13 | int stk[N]; 14 | int tt; 15 | 16 | 17 | int n; 18 | 19 | 20 | 21 | int main(){ 22 | cin >> n; 23 | for(int i = 0; i < n; i++){ 24 | int x; 25 | cin >> x; 26 | while(tt && stk[tt] >= x) tt--; 27 | if(!tt) cout << -1 << " "; 28 | else cout << stk[tt] << " "; 29 | stk[++tt] = x; 30 | } 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /ACwing/ac9.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode *father; 8 | * TreeNode(int x) : val(x), left(NULL), right(NULL), father(NULL) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | TreeNode* inorderSuccessor(TreeNode* p) { 14 | if(p->right) { 15 | p = p->right; 16 | while(p->left) p = p->left; 17 | return p; 18 | } 19 | while(p->father && p == p->father->right) p = p->father; 20 | return p->father; 21 | } 22 | }; -------------------------------------------------------------------------------- /LeetCode/LC199.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | vector rightSideView(TreeNode *root) { 5 | queue que; 6 | vector ret; 7 | if(root == nullptr) return ret; 8 | que.push(root); 9 | while(que.size()) { 10 | int n = que.size(); 11 | for(int i = 0; i < n; i++) { 12 | TreeNode *cur = que.front(); 13 | que.pop(); 14 | if(cur->left) que.push(cur->left); 15 | if(cur->right) que.push(cur->right); 16 | if(i == n-1) ret.push_back(cur->val); 17 | } 18 | } 19 | return ret; 20 | } -------------------------------------------------------------------------------- /LeetCode/LC28.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int strStr(string s, string p) { 4 | if(p.empty()) return 0; 5 | int n = s.size(), m = p.size(); 6 | s = ' ' + s, p = ' ' + p; 7 | vector next(m+1); 8 | 9 | for(int i = 2, j = 0; i <= m; i++) { 10 | while(j && p[i] != p[j+1]) j = next[j]; 11 | if(p[i] == p[j+1]) j++; 12 | next[i] = j; 13 | } 14 | 15 | for(int i = 1, j = 0; i <= n; i++) { 16 | while(j && s[i] != p[j+1]) j = next[j]; 17 | if(s[i] == p[j+1]) j++; 18 | if(j == m) return i - m; 19 | } 20 | 21 | return -1; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/LC539.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMinDifference(vector& timePoints) { 4 | int res = INT_MAX; 5 | vector q; 6 | for(auto &t : timePoints) { 7 | int h, m; 8 | sscanf(t.c_str(), "%d:%d", &h, &m); 9 | q.push_back(60 * h + m); 10 | } 11 | sort(q.begin(), q.end()); 12 | for(int i = 1; i < q.size(); i++) res = min(res, q[i] - q[i-1]); 13 | // 最后一个时刻和第一个时刻的间隔 14 | res = min(res, 24 * 60 + q[0] - q.back()); 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC684.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector p; 4 | int find(int x) { 5 | if(p[x] != x) p[x] = find(p[x]); 6 | return p[x]; 7 | } 8 | 9 | vector findRedundantConnection(vector>& edges) { 10 | int n = edges.size(); 11 | p.resize(n+1); 12 | for(int i = 1; i <= n; i++) p[i] = i; 13 | for(auto &e : edges) { 14 | int a = find(e[0]), b = find(e[1]); 15 | if(a != b) p[a] = b; 16 | else return e; 17 | } 18 | return {}; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC735.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector asteroidCollision(vector& asteroids) { 4 | vector res; 5 | for(auto aster : asteroids) { 6 | if(aster > 0) res.push_back(aster); 7 | else { 8 | while(res.size() && res.back() > 0 && res.back() < -aster) res.pop_back(); 9 | if (res.size() && res.back() == -aster) res.pop_back(); 10 | else if(res.empty() || res.back() < 0) res.push_back(aster); 11 | } 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /LeetCode/LC918.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubarraySumCircular(vector& nums) { 4 | int n = nums.size(); 5 | vector s(n * 2 + 1); 6 | for(int i = 1; i <= 2 * n; i++) s[i] = s[i-1] + nums[(i-1) % n]; 7 | 8 | deque q; 9 | q.push_front(0); 10 | int res = INT_MIN; 11 | for(int i = 1; i <= n * 2; i++) { 12 | while(q.size() && i - q.front() > n) q.pop_front(); 13 | res = max(res, s[i] - s[q.front()]); 14 | while(q.size() && s[q.back()] >= s[i]) q.pop_back(); 15 | q.push_back(i); 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC402.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeKdigits(string num, int k) { 4 | k = min(k, (int)num.size());; 5 | string res; 6 | for(auto c : num) { 7 | while(k && res.size() && res.back() > c) { 8 | k--; 9 | res.pop_back(); 10 | } 11 | res += c; 12 | } 13 | while(k--) res.pop_back(); 14 | k = 0; 15 | while(k < res.size() && res[k] == '0') k++; 16 | if(k == res.size()) res += '0'; 17 | return res.substr(k); 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC454.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fourSumCount(vector& A, vector& B, vector& C, vector& D) { 4 | unordered_map hash; 5 | for(auto c : C) { 6 | for(auto d : D) { 7 | hash[c + d]++; 8 | } 9 | } 10 | int res = 0; 11 | for(auto a : A) { 12 | for(auto b : B) { 13 | if(hash.count(-(a+b))) { 14 | res += hash[-(a+b)]; 15 | } 16 | } 17 | } 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC822.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int flipgame(vector& fronts, vector& backs) { 4 | unordered_set S; 5 | for(int i = 0; i < fronts.size(); i++) { 6 | if(fronts[i] == backs[i]) S.insert(fronts[i]); 7 | } 8 | int res = INT_MAX; 9 | for(auto x : fronts) { 10 | if(!S.count(x)) res = min(res, x); 11 | } 12 | for(auto x : backs) { 13 | if(!S.count(x)) res = min(res, x); 14 | } 15 | if(res == INT_MAX) res = 0; 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC875.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int get(vector& piles, int mid) { 4 | int res = 0; 5 | for(auto x : piles) { 6 | // 求a/b的上取整等价于求(a+b-1)/b的下取整 7 | res += (x + mid - 1) / mid; 8 | } 9 | return res; 10 | } 11 | int minEatingSpeed(vector& piles, int h) { 12 | int l = 1, r = 1e9; 13 | while(l < r) { 14 | int mid = l + r >> 1; 15 | if(get(piles, mid) <= h) r = mid; 16 | else l = mid + 1; 17 | } 18 | return r; 19 | } 20 | }; -------------------------------------------------------------------------------- /ACwing/ac895.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | 6 | const int N = 1010; 7 | int n; 8 | int arr[N], f[N]; 9 | 10 | 11 | int main() { 12 | scanf("%d", &n); 13 | for(int i = 1; i <= n; i++) scanf("%d", &arr[i]); 14 | 15 | for(int i = 1; i <= n; i++) { 16 | f[i] = 1; 17 | for(int j = 1; j <= i; j++) { 18 | if(arr[j] < arr[i]) f[i] = max(f[i], f[j] +1); 19 | } 20 | } 21 | int res = 0; 22 | for(int i = 1; i <= n; i++) res = max(res, f[i]); 23 | 24 | cout << res << endl; 25 | return 0; 26 | } -------------------------------------------------------------------------------- /LeetCode/LC16.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int threeSumClosest(vector& nums, int target) { 4 | int res = 0, n = nums.size(); 5 | res = nums[0] + nums[1] + nums[2]; 6 | sort(nums.begin(), nums.end()); 7 | for(int i = 0; i < n; i++) { 8 | int x = nums[i]; 9 | for(int l = i + 1, r = n - 1; l < r; ) { 10 | int sum = nums[l] + nums[r] + x; 11 | if(abs(sum - target) < abs(res - target)) 12 | res = sum; 13 | if(sum == target) return target; 14 | else if(sum < target) l++; 15 | else r--; 16 | } 17 | } 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC424.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int characterReplacement(string s, int k) { 4 | int res = 0; 5 | int n = s.size(); 6 | for(int c = 'A'; c <= 'Z'; c++) { 7 | int cnt = 0; 8 | for(int i = 0, j = 0; i < n; i++){ 9 | if(s[i] == c) cnt++; 10 | while(i - j + 1 - cnt > k) { 11 | if(s[j] == c) cnt--; 12 | j++; 13 | } 14 | res = max(res, i - j + 1); 15 | } 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC630.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int scheduleCourse(vector>& courses) { 4 | sort(courses.begin(), courses.end(), [](vector&a, vector&b) { 5 | return a[1] < b[1]; 6 | }); 7 | priority_queue heap; 8 | int tot = 0; 9 | for(auto &c : courses) { 10 | tot += c[0]; 11 | heap.push(c[0]); 12 | if(tot > c[1]) { 13 | tot -= heap.top(); 14 | heap.pop(); 15 | } 16 | } 17 | return heap.size(); 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC697.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findShortestSubArray(vector& nums) { 4 | unordered_map cnt, minp, maxp; 5 | int d = 0; 6 | for (int i = 0; i < nums.size(); i ++ ) { 7 | int x = nums[i]; 8 | d = max(d, ++ cnt[x]); 9 | if (!minp.count(x)) minp[x] = i; 10 | maxp[x] = i; 11 | } 12 | int res = INT_MAX; 13 | for (auto x: nums) 14 | if (cnt[x] == d) 15 | res = min(res, maxp[x] - minp[x] + 1); 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC732.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendarThree { 2 | public: 3 | map S; 4 | MyCalendarThree() { 5 | 6 | } 7 | 8 | int book(int start, int end) { 9 | S[start]++, S[end]--; 10 | int sum = 0, res = 0; 11 | for(auto [k,v] : S) { 12 | sum += v; 13 | res = max(res, sum); 14 | } 15 | return res; 16 | } 17 | }; 18 | 19 | /** 20 | * Your MyCalendarThree object will be instantiated and called as such: 21 | * MyCalendarThree* obj = new MyCalendarThree(); 22 | * int param_1 = obj->book(start,end); 23 | */ -------------------------------------------------------------------------------- /LeetCode/LC860.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool lemonadeChange(vector& bills) { 4 | int five = 0, ten = 0; 5 | for(auto b : bills){ 6 | if(b == 5) five++; 7 | else if (b == 10){ 8 | if(!five) return false; 9 | five--; 10 | ten++; 11 | } 12 | else { 13 | if(five && ten) ten--, five--; 14 | else if(five >= 3) five -= 3; 15 | else return false; 16 | } 17 | } 18 | return true; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC877.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool stoneGame(vector& piles) { 4 | int n = piles.size(); 5 | vector> f(n, vector(n,0)); 6 | for(len = 1; len <= n; len++) { 7 | for(int i = 0; i + len - 1 < n; i++) { 8 | int j = i + len - 1; 9 | if(len == 1) f[i][j] = piles[i]; 10 | else { 11 | f[i][j] = max(piles[i] - f[i+1][j], piles[j] - f[i][j-1]); 12 | } 13 | } 14 | } 15 | return f[0][n-1] > 0; 16 | } 17 | }; -------------------------------------------------------------------------------- /ACwing/ac875.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long LL; 6 | 7 | 8 | LL qmi(int a, int b, int p) 9 | { 10 | LL res = 1 % p; 11 | while (b) 12 | { 13 | if (b & 1) res = res * a % p; 14 | a = a * (LL)a % p; 15 | b >>= 1; 16 | } 17 | return res; 18 | } 19 | 20 | 21 | int main() 22 | { 23 | int n; 24 | scanf("%d", &n); 25 | while (n -- ) 26 | { 27 | int a, b, p; 28 | scanf("%d%d%d", &a, &b, &p); 29 | printf("%lld\n", qmi(a, b, p)); 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /LeetCode/LC768.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxChunksToSorted(vector& a) { 4 | auto b = a; 5 | sort(b.begin(), b.end()); 6 | unordered_map cnt; 7 | int res = 0; 8 | for(int i = 0, s = 0; i < a.size(); i++) { 9 | if(cnt[a[i]] == 1) s--; 10 | else if(cnt[a[i]] == 0) s++; 11 | cnt[a[i]]--; 12 | if(cnt[b[i]] == -1) s--; 13 | else if(cnt[b[i]] == 0) s++; 14 | cnt[b[i]]++; 15 | if(!s) res++; 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /ACwing/ac897.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int N = 1010; 7 | 8 | int n, m; 9 | char a[N], b[N]; 10 | int f[N][N]; 11 | 12 | int main() 13 | { 14 | scanf("%d%d", &n, &m); 15 | scanf("%s%s", a + 1, b + 1); 16 | 17 | for (int i = 1; i <= n; i ++ ) 18 | for (int j = 1; j <= m; j ++ ) 19 | { 20 | f[i][j] = max(f[i - 1][j], f[i][j - 1]); 21 | if (a[i] == b[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1); 22 | } 23 | 24 | printf("%d\n", f[n][m]); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /ACwing/ohter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | 9 | const int N = 110; 10 | int a[N], b[N]; 11 | 12 | 13 | 14 | 15 | 16 | int main(){ 17 | for(int i = 1; i <= 100; i++) a[i] = i; 18 | 19 | int range = 100; 20 | 21 | for(int i = 0; i < 100; i++){ 22 | int r = rand() % range; 23 | b[i] = a[r]; 24 | a[r] = a[range-1]; 25 | range--; 26 | } 27 | 28 | for(int i = 0; i < 100; i ++) printf("%d ", b[i]); 29 | 30 | 31 | 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /LeetCode/LC203.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ListNode* removeElements(ListNode* head, int val) { 5 | if (head == nullptr) return head; 6 | ListNode *dummy = new ListNode(-1); 7 | ListNode *tail = dummy, *cur = head; 8 | while(cur) { 9 | while(cur->val == val && cur->next){ 10 | cur = cur->next; 11 | } 12 | if(cur->val == val && cur->next == nullptr){ 13 | tail->next = nullptr; 14 | break; 15 | } 16 | tail->next = cur; 17 | tail = cur; 18 | cur = cur->next; 19 | } 20 | return dummy->next; 21 | } -------------------------------------------------------------------------------- /LeetCode/LC374.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Forward declaration of guess API. 3 | * @param num your guess 4 | * @return -1 if num is lower than the guess number 5 | * 1 if num is higher than the guess number 6 | * otherwise return 0 7 | * int guess(int num); 8 | */ 9 | 10 | class Solution { 11 | public: 12 | int guessNumber(int n) { 13 | int l = 0, r = n; 14 | while(l < r) { 15 | int mid = (long long)l + r >> 1; 16 | if(guess(mid) <= 0) r = mid; 17 | else l = mid + 1; 18 | } 19 | return r; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/LC435.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int eraseOverlapIntervals(vector>& intervals) { 4 | int n = intervals.size(); 5 | if(n < 1) return 0; 6 | sort(intervals.begin(), intervals.end(), [](vectora, vector b) { 7 | return a[1] < b[1]; 8 | }); 9 | int res = 1, t = intervals[0][1]; 10 | for(int i = 1; i < n; i++){ 11 | if(intervals[i][0] >= t){ 12 | res++; 13 | t = intervals[i][1]; 14 | } 15 | } 16 | return n - res; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC486.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool PredictTheWinner(vector& nums) { 4 | int n = nums.size(); 5 | vector> f(n, vector(n, 0)); 6 | for(int len = 1; len <= n; len++) { 7 | for(int i = 0; i + len - 1 < n; i++) { 8 | int j = i + len - 1; 9 | if(len == 1) f[i][j] = nums[i]; 10 | else { 11 | f[i][j] = max(-f[i+1][j] + nums[i], -f[i][j-1]+ nums[j]); 12 | } 13 | } 14 | } 15 | return f[0][n-1] >= 0; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC649.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string predictPartyVictory(string senate) { 4 | int n = senate.size(); 5 | queue r, d; 6 | for(int i = 0; i < n; i++) { 7 | if(senate[i] == 'R') r.push(i); 8 | else d.push(i); 9 | } 10 | while(r.size() && d.size()) { 11 | if(r.front() < d.front()) r.push(r.front() + n); 12 | else d.push(d.front() + n); 13 | r.pop(); 14 | d.pop(); 15 | } 16 | if(r.size()) return "Radiant"; 17 | return "Dire"; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC680.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(string s, int i, int j) { 4 | while(i < j) { 5 | if(s[i] != s[j]) return false; 6 | i++, j--; 7 | } 8 | return true; 9 | } 10 | bool validPalindrome(string s) { 11 | int n = s.length(); 12 | for(int i = 0, j = n-1; i < n && j >= 0; i++, j--) { 13 | if(s[i] != s[j]) { 14 | if(check(s, i+1, j) || check(s, i, j-1)) return true; 15 | return false; 16 | } 17 | } 18 | return true; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC777.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canTransform(string start, string end) { 4 | int n = start.size(); 5 | int i = 0, j = 0; 6 | while(1) { 7 | while(i < n && start[i] == 'X') i++; 8 | while(j < n && end[j] == 'X') j++; 9 | if(i == n || j == n) break; 10 | if(start[i] != end[j]) return false; 11 | if(start[i] == 'L' && i < j) return false; 12 | if(end[j] == 'R' && j < i) return false; 13 | i++, j++; 14 | } 15 | return i == n && j == n; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC953.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAlienSorted(vector& words, string order) { 4 | unordered_map hash; 5 | for(int i = 0; i < order.size(); i++) hash[order[i]] = i; 6 | 7 | for(int i = 1; i < words.size(); i ++) { 8 | auto a = words[i-1], b = words[i]; 9 | int x = 0, y = 0; 10 | while(x < a.size() && y < b.size()){ 11 | if(hash[a[x]] < hash[b[y]]) break; 12 | if(hash[a[x]] > hash[b[y]]) return false; 13 | x++, y++; 14 | } 15 | if(x < a.size() && y == b.size()) return false; 16 | } 17 | return true; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC332.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vectorstring> ans; 4 | unordered_map> g; 5 | 6 | vector findItinerary(vector>& tickets) { 7 | for(auto &t : tickets) g[t[0]].insert(t[1]); 8 | dfs("JFK"); 9 | reverse(ans.begin(), ans.end()); 10 | return ans; 11 | } 12 | 13 | void dfs(string u) { 14 | while(g[u].size()){ 15 | auto ver = *g[u].begin(); 16 | g[u].erase(g[u].begin()); 17 | dfs(ver); 18 | } 19 | ans.push_back(u); 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/LC647.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countSubstrings(string s) { 4 | int res = 0; 5 | for(int len = 0; len < s.size(); len++) { 6 | // 奇数情况 7 | for(int j = len, k = len; j >= 0 && k < s.size(); j--, k++) { 8 | if(s[j] != s[k]) break; 9 | res++; 10 | } 11 | // 偶数情况 12 | for(int j = len, k = len+1; j >= 0 && k < s.size(); j--,k++) { 13 | if(s[j] != s[k]) break; 14 | res++; 15 | } 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC581.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findUnsortedSubarray(vector& nums) { 4 | int n = nums.size(), l = 0, r = n - 1; 5 | if(n == 1) return 0; 6 | while(l+1 < n && nums[l+1] >= nums[l]) l++; 7 | if (l == r) return 0; 8 | while(r - 1 >= 0 && nums[r-1] <= nums[r]) r--; 9 | for(int i = l + 1; i < n; i++) { 10 | while(l >= 0 && nums[i] < nums[l]) l--; 11 | } 12 | for(int i = r - 1; i >= 0; i--) { 13 | while(r < n && nums[i] > nums[r]) r++; 14 | } 15 | return r - l - 1; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC753.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | unordered_set S; 4 | string ans; 5 | int k; 6 | 7 | void dfs(string u) { 8 | for (int i = 0; i < k; i ++ ) { 9 | auto v = u + to_string(i); 10 | if (!S.count(v)) { 11 | S.insert(v); 12 | dfs(v.substr(1)); 13 | ans += to_string(i); 14 | } 15 | } 16 | } 17 | 18 | string crackSafe(int n, int _k) { 19 | k = _k; 20 | string start(n - 1, '0'); 21 | dfs(start); 22 | return ans + start; 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode/LC799.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double champagneTower(int poured, int query_row, int query_glass) { 4 | vector> f(query_row + 1, vector(query_row+1, 0.0)); 5 | f[0][0] = poured; 6 | for(int i = 0; i < query_row; i++) { 7 | for(int j = 0; j <= i; j++) { 8 | if(f[i][j] > 1) { 9 | double x = (f[i][j] - 1) / 2; 10 | f[i+1][j] += x, f[i+1][j+1] += x; 11 | } 12 | } 13 | } 14 | return min(1.0, f[query_row][query_glass]); 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC8.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int myAtoi(string s) { 4 | int k = 0, n = s.size(); 5 | while(k < n && s[k] == ' ') k++; 6 | int minus = 1; 7 | if(s[k] == '-') minus = -1, k++; 8 | else if(s[k] == '+') k++; 9 | 10 | int res = 0; 11 | while(k < n && s[k] >= '0' && s[k] <= '9') { 12 | int x = s[k] - '0'; 13 | if(minus > 0 && res > (INT_MAX - x) / 10) return INT_MAX; 14 | if(minus < 0 && -res < (INT_MIN + x) / 10) return INT_MIN; 15 | if(-res * 10 - x == INT_MIN) return INT_MIN; 16 | res = res * 10 + x; 17 | k++; 18 | } 19 | return res; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/LC438.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findAnagrams(string s, string p) { 4 | unordered_map hash; 5 | for(auto c : p) hash[c]++; 6 | vector res; 7 | int n = s.size(), t = hash.size(), pn = p.size(); 8 | for(int i = 0, j = 0, k = 0; i < n; i++) { 9 | if(--hash[s[i]] == 0) k++; 10 | while(i - j + 1 > pn) { 11 | if(hash[s[j]] == 0) k--; 12 | hash[s[j++]]++; 13 | } 14 | if(k == t) res.push_back(j); 15 | } 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC474.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxForm(vector& strs, int m, int n) { 4 | vector> f(m+1, vector(n+1)); 5 | for(auto &s : strs) { 6 | int a = 0, b = 0; 7 | for(auto c : s) { 8 | if(c == '0') a++; 9 | else b++; 10 | } 11 | for(int i = m; i >= a; i--) { 12 | for(int j = n; j >= b; j--) { 13 | f[i][j] = max(f[i][j], f[i-a][j-b] + 1); 14 | } 15 | } 16 | } 17 | return f[m][n]; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC900.cpp: -------------------------------------------------------------------------------- 1 | class RLEIterator { 2 | public: 3 | vector q; 4 | int k=0; 5 | RLEIterator(vector& encoding) { 6 | q = encoding; 7 | } 8 | 9 | int next(int n) { 10 | while(k < q.size() && n > q[k]) { 11 | n -= q[k]; 12 | k += 2; 13 | } 14 | if(k >= q.size()) return -1; 15 | q[k] -= n; 16 | return q[k+1]; 17 | } 18 | }; 19 | 20 | /** 21 | * Your RLEIterator object will be instantiated and called as such: 22 | * RLEIterator* obj = new RLEIterator(encoding); 23 | * int param_1 = obj->next(n); 24 | */ -------------------------------------------------------------------------------- /ACwing/ac800.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by devinchang on 2020/3/24. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | const int N = 100010; 9 | int A[N], B[N]; 10 | 11 | 12 | int n, m, x; 13 | 14 | 15 | 16 | int main(){ 17 | scanf("%d%d%d", &n, &m, &x); 18 | for(int i = 0; i < n; i++) scanf("%d", &A[i]); 19 | for(int i = 0; i < m; i++) scanf("%d", &B[i]); 20 | 21 | for(int i = 0, j = m-1; i < n; i++){ 22 | while(j >= 0 && A[i] + B[j] > x) j--; 23 | if(A[i] + B[j] == x) printf("%d %d", i, j); 24 | } 25 | 26 | 27 | 28 | 29 | return 0 ; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /LeetCode/LC559.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | public: 23 | int maxDepth(Node* root) { 24 | if(root == nullptr) return 0; 25 | int res = 0; 26 | for(auto c : root->children) res = max(res, maxDepth(c)); 27 | return res + 1; 28 | } 29 | }; -------------------------------------------------------------------------------- /LeetCode/LC756.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector g[7][7]; 4 | 5 | bool pyramidTransition(string bottom, vector& allowed) { 6 | for(auto& s : allowed) g[s[0]-'A'][s[1]-'A'].push_back(s[2]); 7 | return dfs(bottom, "", 0); 8 | } 9 | 10 | bool dfs(string bottom, string up, int u) { 11 | if(bottom.size() == 1) return true; 12 | if(u == bottom.size()-1) return dfs(up, "", 0); 13 | for(auto c : g[bottom[u]-'A'][bottom[u+1]-'A']) { 14 | if(dfs(bottom, up+c, u+1)) return true; 15 | } 16 | return false; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC787.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | const int INF = 1e8; 4 | 5 | int findCheapestPrice(int n, vector>& flights, int src, int dst, int K) { 6 | vector dist(n, INF); 7 | dist[src] = 0; 8 | K++; 9 | while(K--) { 10 | auto cur = dist; 11 | for(auto& e : flights) { 12 | int a = e[0], b = e[1], c = e[2]; 13 | cur[b] = min(cur[b], dist[a] + c); 14 | } 15 | dist = cur; 16 | } 17 | if(dist[dst] == INF) return -1; 18 | return dist[dst]; 19 | } 20 | }; -------------------------------------------------------------------------------- /ACwing/ac842.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | const int N = 10; 6 | int path[N]; 7 | bool st[N]; 8 | int n; 9 | 10 | 11 | void dfs(int u) { 12 | if(u == n) { 13 | for(int i = 0; i < n; i++) printf("%d ", path[i]); 14 | printf("\n"); 15 | return; 16 | } 17 | for(int i = 0; i < n; i++) { 18 | if(!st[i]) { 19 | path[u] = i+1; 20 | st[i] = true; 21 | dfs(u+1); 22 | st[i] = false; 23 | } 24 | } 25 | } 26 | 27 | 28 | int main() { 29 | scanf("%d", &n); 30 | dfs(0); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /LeetCode/LC17.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector res; 4 | string path; 5 | string phones[10] = { 6 | "", "", "abc", "def", 7 | "ghi", "jkl", "mno", 8 | "pqrs", "tuv", "wxyz" 9 | }; 10 | 11 | void dfs(string digits, int u) { 12 | if(u == digits.size()) { 13 | res.push_back(path); 14 | return; 15 | } 16 | for(auto &c : phones[digits[u]-'0']) { 17 | path += c; 18 | dfs(digits, u+1); 19 | path.pop_back(); 20 | } 21 | } 22 | 23 | vector letterCombinations(string digits) { 24 | if(digits.empty()) return {}; 25 | dfs(digits, 0); 26 | return res; 27 | } 28 | }; -------------------------------------------------------------------------------- /LeetCode/LC790.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTilings(int n) { 4 | const int MOD = 1e9 + 7; 5 | int w[4][4] = { 6 | {1, 1, 1, 1}, 7 | {0, 0, 1, 1}, 8 | {0, 1, 0, 1}, 9 | {1, 0, 0, 0} 10 | }; 11 | vector> f(n + 1, vector(4)); 12 | f[0][0] = 1; 13 | for (int i = 0; i < n; i ++ ) 14 | for (int j = 0; j < 4; j ++ ) 15 | for (int k = 0; k < 4; k ++ ) 16 | f[i + 1][k] = (f[i + 1][k] + f[i][j] * w[j][k]) % MOD; 17 | return f[n][0]; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC871.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minRefuelStops(int target, int startFuel, vector>& stations) { 4 | stations.push_back({target, 0}); 5 | priority_queue heap; 6 | int sum = startFuel, res = 0; 7 | for(auto p : stations){ 8 | int x = p[0], y = p[1]; 9 | while(heap.size() && sum < x){ 10 | sum += heap.top(); 11 | heap.pop(); 12 | res++; 13 | } 14 | if(sum < x) return -1; 15 | heap.push(y); 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/lc57.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> insert(vector>& a, vector& b) { 4 | vector> res; 5 | a.push_back(b); 6 | sort(a.begin(), a.end()); 7 | int st = a[0][0], ed = a[0][1]; 8 | for(int i = 1; i < a.size(); i++) { 9 | if(a[i][0] > ed) { 10 | res.push_back({st,ed}); 11 | st = a[i][0], ed = a[i][1]; 12 | } else { 13 | ed = max(ed, a[i][1]); 14 | } 15 | } 16 | res.push_back({st, ed}); 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /ACwing/ac1612.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int N = 1010; 6 | int dp[N][N]; 7 | int n, m; 8 | 9 | 10 | 11 | int main(){ 12 | scanf("%d%d", &n, &m); 13 | int ans = 0; 14 | for(int i = 1; i <= n; i++){ 15 | for(int j = 1; j <= m; j++){ 16 | int t; 17 | scanf("%d", &t); 18 | if(t != 0){ 19 | dp[i][j] = min(dp[i-1][j], min(dp[i][j-1], dp[i-1][j-1])) + 1; 20 | ans = max(ans, dp[i][j]); 21 | } 22 | } 23 | } 24 | printf("%d\n", ans * ans); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /ACwing/ac19.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | struct TreeNode { 7 | int val; 8 | TreeNode *left; 9 | TreeNode *right; 10 | TreeNode *father; 11 | TreeNode(int x) : val(x), left(NULL), right(NULL), father(NULL) {} 12 | }; 13 | 14 | class Solution { 15 | public: 16 | TreeNode* inorderSuccessor(TreeNode* p) { 17 | if(p->right){ 18 | p = p->right; 19 | while(p->left) p = p->left; 20 | return p; 21 | } 22 | while(p->father && p == p->father->right) p = p->father; 23 | 24 | return p->father; 25 | 26 | } 27 | }; -------------------------------------------------------------------------------- /ACwing/ac906.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | typedef pair PII; 7 | 8 | 9 | 10 | int main() { 11 | int n; 12 | cin >> n; 13 | vector range; 14 | for(int i = 0; i < n; i++) { 15 | int l, r; 16 | cin >> l >> r; 17 | range.push_back({l, r}); 18 | } 19 | sort(range.begin(), range.end(), [&](PII a, PII b) { 20 | return a.second < b.second; 21 | }); 22 | int res = 0, ed = -2e9; 23 | for(auto &r : range) { 24 | if(r.first > ed) { 25 | res++; 26 | ed = r.second; 27 | } 28 | } 29 | cout << res << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /LeetCode/LC503.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector nextGreaterElements(vector& nums) { 4 | int n = nums.size(); 5 | nums.insert(nums.end(), nums.begin(), nums.end()); 6 | stack st; 7 | vector res(n); 8 | for(int i = n * 2 - 1; i >= 0; i--) { 9 | int x = nums[i]; 10 | while(st.size() && x >= st.top()) st.pop(); 11 | if(i < n) { 12 | if(st.empty()) res[i] = -1; 13 | else res[i] = st.top(); 14 | } 15 | st.push(x); 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC524.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(string a, string b) { 4 | int i = 0, j = 0; 5 | while(i < a.size() && j < b.size()) { 6 | if(a[i] == b[j]) i++; 7 | j++; 8 | } 9 | return i == a.size(); 10 | } 11 | string findLongestWord(string s, vector& d) { 12 | string res; 13 | for(auto str : d) { 14 | if(check(str, s)) { 15 | if(res.empty() || res.size() < str.size() || res.size() == str.size() && res > str) res = str; 16 | } 17 | } 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC813.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | double largestSumOfAverages(vector& A, int m) { 4 | int n = A.size(); 5 | vector> f(n+1, vector(m+1, -1e9)); 6 | f[0][0] = 0; 7 | vector s(n+1); 8 | for(int i = 1; i <= n; i++) s[i] = s[i-1] + A[i-1]; 9 | for(int i = 1; i <= n; i++) { 10 | for(int j = 1; j <= m; j++) { 11 | for(int k = 0; k < i; k++) 12 | f[i][j] = max(f[i][j], f[k][j-1] + (s[i] - s[k]) / (double)(i-k)); 13 | } 14 | } 15 | return f[n][m]; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC845.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestMountain(vector& arr) { 4 | int n = arr.size(); 5 | vector L(n, 1), R(n, 1); 6 | for (int i = 1; i < n; i ++ ) 7 | if (arr[i] > arr[i - 1]) 8 | L[i] = L[i - 1] + 1; 9 | for (int i = n - 2; i >= 0; i -- ) 10 | if (arr[i+1] < arr[i]) 11 | R[i] = R[i + 1] + 1; 12 | 13 | int res = 0; 14 | for (int i = 0; i < n; i ++ ) 15 | if (L[i] > 1 && R[i] > 1) 16 | res = max(res, L[i] + R[i] - 1); 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC876.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* middleNode(ListNode* head) { 14 | ListNode *slow = head, *fast = head; 15 | while(fast && fast->next) { 16 | slow = slow->next; 17 | fast = fast->next->next; 18 | } 19 | return slow; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/LC879.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int profitableSchemes(int n, int m, vector& group, vector& profit) { 4 | const int MOD = 1e9 + 7; 5 | vector> f(n+1, vector(m+1)); 6 | for(int i = 0; i <= n; i++) f[i][0] = 1; 7 | for(int i = 0; i < group.size(); i++) { 8 | int g = group[i], p = profit[i]; 9 | for(int j = n; j >= g; j--) { 10 | for(int k = m; k >= 0; k--) 11 | f[j][k] = (f[j][k] + f[j-g][max(0, k - p)]) % MOD; 12 | } 13 | } 14 | return f[n][m]; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC165.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | int compareVersion(string version1, string version2) { 4 | int n1 = version1.size(), n2 = version2.size(); 5 | for(int i = 0, j = 0; i < n1 || j < n2; i++, j++){ 6 | int nums1 = 0, nums2 = 0; 7 | while(i < n1 && version1[i] != '.') { 8 | nums1 = nums1*10 + version1[i]-'0'; 9 | i++; 10 | } 11 | while(j < n2 && version2[j] != '.') { 12 | nums2 = nums2*10 + version2[j]-'0'; 13 | j++; 14 | } 15 | if(nums1 < nums2) return -1; 16 | else if (nums1 > nums2) return 1; 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /LeetCode/LC206.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ListNode *reverList(ListNode *head) { 5 | if(head == nullptr) return head; 6 | ListNode *dummy = new ListNode(-1); 7 | ListNode *cur = head; 8 | while(cur) { 9 | ListNode *next = cur->next; 10 | cur->next = dummy->next; 11 | dummy->next = cur; 12 | cur = next; 13 | } 14 | return dummy->next; 15 | } 16 | 17 | 18 | 19 | ListNode *reverseList(ListNode *head){ 20 | if(!head || !head->next) return head; 21 | ListNode *tail = reverseList(head->next); 22 | head->next->next = head; 23 | head->next = nullptr; 24 | return tail; 25 | } -------------------------------------------------------------------------------- /LeetCode/LC299.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string getHint(string secret, string guess) { 4 | vector hash(10, 0); 5 | int bulls = 0, crows = 0; 6 | int n = secret.size(); 7 | for(int i = 0; i < n; i++){ 8 | hash[secret[i] - '0']++; 9 | if(secret[i] == guess[i]) bulls++; 10 | } 11 | for(int i = 0; i < n; i++){ 12 | if(hash[guess[i]-'0'] > 0){ 13 | crows++; 14 | hash[guess[i]-'0']--; 15 | } 16 | } 17 | return to_string(bulls)+"A" + to_string(crows-bulls)+"B"; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC1337.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector kWeakestRows(vector>& mat, int k) { 4 | int n = mat.size(); 5 | vector res; 6 | if(n < 1) return res; 7 | int m = mat[0].size(); 8 | vector> arr; 9 | for(int i = 0; i < n; i++) { 10 | pair tmp; 11 | for(int j = 0; j < m; j++) { 12 | if(mat[i][j] == 1) { 13 | tmp.first++; 14 | } 15 | } 16 | tmp.second = i; 17 | arr.push_back(tmp); 18 | } 19 | sort(arr.begin(), arr.end()); 20 | for(int i = 0; i < k; i++) { 21 | res.push_back(arr[i].second); 22 | } 23 | return res; 24 | } 25 | }; -------------------------------------------------------------------------------- /LeetCode/LC59.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generateMatrix(int n) { 4 | vector> res(n, vector(n)); 5 | 6 | int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; 7 | for (int i = 1, x = 0, y = 0, d = 0; i <= n * n; i ++ ) { 8 | res[x][y] = i; 9 | int a = x + dx[d], b = y + dy[d]; 10 | if (a < 0 || a >= n || b < 0 || b >= n || res[a][b]) { 11 | d = (d + 1) % 4; 12 | a = x + dx[d], b = y + dy[d]; 13 | } 14 | x = a, y = b; 15 | } 16 | 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC804.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniqueMorseRepresentations(vector& words) { 4 | string pos[26] = { 5 | ".-","-...","-.-.","-..",".", 6 | "..-.","--.","....","..",".---", 7 | "-.-",".-..","--","-.","---", 8 | ".--.","--.-",".-.","...","-", 9 | "..-","...-",".--","-..-","-.--","--.." 10 | }; 11 | unordered_set S; 12 | for (auto& w: words) { 13 | string s; 14 | for (auto c: w) s += pos[c - 'a']; 15 | S.insert(s); 16 | } 17 | return S.size(); 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC931.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minFallingPathSum(vector>& matrix) { 4 | int n = matrix.size(), m = matrix[0].size(); 5 | vector> f(n, vector(m, INT_MAX)); 6 | 7 | for(int i = 0; i < m; i++) f[0][i] = matrix[0][i]; 8 | 9 | for(int i = 1; i < n; i++) { 10 | for(int j = 0; j < m; j++) { 11 | for(int k = max(0, j - 1); k <= min(m-1, j+1); k++) { 12 | f[i][j] = min(f[i][j], f[i-1][k] + matrix[i][j]); 13 | } 14 | } 15 | } 16 | 17 | int res = INT_MAX; 18 | for(int i = 0; i < m; i++) res = min(res, f[n-1][i]); 19 | 20 | return res; 21 | } 22 | }; -------------------------------------------------------------------------------- /ACwing/ac799.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by devinchang on 2020/3/24. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | 9 | 10 | const int N = 100010; 11 | 12 | int arr[N], tmp[N]; 13 | 14 | 15 | int n; 16 | 17 | 18 | int main(){ 19 | scanf("%d", &n); 20 | for(int i = 0; i < n; i++) scanf("%d", &arr[i]); 21 | 22 | 23 | int ans = 1; 24 | for(int i = 0, j = 0; i < n; i++){ 25 | tmp[arr[i]]++; 26 | while(j < i && tmp[arr[i]] > 1) 27 | tmp[arr[j++]]--; 28 | ans = max(ans, i - j + 1); 29 | } 30 | 31 | printf("%d\n", ans); 32 | 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /LeetCode/LC467.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findSubstringInWraproundString(string p) { 4 | int n = p.size(); 5 | unordered_map cnt; 6 | for(int i = 0, j = 0; i < n;) { 7 | j = i + 1; 8 | // 分段 9 | while(j < n && (p[j] - p[j-1]==1 || p[j] - p[j-1] == -25)) j++; 10 | // 统计分段内以每个字母开头的子串的最大长度 11 | while(i < j) { 12 | cnt[p[i]] = max(cnt[p[i]], j - i); 13 | i++; 14 | } 15 | } 16 | int res = 0; 17 | for(auto [a, b] : cnt) res += b; 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC526.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countArrangement(int n) { 4 | vector f(1 << n); 5 | f[0] = 1; 6 | for (int i = 0; i < 1 << n; i ++ ) { 7 | int k = 0; 8 | for (int j = 0; j < n; j ++ ) 9 | if (i >> j & 1) 10 | k ++ ; 11 | for (int j = 0; j < n; j ++ ) 12 | if (!(i >> j & 1)) { 13 | if ((k + 1) % (j + 1) == 0 || (j + 1) % (k + 1) == 0) 14 | f[i | (1 << j)] += f[i]; 15 | } 16 | } 17 | return f[(1 << n) - 1]; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/LC664.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int strangePrinter(string s) { 4 | int n = s.size(); 5 | if (!n) return 0; 6 | vector> f(n + 1, vector(n + 1)); 7 | for (int len = 1; len <= n; len ++ ) 8 | for (int i = 0; i + len - 1 < n; i ++ ) { 9 | int j = i + len - 1; 10 | f[i][j] = 1 + f[i + 1][j]; 11 | for (int k = i + 1; k <= j; k ++ ) 12 | if (s[k] == s[i]) 13 | f[i][j] = min(f[i][j], f[i][k - 1] + f[k + 1][j]); 14 | } 15 | return f[0][n - 1]; 16 | } 17 | }; -------------------------------------------------------------------------------- /ACwing/ac902.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N = 1010; 5 | int n, m; 6 | char a[N], b[N]; 7 | int f[N][N]; 8 | 9 | 10 | int main() { 11 | scanf("%d%s", &n, a + 1); 12 | scanf("%d%s", &m, b + 1); 13 | for(int i = 0; i <= m; i++) f[0][i] = i; 14 | for(int i = 0; i <= n; i++) f[i][0] = i; 15 | 16 | for(int i = 1; i <= n; i++) { 17 | for(int j = 1; j <= m; j++) { 18 | f[i][j] = min(f[i-1][j], f[i][j-1]) + 1; 19 | if(a[i] == b[j]) f[i][j] = min(f[i][j], f[i-1][j-1]); 20 | else f[i][j] = min(f[i][j], f[i-1][j-1] + 1); 21 | } 22 | } 23 | 24 | printf("%d\n", f[n][m]); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /LeetCode/LC500.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findWords(vector& words) { 4 | string line[3] = { 5 | "qwertyuiop", 6 | "asdfghjkl", 7 | "zxcvbnm" 8 | }; 9 | unordered_map hash; 10 | for(int i = 0; i < 3; i++) { 11 | for(auto c : line[i]) hash[c] = i; 12 | } 13 | vector res; 14 | for(auto w : words) { 15 | set s; 16 | for(auto c : w) s.insert(hash[tolower(c)]); 17 | if(s.size() == 1) res.push_back(w); 18 | } 19 | return res; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/LC516.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindromeSubseq(string s) { 4 | int n = s.size(); 5 | vector> f(n, vector(n, 0)); 6 | for(int len = 1; len <= n; len++) { 7 | for(int i = 0; i + len - 1 < n; i++) { 8 | int j = i + len -1; 9 | if(len == 1) f[i][j] = 1; 10 | else { 11 | if(s[i] == s[j]) f[i][j] = f[i+1][j-1] + 2; 12 | else f[i][j] = max(f[i][j], max(f[i+1][j], f[i][j-1])); 13 | } 14 | } 15 | } 16 | return f[0][n-1]; 17 | } 18 | }; -------------------------------------------------------------------------------- /ACwing/ac795.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by devinchang on 2020/3/24. 3 | // 4 | 5 | #include 6 | using namespace std; 7 | 8 | 9 | /* 前缀和 10 | */ 11 | 12 | 13 | const int N = 100010; 14 | 15 | int arr[N]; 16 | int S[N]; 17 | 18 | int n, m; 19 | 20 | 21 | 22 | int main(){ 23 | scanf("%d%d", &n, &m); 24 | 25 | for(int i = 1; i <= n; i++){ 26 | scanf("%d", &arr[i]); 27 | S[i] = S[i-1] + arr[i]; 28 | } 29 | 30 | while(m--){ 31 | int l, r; 32 | scanf("%d%d", &l, &r); 33 | printf("%d\n", S[r] - S[l-1]); 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /LeetCode/LC318.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& words) { 4 | vector state; 5 | for(auto w : words){ 6 | int s = 0; 7 | for(auto c : w){ 8 | s |= 1 << (c - 'a'); 9 | } 10 | state.push_back(s); 11 | } 12 | int n = words.size(); 13 | int res = 0; 14 | for (int i = 0; i < n; i ++ ) 15 | for (int j = i + 1; j < n; j ++ ) 16 | if ((state[i] & state[j]) == 0) 17 | res = max(res, (int)(words[i].size() * words[j].size())); 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC64.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minPathSum(vector>& grid) { 4 | int n = grid.size(); 5 | if(n < 1) return 0; 6 | int m = grid[0].size(); 7 | vector> f(n, vector(m, 0)); 8 | f[0][0] = grid[0][0]; 9 | for(int i = 1; i < n; i++) f[i][0] = f[i-1][0] + grid[i][0]; 10 | for(int i = 1; i < m; i++) f[0][i] = f[0][i-1] + grid[0][i]; 11 | for(int i = 1; i < n; i++) { 12 | for(int j = 1; j < m; j++) 13 | f[i][j] = min(f[i-1][j], f[i][j-1]) + grid[i][j]; 14 | } 15 | return f[n-1][m-1]; 16 | } 17 | }; -------------------------------------------------------------------------------- /ACwing/ac61.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Created by devinchang on 2020/3/24. 3 | // 4 | 5 | //#include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | const int N = 100010; 12 | 13 | int ihash[N]; 14 | 15 | 16 | 17 | 18 | int main(){ 19 | string s; 20 | cin >> s; 21 | int n = s.size(); 22 | int ans = 1; 23 | for(int i = 0, j = 0; i < n; i++){ 24 | ihash[s[i]-'0']++; 25 | while(j < i && ihash[s[i]-'0'] > 1) ihash[s[j++]-'0']--; 26 | ans = max(ans, i - j + 1); 27 | } 28 | 29 | printf("%d\n", ans); 30 | 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /LeetCode/LC220.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | bool containsNearbyAlmostDuplicate(vector& nums, int k, int t) { 5 | typedef long long LL; 6 | multiset ms; 7 | // 插入哨兵防止越界 8 | ms.insert(1e18), ms.insert(-1e18); 9 | int n = nums.size(); 10 | for (int i = 0, j = 0; i < n; i ++ ) { 11 | // 窗口大小大于k 12 | if (i - j > k) ms.erase(ms.find(nums[j ++ ])); 13 | int x = nums[i]; 14 | auto it = ms.lower_bound(x); 15 | if (*it - x <= t) return true; 16 | // 判断小于nums[i]的元素是否符合要 17 | -- it; 18 | if (x - *it <= t) return true; 19 | ms.insert(x); 20 | } 21 | return false; 22 | } -------------------------------------------------------------------------------- /LeetCode/LC222.cpp: -------------------------------------------------------------------------------- 1 | 2 | void preOrder(TreeNode *root, int &count) { 3 | if(root == nullptr) return; 4 | count++; 5 | preOrder(root->left); 6 | preOrder(root->right); 7 | } 8 | 9 | int countNodes(TreeNode* root) { 10 | int ret = 0; 11 | preOrder(root, ret); 12 | return ret; 13 | } 14 | 15 | 16 | int countNodes(TreeNode *root){ 17 | if(root == nullptr) return 0; 18 | auto l = root->left, r = root->right; 19 | int hl = 1, hr = 1; 20 | while(l) hl++, l = l->left; 21 | while(r) hr++, r = r->right; 22 | if(hl == hr) return (1 << hl) - 1; 23 | return countNodes(root->left) + countNodes(root->right) + 1; 24 | } -------------------------------------------------------------------------------- /LeetCode/LC331.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string s; 4 | int k; 5 | 6 | bool isValidSerialization(string preorder) { 7 | s = preorder + ','; 8 | k = 0; 9 | if(!dfs()) return false; 10 | return k == s.size(); 11 | } 12 | 13 | bool dfs(){ 14 | if(k == s.size()) return false; // 该判定表示k遍历到字符最后,而序列已经没有值的情况下,不合法 15 | // 当前的节点为空,需要略过去#和逗号,注意这个是正常返回,合法的,所有要返回true 16 | if(s[k] == '#'){ 17 | k += 2; 18 | return true; 19 | } 20 | while(s[k] != ',') k++; 21 | k++; // 跳过逗号 22 | return dfs() && dfs(); // 再判断左右子树是否合法 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode/LC34.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector searchRange(vector& nums, int target) { 4 | if(nums.empty()) return {-1, -1}; 5 | int n = nums.size(); 6 | vector res; 7 | int l = 0, r = n - 1; 8 | while(l < r) { 9 | int mid = l + r >> 1; 10 | if(nums[mid] >= target) r = mid; 11 | else l = mid + 1; 12 | } 13 | if(nums[r] != target) return {-1, -1}; 14 | res.push_back(r); 15 | l = 0, r = n-1; 16 | while(l < r) { 17 | int mid = l + r + 1 >> 1; 18 | if(nums[mid] <= target) l = mid; 19 | else r = mid - 1; 20 | } 21 | res.push_back(r); 22 | return res; 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode/LC765.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector p; 4 | 5 | int find(int x) { 6 | if (p[x] != x) p[x] = find(p[x]); 7 | return p[x]; 8 | } 9 | 10 | int minSwapsCouples(vector& row) { 11 | int n = row.size() / 2; 12 | for (int i = 0; i < n; i ++ ) p.push_back(i); 13 | 14 | int cnt = n; 15 | for (int i = 0; i < n * 2; i += 2) { 16 | int a = row[i] / 2, b = row[i + 1] / 2; 17 | if (find(a) != find(b)) { 18 | p[find(a)] = find(b); 19 | cnt -- ; 20 | } 21 | } 22 | return n - cnt; 23 | } 24 | }; -------------------------------------------------------------------------------- /LeetCode/LC901.cpp: -------------------------------------------------------------------------------- 1 | class StockSpanner { 2 | public: 3 | int k = 0; 4 | stack day, price; 5 | 6 | StockSpanner() { 7 | day.push(-1); 8 | price.push(1e6); 9 | } 10 | 11 | int next(int x) { 12 | while(price.top() <= x) { 13 | day.pop(); 14 | price.pop(); 15 | } 16 | int res = k - day.top(); 17 | day.push(k++); 18 | price.push(x); 19 | return res; 20 | } 21 | }; 22 | 23 | /** 24 | * Your StockSpanner object will be instantiated and called as such: 25 | * StockSpanner* obj = new StockSpanner(); 26 | * int param_1 = obj->next(price); 27 | */ -------------------------------------------------------------------------------- /ACwing/ac841.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef unsigned long long ULL; 5 | const int N = 100010, P = 131; 6 | ULL h[N], p[N]; 7 | int n, m; 8 | char str[N]; 9 | 10 | ULL get(int l, int r) { 11 | return h[r] - h[l-1] * p[r-l + 1]; 12 | } 13 | 14 | 15 | int main() { 16 | scanf("%d%d", &n, &m); 17 | scanf("%s", str+1); 18 | p[0] = 1; 19 | for(int i = 1; i <= n; i++) { 20 | p[i] = p[i-1] * P; 21 | h[i] = h[i-1] * P + str[i]; 22 | } 23 | while(m--) { 24 | int l1, r1, l2, r2; 25 | scanf("%d%d%d%d", &l1, &r1, &l2, &r2); 26 | if(get(l1, r1) == get(l2, r2)) puts("Yes"); 27 | else puts("No"); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /LeetCode/LC44.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMatch(string s, string p) { 4 | int n = s.size(), m = p.size(); 5 | s = ' ' + s, p = ' ' + p; 6 | vector> f(n+1, vector(m+1)); 7 | f[0][0] = true; 8 | for(int i = 0; i <= n; i++) { 9 | for(int j = 1; j <= m ;j++) { 10 | if(p[j] != '*') { 11 | f[i][j] = i && f[i-1][j-1] && (s[i] == p[j] || p[j] == '?'); 12 | } else { 13 | f[i][j] = f[i][j-1] || i && f[i-1][j]; 14 | } 15 | } 16 | } 17 | return f[n][m]; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC547.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector p; 4 | int find(int x) { 5 | if(p[x] != x) p[x] = find(p[x]); 6 | return p[x]; 7 | } 8 | 9 | int findCircleNum(vector>& M) { 10 | int n = M.size(); 11 | for(int i = 0; i < n; i++) p.push_back(i); 12 | int cnt = n; 13 | for(int i = 0; i < n; i++) { 14 | for(int j = 0; j < n; j++) { 15 | if(M[i][j] && find(i) != find(j)) { 16 | p[find(i)] = find(j); 17 | cnt--; 18 | } 19 | } 20 | } 21 | return cnt; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/LC629.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kInversePairs(int n, int k) { 4 | int mod = 1e9 + 7; 5 | vector> f(n+1, vector(k+1)); 6 | f[1][0] = 1; 7 | // f[i][j] 表示选择到第i个数能产生逆序对的数量 8 | // f[i][j] = f[i-1][j] + f[i-1][j-1] + .. + f[i-1][0] 9 | for (int i = 2; i <= n; i ++ ) { 10 | long long s = 0; 11 | for (int j = 0; j <= k; j ++ ) { 12 | s += f[i - 1][j]; 13 | if (j - i >= 0) s -= f[i - 1][j - i]; 14 | f[i][j] = s % mod; 15 | } 16 | } 17 | return (f[n][k] + mod) % mod; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC719.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int get(vector& nums, int mid) { 4 | int res = 0; 5 | for(int i = 0, j = 0; i < nums.size(); i++) { 6 | while(nums[i] - nums[j] > mid) j++; 7 | res += i - j; 8 | } 9 | return res; 10 | } 11 | int smallestDistancePair(vector& nums, int k) { 12 | sort(nums.begin(), nums.end()); 13 | int l = 0, r = nums.back() - nums[0]; 14 | while(l < r) { 15 | int mid = (l + r) >> 1; 16 | if(get(nums, mid) >= k) r = mid; 17 | else l = mid +1; 18 | } 19 | return r; 20 | } 21 | }; -------------------------------------------------------------------------------- /LeetCode/LC731.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendarTwo { 2 | public: 3 | map S; 4 | MyCalendarTwo() { 5 | 6 | } 7 | 8 | bool book(int start, int end) { 9 | S[start]++, S[end]--; 10 | int sum = 0; 11 | for(auto [k,v] : S) { 12 | sum += v; 13 | if(sum >= 3) { 14 | S[start]--, S[end]++; 15 | return false; 16 | } 17 | } 18 | return true; 19 | } 20 | }; 21 | 22 | /** 23 | * Your MyCalendarTwo object will be instantiated and called as such: 24 | * MyCalendarTwo* obj = new MyCalendarTwo(); 25 | * bool param_1 = obj->book(start,end); 26 | */ -------------------------------------------------------------------------------- /LeetCode/LC807.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxIncreaseKeepingSkyline(vector>& grid) { 4 | int n = grid.size(), m = grid[0].size(); 5 | vector r(n), c(m); 6 | for(int i = 0; i < n; i++) { 7 | for(int j = 0; j < m; j++) { 8 | r[i] = max(r[i], grid[i][j]); 9 | c[j] = max(c[j], grid[i][j]); 10 | } 11 | } 12 | int res = 0; 13 | for(int i = 0; i < n; i++) { 14 | for(int j = 0; j < m; j++) { 15 | res += min(r[i], c[j]) - grid[i][j]; 16 | } 17 | } 18 | return res; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC835.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestOverlap(vector>& img1, vector>& img2) { 4 | int res = 0; 5 | int n = img1.size(); 6 | for (int i = -n; i < n; i ++ ) 7 | for (int j = -n; j < n; j ++ ) { 8 | int cnt = 0; 9 | for (int x = max(0, -i); x < min(n, n - i); x ++ ) 10 | for (int y = max(0, -j); y < min(n, n - j); y ++ ) 11 | if (img1[i + x][j + y] && img2[x][y]) 12 | cnt ++ ; 13 | res = max(res, cnt); 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /LeetCode/LC937.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector reorderLogFiles(vector& logs) { 4 | stable_sort(logs.begin(), logs.end(), [](const string& a, const string& b) { 5 | int x = a.find(' '), y = b.find(' '); 6 | auto a1 = a.substr(0, x), b1 = b.substr(0, y); 7 | auto a2 = a.substr(x + 1), b2 = b.substr(y + 1); 8 | auto a3 = isdigit(a2[0]), b3 = isdigit(b2[0]); 9 | if (a3 != b3) return a3 < b3; 10 | if (a3) return false; 11 | if (a2 != b2) return a2 < b2; 12 | return a1 < b1; 13 | }); 14 | return logs; 15 | } 16 | }; -------------------------------------------------------------------------------- /LeetCode/LC583.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string word1, string word2) { 4 | int n = word1.size(), m = word2.size(); 5 | vector> f(n+1, vector(m+1, 0)); 6 | for(int i = 0; i <= n; i++) f[i][0] = i; 7 | for(int i = 0; i <= m; i++) f[0][i] = i; 8 | for(int i = 1; i <= n; i++) { 9 | for(int j = 1; j <= m; j++) { 10 | if(word1[i-1] == word2[j-1]) f[i][j] = f[i-1][j-1]; 11 | else { 12 | f[i][j] = min(f[i-1][j], f[i][j-1]) + 1; 13 | } 14 | } 15 | } 16 | return f[n][m]; 17 | } 18 | }; -------------------------------------------------------------------------------- /LeetCode/LC690.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for Employee. 3 | class Employee { 4 | public: 5 | int id; 6 | int importance; 7 | vector subordinates; 8 | }; 9 | */ 10 | 11 | class Solution { 12 | public: 13 | unordered_map hash; 14 | 15 | int dfs(int id) { 16 | auto p = hash[id]; 17 | int res = p->importance; 18 | for(auto sub : p->subordinates) { 19 | res += dfs(sub); 20 | } 21 | return res; 22 | } 23 | 24 | int getImportance(vector employees, int id) { 25 | for(auto e : employees) hash[e->id] = e; 26 | return dfs(id); 27 | } 28 | }; -------------------------------------------------------------------------------- /LeetCode/LC788.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | unordered_set s1, s2; 4 | bool judge(int x) { 5 | bool is_diff = false; 6 | while(x) { 7 | int d = x % 10; 8 | if(s2.count(d)) is_diff = true; 9 | else if (!s1.count(d)) return false; 10 | x /= 10; 11 | } 12 | return is_diff; 13 | } 14 | int rotatedDigits(int N) { 15 | s1 = unordered_set{0, 1, 8}; 16 | s2 = unordered_set{2, 5, 6, 9}; 17 | int res = 0; 18 | for(int i = 0; i <= N; i++) { 19 | if(judge(i)) res++; 20 | } 21 | return res; 22 | } 23 | }; -------------------------------------------------------------------------------- /LeetCode/LC502.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaximizedCapital(int k, int W, vector& Profits, vector& Capital) { 4 | vector> q; 5 | int n = Profits.size(); 6 | for(int i = 0; i < n; i++) q.push_back({Capital[i], Profits[i]}); 7 | sort(q.begin(), q.end()); 8 | priority_queue heap; 9 | int i = 0; 10 | while(k--) { 11 | while(i < n && q[i].first <= W) heap.push(q[i].second), i++; 12 | if(heap.empty()) break; 13 | auto t = heap.top(); 14 | W += t; 15 | heap.pop(); 16 | } 17 | return W; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC812.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int cross(int x1, int y1, int x2, int y2) { 4 | return x1 * y2 - x2 * y1; 5 | } 6 | int area(vector&a, vector& b, vector& c) { 7 | return cross(b[0]-a[0], b[1]-a[1], c[0]-a[0], c[1]-a[1]); 8 | } 9 | double largestTriangleArea(vector>& points) { 10 | int res = 0; 11 | for(auto& a : points) { 12 | for(auto& b : points) { 13 | for(auto& c : points) { 14 | res = max(res, abs(area(a, b, c))); 15 | } 16 | } 17 | } 18 | return res / 2.0; 19 | } 20 | }; -------------------------------------------------------------------------------- /LeetCode/LC890.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(string& a, string& b) { 4 | unordered_map f, g; 5 | for(int i = 0; i < a.size(); i++) { 6 | if(f.count(a[i]) && f[a[i]] != b[i]) return false; 7 | if(g.count(b[i]) && g[b[i]] != a[i]) return false; 8 | f[a[i]] = b[i], g[b[i]] = a[i]; 9 | } 10 | return true; 11 | } 12 | vector findAndReplacePattern(vector& words, string pattern) { 13 | vector res; 14 | for(auto& w : words) { 15 | if(check(w, pattern)) res.push_back(w); 16 | } 17 | return res; 18 | } 19 | }; -------------------------------------------------------------------------------- /LeetCode/LC207.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | bool canFinish(int n, vector> &prerequisites) { 4 | vector> g(n); 5 | vector d(n); 6 | for (auto& e: prerequisites) { 7 | int b = e[0], a = e[1]; 8 | g[a].push_back(b); 9 | d[b] ++ ; 10 | } 11 | 12 | queue q; 13 | for (int i = 0; i < n; i ++ ) 14 | if (d[i] == 0) 15 | q.push(i); 16 | 17 | int cnt = 0; 18 | while (q.size()) { 19 | auto t = q.front(); 20 | q.pop(); 21 | cnt ++ ; 22 | for (auto i : g[t]) 23 | if ( -- d[i] == 0) 24 | q.push(i); 25 | } 26 | 27 | return cnt == n; 28 | } --------------------------------------------------------------------------------