双指针专题 就是把前面几个专题的部分使用到双指针的题目进行挑选
class Solution {
public:
int removeElement(vector& nums, int val) {
int slowIndex = 0;
for(int fastIndex = 0; fastIndex < nums.size(); fastIndex++) {
if(val != nums[fastIndex]) {
nums[slowIndex++] = nums[fastIndex];
}
}
return slowIndex;
}
};
class Solution {
public:
int removeDuplicates(vector& nums) {
int slow = 0;
for(int fast = 0; fast < nums.size() - 1; fast++) {
if(nums[fast] != nums[fast + 1]) {
nums[++slow] = nums[fast + 1];
}
}
return slow + 1;
}
};
class Solution {
public:
void moveZeroes(vector& nums) {
int slow = 0;
for(int fast = 0; fast < nums.size(); fast++) {
if(0 != nums[fast]) {
nums[slow ++] = nums[fast];
}
}
for(int i = slow; i < nums.size(); i++) {
nums[i] = 0;
}
}
};
class Solution {
public:
bool backspaceCompare(string s, string t) {
int SSkipNum = 0; // 记录s的数量
int TSkipNum = 0; // 记录t的数量
int i = s.size() - 1;
int j = t.size() - 1;
while(1) {
while(i >= 0) {
if(s[i] == '#') SSkipNum++;
else {
if(SSkipNum > 0) SSkipNum--;
else break;
}
i--;
}
while(j >= 0) {
if(t[j] == '#') TSkipNum++;
else {
if(TSkipNum > 0) TSkipNum--;
else break;
}
j--;
}
// 后半部分#消除完了,接下来比较s[i] != t[j]
if(i < 0 || j < 0) break; // 遍历到头了
if(s[i] != t[j]) return false;
i--;
j--;
}
// 说明s和t同时遍历完毕
if(i == -1 && j == -1) return true;
return false;
}
};
class Solution {
public:
vector sortedSquares(vector& nums) {
int left = 0;
int right = nums.size() - 1;
vector vec(nums.size());
int i = nums.size() - 1;
while(left <= right) {
if(nums[left] * nums[left] > nums[right] * nums[right]) {
vec[i--] = nums[left] * nums[left];
left++;
} else {
vec[i--] = nums[right] * nums[right];
right--;
}
}
return vec;
}
};
class Solution {
public:
void reverseString(vector& s) {
int left = 0;
int right = s.size() - 1;
while(left < right) {
swap(s[left], s[right]);
left++;
right--;
}
}
};
# include
# include
using namespace std;
int main() {
string s;
cin >> s;
int front = s.size() - 1;
int num = 0;
for(int i = 0; i < s.size(); i++) {
if(s[i] >= '0' && s[i] <= '9') {
num++;
}
}
s.resize(num * 5 + front + 1);
int back = s.size() - 1;
for(; front < back; back--, front--) {
if(s[front] >= '0' && s[front] <= '9') {
s[back - 5] = 'n';
s[back - 4] = 'u';
s[back - 3] = 'm';
s[back - 2] = 'b';
s[back - 1] = 'e';
s[back] = 'r';
back -= 5;
} else {
s[back] = s[front];
}
}
cout << s;
}
class Solution {
public:
void reverse(string& s, int start, int end) {
while(start < end) {
swap(s[start], s[end]);
start++;
end--;
}
}
void removeExtraSpaces(string& s) {
int slow = 0;
for(int i = 0; i < s.size(); i++) {
if(s[i] != ' ') {
if(slow > 0) s[slow++] = ' ';
while(i < s.size() && s[i] != ' ') {
s[slow] = s[i];
slow++;
i++;
}
}
}
s.resize(slow);
}
string reverseWords(string s) {
removeExtraSpaces(s);
int start = 0;
reverse(s, start, s.size() - 1);
for(int i = 0; i <= s.size(); i++) {
if(s[i] == ' ' || i == s.size()) {
reverse(s, start, i - 1);
start = i + 1;
}
}
return s;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* slow = dummyHead;
ListNode* fast = dummyHead;
while(n-- && fast != nullptr) {
fast = fast->next;
}
// fast 在提前走一步,需要让slow指向删除节点的上一个节点
fast = fast->next;
while(fast != nullptr) {
fast = fast->next;
slow = slow->next;
}
// 找到倒数第N个节点
slow->next = slow->next->next;
return dummyHead->next;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* curA = headA;
ListNode* curB = headB;
while(curA != curB) {
if(curA == NULL) {
curA = headB;
} else {
curA = curA->next;
}
if(curB == NULL) {
curB = headA;
} else {
curB = curB->next;
}
}
return curA;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
while(fast != NULL && fast->next != NULL) {
// 双指针
slow = slow->next;
fast = fast->next->next;
// 快慢指针相遇
if(slow == fast) {
ListNode* index1 = fast;
ListNode* index2 = head;
while(index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index2;
}
}
return NULL;
}
};
class Solution {
public:
vector> threeSum(vector& nums) {
sort(nums.begin(), nums.end());
vector> result;
for(int i = 0; i < nums.size(); i++) {
if(nums[i] > 0 && i >= 0) break;
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.size() - 1;
while(left < right) {
if(nums[i] + nums[left] + nums[right] > 0) right--;
else if(nums[i] + nums[left] + nums[right] < 0) left++;
else {
result.push_back(vector{nums[i], nums[left], nums[right]});
while(left < right && nums[left] == nums[left + 1]) left++;
while(left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
}
}
}
return result;
}
};
class Solution {
public:
vector> fourSum(vector& nums, int target) {
sort(nums.begin(), nums.end());
vector> vec;
for(int i = 0; i < nums.size(); i++) {
if(nums[i] >= 0 && nums[i] > target) {
break;
}
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for(int j = i + 1; j < nums.size(); j++) {
if(nums[i] + nums[j] >= 0 && nums[i] + nums[j] > target) {
break;
}
if(j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int left = j + 1;
int right = nums.size() - 1;
while(left < right) {
if((long) nums[i] + nums[j] + nums[left] + nums[right] > target) right--;
else if((long) nums[i] + nums[j] + nums[left] + nums[right] < target) left++;
else {
vec.push_back(vector{nums[i], nums[j], nums[left], nums[right]});
while(left < right && nums[left] == nums[left + 1]) left++;
while(left < right && nums[right] == nums[right - 1]) right--;
left++;
right--;
}
}
}
}
return vec;
}
};
明天开始HOT100
坚持每天3道HoT100 + 复盘随想录的栈和队列专题
冲!!!
争取三天把项目复盘完!