Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d6c1a1a

Browse files
新增双指针部分题解
1 parent 2f394d4 commit d6c1a1a

23 files changed

+792
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @lc app=leetcode.cn id=653 lang=cpp
3+
*
4+
* [653] 两数之和 IV - 输入 BST
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
bool findTarget(TreeNode* root, int k) {
22+
if (root == nullptr)
23+
return false;
24+
if (st.find(k - root->val) != st.end())
25+
return true;
26+
st.insert(root->val);
27+
return findTarget(root->left, k) || findTarget(root->right, k);
28+
}
29+
private:
30+
unordered_set<int> st;
31+
};
32+
// @lc code=end
33+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode.cn id=1089 lang=cpp
3+
*
4+
* [1089] 复写零
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
// void duplicateZeros(vector<int>& arr) {
11+
// vector<int> a(arr);
12+
// int idx = 0;
13+
// for (int i = 0; i < a.size() - 1; i++) {
14+
// if (idx >= arr.size()) break;
15+
// if (a[i] == 0) {
16+
// arr[idx++] = 0;
17+
// if (idx >= arr.size()) break;
18+
// }
19+
// arr[idx++] = a[i];
20+
// }
21+
// }
22+
23+
void duplicateZeros(vector<int>& arr) {
24+
int j = 0;
25+
for (int i = 0; j < arr.size(); i++) {
26+
if ((arr[i] & 0x0f) == 0) {
27+
j += 2; // 0 - 9 高4位为0 不用操作
28+
} else {
29+
arr[j++] |= ((arr[i] & 0x0f) << 4); // 低四位为arr[i]原来的值
30+
}
31+
}
32+
for (int i = 0; i < arr.size(); i++) {
33+
arr[i] = (arr[i] >> 4);
34+
}
35+
}
36+
};
37+
// @lc code=end
38+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode.cn id=1332 lang=cpp
3+
*
4+
* [1332] 删除回文子序列
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int removePalindromeSub(string s) {
11+
// 因为只有 'a' 'b'
12+
// 所以最多只需要删除 2 次, 一次删除所有的 'a', 一次删除所有的 'b'
13+
if (s.size() == 0)
14+
return 0;
15+
int i = 0;
16+
int j = s.size() - 1;
17+
while (i < j) {
18+
if (s[i] != s[j]) {
19+
return 2;
20+
}
21+
i++;
22+
j--;
23+
}
24+
return 1;
25+
}
26+
};
27+
// @lc code=end
28+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode.cn id=1385 lang=cpp
3+
*
4+
* [1385] 两个数组间的距离值
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
11+
sort(arr2.begin(), arr2.end());
12+
13+
int cnt = 0;
14+
// | a - b | <= d
15+
// -d <= a - b <= d
16+
// a - d <= b <= a + d
17+
for (int a : arr1) {
18+
auto it1 = lower_bound(arr2.begin(), arr2.end(), a - d);
19+
auto it2 = upper_bound(arr2.begin(), arr2.end(), a + d);
20+
// cout << *it1 << " " << *it2 << endl;
21+
if (it1 < it2) continue;
22+
cnt++;
23+
}
24+
return cnt;
25+
}
26+
};
27+
// @lc code=end
28+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode.cn id=532 lang=cpp
3+
*
4+
* [532] 数组中的 k-diff 数对
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int findPairs(vector<int>& nums, int k) {
11+
sort(nums.begin(), nums.end());
12+
unordered_set<int> memo;
13+
unordered_set<int> ans;
14+
for (auto c : nums) {
15+
if (memo.find(c - k) != memo.end())
16+
ans.insert(c);
17+
if (memo.find(c + k) != memo.end())
18+
ans.insert(c);
19+
memo.insert(c);
20+
}
21+
return ans.size();
22+
}
23+
};
24+
// @lc code=end
25+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode id=541 lang=cpp
3+
*
4+
* [541] Reverse String II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
string reverseStr(string s, int k) {
11+
int left = 0;
12+
int right = 0;
13+
int n = s.size();
14+
while (left < n) {
15+
right = min(n - 1, left + k - 1);
16+
reverse(s, left, right);
17+
left = left + 2 * k;
18+
}
19+
return s;
20+
}
21+
void reverse(string& s, int left, int right) {
22+
while (left < right) {
23+
char c = s[left];
24+
s[left] = s[right];
25+
s[right] = c;
26+
left++;
27+
right--;
28+
}
29+
}
30+
};
31+
// @lc code=end
32+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=557 lang=cpp
3+
*
4+
* [557] Reverse Words in a String III
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
string reverseWords(string s) {
11+
// reverse(s, 0, s.size() - 1);
12+
int left = 0;
13+
int right = 0;
14+
while (left < s.size()) {
15+
while (right < s.size() && s[right] != ' ')
16+
right++;
17+
// cout << right << "/" << s.size() << " ";
18+
reverse(s, left, right - 1);
19+
// cout << "22/" << " " << endl;
20+
left = right = right + 1;
21+
}
22+
return s;
23+
}
24+
25+
void reverse(string& s, int left, int right) {
26+
while (left < right) {
27+
char c = s[left];
28+
s[left] = s[right];
29+
s[right] = c;
30+
left++;
31+
right--;
32+
}
33+
}
34+
};
35+
// @lc code=end
36+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode id=567 lang=cpp
3+
*
4+
* [567] Permutation in String
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool checkInclusion(string s1, string s2) {
11+
if (s2.size() < s1.size()) return false;
12+
vector<int> v1(26);
13+
vector<int> v2(26);
14+
// 滑动窗口
15+
int n = s1.size();
16+
for (int i = 0; i < n; i++) {
17+
v1[s1[i] - 'a']++;
18+
v2[s2[i] - 'a']++;
19+
}
20+
if (v1 == v2) return true;
21+
for (int i = n; i < s2.size(); i++) {
22+
v2[s2[i - n] - 'a']--;
23+
v2[s2[i] - 'a']++;
24+
if (v1 == v2) return true;
25+
}
26+
return false;
27+
}
28+
};
29+
// @lc code=end
30+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode.cn id=581 lang=cpp
3+
*
4+
* [581] 最短无序连续子数组
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
// TODO: 未完成
11+
int findUnsortedSubarray(vector<int>& nums) {
12+
int left = 0;
13+
int right = 0;
14+
for (int i = 1; i < nums.size(); i++) {
15+
if (nums[i] < nums[i-1]) {
16+
left = i - 1;
17+
right = i;
18+
break;
19+
}
20+
}
21+
if (left == 0 && right == 0)
22+
return 0;
23+
cout << left << " " << right << endl;
24+
for (int i = right; i < nums.size(); i++) {
25+
cout << " i=" << i << endl;;
26+
if (nums[i] < nums[i - 1]) {
27+
// 处理重复
28+
while (i < nums.size() - 1 && nums[i + 1] == nums[i])
29+
{
30+
i++;
31+
}
32+
right = i;
33+
}
34+
}
35+
36+
return right - left + 1;
37+
}
38+
};
39+
// @lc code=end
40+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode.cn id=611 lang=cpp
3+
*
4+
* [611] 有效三角形的个数
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int triangleNumber(vector<int>& nums) {
11+
int ans = 0;
12+
sort(nums.begin(), nums.end());
13+
for (int i = 0; i < nums.size(); i++) {
14+
for (int j = i + 1; j < nums.size(); j++) {
15+
// 二分法搜索
16+
int left = j + 1;
17+
int right = nums.size() - 1;
18+
int b = j;
19+
while (left <= right) {
20+
int mid = left + (right - left) / 2;
21+
if (nums[mid] < nums[i] + nums[j]) {
22+
left = mid + 1;
23+
b = mid;
24+
} else {
25+
right = mid - 1;
26+
}
27+
}
28+
// cout << left << " " << right << " " << j << endl;
29+
ans += (b - j);
30+
}
31+
}
32+
return ans;
33+
}
34+
35+
};
36+
// @lc code=end
37+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /