diff --git a/solution/167.Two Sum II - Input array is sorted/Solution.cpp b/solution/167.Two Sum II - Input array is sorted/Solution.cpp new file mode 100644 index 0000000000000..0047bc1d85238 --- /dev/null +++ b/solution/167.Two Sum II - Input array is sorted/Solution.cpp @@ -0,0 +1,39 @@ +// 先看target是否是偶数, +// 偶数的话先排除numbers[i] == numbers[i+1] == target/2 +// 然后set查找 +// 存在的话 再折半查找定位 +class Solution { +public: + vector twoSum(vector& numbers, int target) { + set s(numbers.begin(), numbers.end()) ; + + int t = target ; + if (!(target & 1)) + t /= 2 ; + + for (int i = 0; i < numbers.size()-1; ++i) + { + if (numbers[i] == t && numbers[i] == numbers[i+1]) + return {i+1, i+2} ; + + int tar = target - numbers[i] ; + if (s.find(tar) != s.end()) + { + int l = i+1, r = numbers.size(), mid ; + while (l < r) + { + mid = (l+r) >> 1 ; + if (numbers[mid] > tar) + r = mid ; + else if (numbers[mid] < tar) + l = mid+1 ; + else + break ; + } + return {i+1, mid+1} ; + } + } + + return {0, 0} ; + } +}; \ No newline at end of file diff --git a/solution/438.Find All Anagrams in a String/Solution.cpp b/solution/438.Find All Anagrams in a String/Solution.cpp new file mode 100644 index 0000000000000..f40fe4c35dc6b --- /dev/null +++ b/solution/438.Find All Anagrams in a String/Solution.cpp @@ -0,0 +1,71 @@ +static const auto io_speed_up = []() +{ + std::ios::sync_with_stdio(false) ; + cin.tie(nullptr) ; + return 0 ; +}() ; + +class Solution { +public: + vector findAnagrams(string s, string p) { + int l = s.size() - p.size() ; + if (l < 0) + return {} ; + vector pt(128, 0) ; + vector st(128, 0) ; + vector res ; + + for (auto ch: p) + ++pt[ch] ; + + for (int i = 0; i <= l; ++i) + { + for (int j = 0; j <= p.size(); ++j) + { + if (p.size() == j) + { + if (match(st, pt)) + { + res.push_back(i) ; + } + --st[s[i]] ; + ++i ; + j-=2 ; + continue ; + } + + char ch = s[i+j] ; + if (0 == pt[ch]) + { + i = i+j ; + clear(st) ; + break ; + } + + if (pt[ch] < ++st[ch]) + { + clear(st) ; + break ; + } + } + } + + return res ; + } + + inline void clear(vector &v) + { + for (int i = 'a'; i <= 'z'; ++i) + v[i] = 0 ; + } + + inline bool match(vector &a, vector &b) + { + for (int i = 'a'; i <= 'z'; ++i) + if (a[i] != b[i]) + return false ; + return true ; + } + + +}; \ No newline at end of file diff --git a/solution/443.String Compression/Solution.cpp b/solution/443.String Compression/Solution.cpp new file mode 100644 index 0000000000000..0c8bce05d3c4d --- /dev/null +++ b/solution/443.String Compression/Solution.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + int compress(vector& chars) { + char pre = chars[0] ; + int cnt = 1 ; + int len = 0 ; + for (int i = 1; ; ++i) + { + if (i < chars.size() && chars[i] == pre) + ++cnt ; + else + { + chars[len++] = pre ; + + if (cnt > 1) + for (auto c: to_string(cnt)) + chars[len++] = c ; + + if (i >= chars.size()) + break ; + + pre = chars[i] ; + cnt = 1 ; + } + } + + return len ; + } +}; \ No newline at end of file diff --git a/solution/605.Can Place Flowers/Solution.cpp b/solution/605.Can Place Flowers/Solution.cpp new file mode 100644 index 0000000000000..99e5d98845fb6 --- /dev/null +++ b/solution/605.Can Place Flowers/Solution.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + bool canPlaceFlowers(vector& flowerbed, int n) { + int i = 0, j = flowerbed.size()-1 ; + int cnt=0 ; + int num ; + while (i <= j && !flowerbed[i]) + ++i ; + if (i > j) + return n <= 1 + (j>>1) ; + num = i>>1 ; + while (!flowerbed[j]) + --j ; + num += (flowerbed.size()-1-j)>>1 ; + //cout << i << ' ' << j << endl ; + while (i <= j) + { + + //cout << "num = " << num << ", cnt = " << cnt << endl ; + if (flowerbed[i]) + { + if (cnt > 0) + num += (cnt-1) >> 1 ; + cnt = 0 ; + } + else + cnt++ ; + ++i ; + } + //cout << num << endl ; + return num >= n ; + } +}; \ No newline at end of file diff --git a/solution/836.Rectangle Overlap/Solution.cpp b/solution/836.Rectangle Overlap/Solution.cpp new file mode 100644 index 0000000000000..e5f4b0259521c --- /dev/null +++ b/solution/836.Rectangle Overlap/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + bool isRectangleOverlap(vector& rec1, vector& rec2) { + int l1 = rec1[1], u1 = rec1[0], r1 = rec1[3], d1 = rec1[2] ; + int l2 = rec2[1], u2 = rec2[0], r2 = rec2[3], d2 = rec2[2] ; + + //printf("1: (%d,%d), (%d,%d)\n", l1, u1, r1, d1) ; + //printf("2: (%d,%d), (%d,%d)\n", l2, u2, r2, d2) ; + + if (l1 < r2 && u1 < d2 && l2 < r1 && u2 < d1) + return true ; + return false ; + } +}; \ No newline at end of file diff --git a/solution/874.Walking Robot Simulation/Solution.cpp b/solution/874.Walking Robot Simulation/Solution.cpp new file mode 100644 index 0000000000000..45136425b513f --- /dev/null +++ b/solution/874.Walking Robot Simulation/Solution.cpp @@ -0,0 +1,75 @@ +class Solution { +public: + int robotSim(vector& commands, vector>& obstacles) { + set> s ; + for (auto o: obstacles) + s.insert(pair(o[0], o[1])) ; + + int dir = 0 ; + int x = 0, y = 0 ; + int m = 0 ; + + for (auto c: commands) + { + //cout << c << ":" << x << ' ' << y << endl ; + long long d = x*x + y*y ; + if (m < d) + m = d ; + if (-2 == c) + { + dir += 3 ; + } + else if (-1 == c) + { + ++dir ; + } + else + { + int step = c ; + dir %= 4 ; + if (0 == dir) + { + while (step--) + { + if (s.find(pair(x, y+1)) != s.end()) + break ; + //cout << "++i" << endl ; + ++y ; + } + + } + else if (1 == dir) + { + while (step--) + { + if (s.find(pair(x+1, y)) != s.end()) + break ; + ++x ; + } + } + else if (2 == dir) + { + while (step--) + { + if (s.find(pair(x, y-1)) != s.end()) + break ; + --y ; + } + } + else if (3 == dir) + { + while (step--) + { + if (s.find(pair(x-1, y)) != s.end()) + break ; + --x ; + } + } + } + } + + //cout << ":" << x << ' ' << y << endl ; + + return max(m, x*x + y*y); + } +}; \ No newline at end of file