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. .NET Core2.1获取自定义配置文件信息

    前言 .net core来势已不可阻挡.既然挡不了,那我们就顺应它.了解它并学习它.今天我们就来看看和之前.net版本的配置文件读取方式有何异同,这里不在赘述.NET Core 基础知识. ps:更新 ...

  2. 【ASP.NET Core快速入门】(二)部署到IIS

    配置IIS模块 ASP.NET Core Module载地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/asp ...

  3. 【WCF系列】(四)WCF客户端怎么消费服务

    WCF客户端怎么消费服务 获取服务绑定协议.绑定和地址:实现方式 SvcUtil方式:SvcUtil.exe是一个命令行工具,位于:C:\Program Files (x86)\Microsoft S ...

  4. 关于微信小程序textarea层级过高问题解决

    一.前言 相信做过微信小程序开发的人应该都碰到过这个问题,因为这个需求实在太常见了 由于textarea是属于原生组件,层级最高,不管在页面中我们设置z-index值多少都会被textarea组件遮挡 ...

  5. 痞子衡嵌入式:语音处理工具Jays-PySPEECH诞生记(6)- 文语合成实现(pyttsx3, eSpeak1.48.04)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是语音处理工具Jays-PySPEECH诞生之文语合成实现. 文语合成是Jays-PySPEECH的核心功能,Jays-PySPEECH借 ...

  6. ROS笔记 Topics

    http://wiki.ros.org/ROS/Tutorials/UnderstandingTopics rostopic rqt_graph rosmsg rqt_graph 一个用于查看topi ...

  7. MEF 基础简介 四

    前言 前面讲解了MEF的引用方法,接口的导入导出,类属性的导入导出和集合的导出用法其实大家可以看到基本上大同小异的. MEF的延迟加载 我们知道当装配一个组件的时候,当前组件里面的所有的Import的 ...

  8. 安装VS2010时出现进入的图标没有与需要部分升级VS10Sp1-KB983509的解决方案

    大家好,今天又想到需要写一下博客了,毕竟感觉应该在新人入公司的时候可能需要将你电脑上的开发环境进行修改. 下面讲的主要是将VS2012卸载后,重新安装VS2010,. 我遇到了这种情况:在我将VS20 ...

  9. 消息队列_MSMQ(1)简单了解

    MSMQ (微软消息队列)   MSMQ 百度百科 MicroSoft Message Queuing(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一 ...

  10. Bootstrap 实战之响应式个人博客 (一)

    一.示例 1.主页 2.博客详情页 3.在线地址 在线地址:入口 Addition:这里使用github-page将自己的静态项目免费部署到线上. 如果你只是做一些简单的静态项目做展示,付出这么大的时 ...