class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head){
return head;//链表防范
}
ListNode*dummy=new ListNode(0,head);//防止头结点被删除先弄个哑结点
ListNode*cur=dummy;
while(cur->next&&cur->next->next){
if(cur->next->val==cur->next->next->val){
int x=cur->next->val;
while(cur->next&&cur->next->val==x){
cur->next=cur->next->next;
}//如果结点等于这个值一直往后
}
else{
cur=cur->next;//下一个结点
}
}
return dummy->next;//由哑结点返回链表
}
};
这个是标准双指针问题的进阶,即三指针,多加一层循环罢了。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n=nums.size();
sort(nums.begin(),nums.end());//sort函数排序
vector<vector<int>> ans;//建一个二维数组作容器以待返回
for(int first=0;first<n;first++){
if(first>0&&nums[first]==nums[first-1]){
continue;
}//这个判定条件
int third=n-1;
int target=-nums[first];
for(int second=first+1;second<n;second++){
if(second>first+1&&nums[second]==nums[second-1]){
continue;
}
while(second<third&&nums[second]+nums[third]>target){
third--;//注意second<third
}
if(second==third){
break;//指针重合即可break
}
if(nums[second]+nums[third]==target){
ans.push_back({nums[first],nums[second],nums[third]});
//push_back函数的使用,注意大括号
}
}
}
return ans;
}
};