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. PC逆向之代码还原技术,第三讲汇编中加法的代码还原

    目录 PC逆向之代码还原技术,第三讲汇编中加法的代码还原 一丶汇编简介 二丶高级代码对应汇编观看. 1.代码还原解析: 总结 PC逆向之代码还原技术,第三讲汇编中加法的代码还原 一丶汇编简介 在讲解加 ...

  2. JDBC 异常简介 jDBC简介(六)

    SQL 异常简介 对于数据库的操作访问,必然也很可能抛出异常. JDBC中定义了SQLException,用于描述数据库相关操作中可能出现的异常情况. java.sql.SQLException    ...

  3. Odd-e CSD Course Day 1

    First 強烈的建議,記得準備好當地的 SIM 卡及插座轉接頭,在這五天中很好用的 接下來,我就各個主題來介紹一下相關的心得.首先我們這五天裡會依照 Scrum 的流程,完全的跑過一次,從一開始的需 ...

  4. .net 用ajaxFileUpload 上传超过20M文件设置

    1.在web.config的 <system.web>  节点里面添加   <httpRuntime targetFramework="4.5.2"  execu ...

  5. 学JAVA第五天,今天困得要死

    好不容易坚持到第五天了,继续继续!!! 今天老师没有讲JAVA的for循环,倒是讲了HTML的相关内容: 讲了JAVA代码怎么在HTML中运行. 只要在HTML加入这个 background-colo ...

  6. Django学习之四:Django Model模块

    目录 Django Model 模型 MODEL需要在脑子里记住的基础概念 区分清楚,必须不能混淆的 class Meta 内嵌元数据定义类 简单model创建实例 数据源配置 接着通过models在 ...

  7. 前端导出excel数据-jsonToExcel

    咳咳,好久没有写博了... 在工作中遇到了纯前端,将数据导出为excel文件.正文开始: 第一步 安装依赖: npm i xlsx 第二步 写导出函数: import XLSX from 'xlsx' ...

  8. 基本数据类型 int float str

    一.数字型1.整型 int======================================基本使用======================================1.用途 用来 ...

  9. Redis过期策略

    一.设置过期时间 expire key time(以秒为单位) -- 这是最常用的方式 setex(String key, int seconds, String value) -- 字符串独有的方式 ...

  10. Vue:如何在地图上添加自定义覆盖物(点)

    目录 如何在地图上添加自定义覆盖物(点) 首发日期:2019-1-25 如何在地图上添加自定义覆盖物(点) 此文重点是在地图上标点,所以就省去引入百度地图的步骤了. 先给一下最终的效果. 这个效果主要 ...