双指针
数组
leetcode: 344 反转字符串
原地移除数组上的元素,数组上的元素,不能真正的删除,因此只能覆盖。下述代码中通过两个前后指针指向前端和末尾,在一个for循环下实现转换。
class Solution {
public:
void reverseString(vector<char>& s) {
for (int i = 0; i < s.size()/2; i++) {
swap(s[i],s[s.size() - i - 1]);
}
}
};
leetcode: 977 有序数组的平方
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
vector<int> result(nums.size(), 0);
int k = nums.size() - 1;
while(left <= right){
if(abs(nums[left]) > abs(nums[right])){
result[k--] = nums[left] * nums[left];
left++;
}
else{
result[k--] = nums[right] * nums[right];
right--;
}
}
return result;
}
};
Leetcode: 209 长度最小的子数组
滑动窗,实际上相当于双指针
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0, right = 0;
int result = INT32_MAX;
int sum = 0;
for(int right = 0; right < nums.size();right++){
sum += nums[right];
while(sum >= target){//特别注意这是是while,不是if,因为要左边缩小到最小
int length = right - left + 1;
if(length < result) result = length;
sum -= nums[left];
left++;
}
}
if(result == INT32_MAX) return false;
return result;
}
};
字符串
Leetcode: 27 移除元素
定义两个快慢指针,快指针指向跳过位置的下一个位置。慢指针用于对元素的赋值。
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int slow = 0;
for(int fast = 0; fast < nums.size(); fast++){
if(nums[fast] != val){
nums[slow++] = nums[fast];
}
}
return slow;
}
};
Leetcode: 151 反转字符串中的单词
分单词的反转。
- 删除字符串中额外的空格进行处理
- 整体字符串反转
- 遇到空格的话,小的字符反转
class Solution {
public:
void reverse(string& s, int start, int end){//定义反转的函数
for(int i = start, j = end; i < j; i++, j--){
swap(s[i], s[j]);
}
}
void removezero(string& s){//定义删除空格的操作,使用了双指针
int start = 0;
for(int fast = 0; fast < s.size(); fast++){
if(start != 0 && s[fast] != ' ') s[start++] = ' ';//如果在非字符首遇到不是空格的元素,说明是单词的开头,在前面加上一个空格
while(fast < s.size() && s[fast] != ' '){
s[start++] = s[fast++];//如果快指针没指到最后,并且没有遇到空格,复制单词
}
}
s.resize(start);//字符串长度resize
}
string reverseWords(string s) {
removezero(s);
reverse(s, 0, s.size() - 1);
int start = 0;
for(int i = 0; i <= s.size(); ++i){//注意是左闭右闭区间
if(i == s.size() || s[i] == ' '){//如果当前i元素等于‘ ’,那么就反转到i-1
reverse(s, start, i - 1);
start = i + 1;//从i后面一个元素开始
}
}
return s;
}
};
链表
Leetcode: 19 删除链表的倒数第N个节点
定义快慢指针来删除
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 = fast->next;
}
while(fast->next != NULL){
slow = slow->next;
fast = fast->next;
}
slow->next = slow->next->next;
return dummyhead->next;
}
};
Leetcode: 206反转链表
定义两个指针,pre和cur,其中temp用于保存断开链表之后的cur-next。
pre和cur一前一后,先将cur-next保存好,然后将pre连接到cur-next上,将pre箱右移动,cur向右移动。
/**
* 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,