203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个;82也是删除重复的数值,但重复的都删除,不保留。

比如[1、2、2、3],83题要求的结果是[1、2、3],82题要求的结果是[1,3]。

这种题用递归去做比较方便思考,特别是这种重复的数值。递归就是只遍历当前的节点的情况,之前的不用管,之后的以当前节点为出发点思考问题。

203. Remove Linked List Elements

class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(head == NULL)
return NULL;
if(head->val == val)
return removeElements(head->next,val);
else{
head->next = removeElements(head->next,val);
return head;
}
}
};

注意这种边界条件:

[1,1]
1
Expected:
[]

83. Remove Duplicates from Sorted List

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == NULL)
return NULL;
int value = head->val;
while(head->next != NULL && head->next->val == value)
head->next = head->next->next;
head->next = deleteDuplicates(head->next);
return head;
}
};

82. Remove Duplicates from Sorted List II,这个题和剑指offer57 删除链表中重复的结点的题是一样的

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == NULL)
return NULL;
if(head->next == NULL)
return head;
if(head->val == head->next->val){
ListNode* node = head->next;
while(node != NULL && head->val == node->val)
node = node->next;
return deleteDuplicates(node);
}
else{
head->next = deleteDuplicates(head->next);
return head;
}
}
};

26、80两个题类似,第一个题是不允许有重复的数字,第二个题是允许每个数字最多重复两个,两个题目都要求在原数组上进行操作,并返回生成数组的长度,即空间复杂度为O(1)。

两个题都是使用双指针,第一个指针指向生成新的数组的最后一个位置,第二个指针指向当前进行判断的位置。

唯一不同的是, 第二个题需要设置一个变量来控制重复的个数是否超过2

26. Remove Duplicates from Sorted Array

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.empty())
return ;
int pre = ;
int cur = ;
while(cur < nums.size()){
if(nums[pre] == nums[cur])
cur++;
else
nums[++pre] = nums[cur++];
}
return pre + ;
}
};

80. Remove Duplicates from Sorted Array II

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.empty())
return ;
int pre = ;
int cur = ;
int count = ;
while(cur < nums.size()){
if(nums[pre] == nums[cur]){
if(count == )
cur++;
else{
nums[++pre] = nums[cur++];
count--;
}
}
else{
nums[++pre] = nums[cur++];
count = ;
}
}
return pre + ;
}
};

错误代码:

在这种情况下出错:{0,0,1,1,1,1,2,3,3};

这个代码用了多个if,多个if会重复计算,比如第一个if和第三个if会在同一次中重复计算。而我们想要的是每次计算一次。

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.empty())
return ;
int left = ,right = ,count = ;
while(right < nums.size()){
if(nums[left] == nums[right] && count != ){
nums[++left] = nums[right++];
count--;
}
if(nums[left] == nums[right] && count == )
right++;
if(nums[left] != nums[right]){
nums[++left] = nums[right++];
count = ;
}
}
return left + ;
}
};

leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)的更多相关文章

  1. LeetCode 剑指 Offer 22. 链表中倒数第k个节点

    剑指 Offer 22. 链表中倒数第k个节点 题意 输入一个链表,输出该链表中倒数第k个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点. ​ 例如,一个链表有 6 个 ...

  2. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

    237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...

  3. 203. Remove Linked List Elements - LeetCode

    Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...

  4. 【LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...

  5. (LeetCode 203)Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  6. 【刷题-LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...

  7. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  8. leetcode:Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  9. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

随机推荐

  1. 【Java基础】【21IO(字符流)&字符流其他内容&递归】

    21.01_IO流(字符流FileReader) 1.字符流是什么 字符流是可以直接读写字符的IO流 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写 ...

  2. Java开发知识之Java中的泛型

    Java开发知识之Java中的泛型 一丶简介什么是泛型. 泛型就是指泛指任何数据类型. 就是把数据类型用泛型替代了. 这样是可以的. 二丶Java中的泛型 Java中,所有类的父类都是Object类. ...

  3. javascript基础修炼(3)—What's this(下)

    开发者的javascript造诣取决于对[动态]和[异步]这两个词的理解水平. 这一期主要分析各种实际开发中各种复杂的this指向问题. 一. 严格模式 严格模式是ES5中添加的javascript的 ...

  4. [python] 小游戏 - play_plane

    GitHub:https://github.com/liqingwen2015/play_plane 目前只做了第一部分:一个界面,有个飞机,可以左右移动,放子弹. 暂无计划做第二部分. alien_ ...

  5. Java开发笔记(九)赋值运算符及其演化

    前面的加减乘除四则运算,计算结果通过等号输出给指定变量,注意此时代码把变量放到等号左边.而在算术课本里,加法运算的完整写法类似于“1+1=2”这样,运算结果应该跟在等号右边.不过代数课本里的方程式存在 ...

  6. react中的路由配置踩坑记

    react 路由配置中,如果根路由(/)匹配一个组件,另一个路由(/list)在进行匹配的时候也会匹配到根路由(/),即在 /list 页面展示的时候 / 页面总是展示在上方. 此时如果想进行严格匹配 ...

  7. Html和Css学习笔记-html进阶-div与span

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 此篇博客是我的复习笔记,html和css学的时间太久了,忘得差不多了,最近要使用一下,所以重新打开html的书略读,后记录了标签 ...

  8. 亚马逊 amazon connect(呼叫中心)

    背景 公司为提高客服部门沟通效率对接电话呼叫中心,调研后选择了亚马逊的Amazon Connect服务,因为是国外业务没有选择用阿里云,怕有坑. Amazon Connect后台 需要在后台创建“联系 ...

  9. 【代码笔记】Web-JavaScript-JavaScript JSON

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  10. 手机Soc芯片简介

    手机SoC(System On a Chip,在一个芯片里面集成CPU.GPU.SP.ISP.RAM内存.Wi-Fi控制器.基带芯片以及音频芯片等)芯片(基于arm架构指令集) 高通骁龙(Snapdr ...