leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
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 删除链表中重复的结点)的更多相关文章
- LeetCode 剑指 Offer 22. 链表中倒数第k个节点
剑指 Offer 22. 链表中倒数第k个节点 题意 输入一个链表,输出该链表中倒数第k个节点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点. 例如,一个链表有 6 个 ...
- 【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 ...
- 203. Remove Linked List Elements - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
- 【LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- (LeetCode 203)Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- leetcode:Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LeetCode Remove Linked List Elements 删除链表元素
题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...
随机推荐
- 文本主题模型之非负矩阵分解(NMF)
在文本主题模型之潜在语义索引(LSI)中,我们讲到LSI主题模型使用了奇异值分解,面临着高维度计算量太大的问题.这里我们就介绍另一种基于矩阵分解的主题模型:非负矩阵分解(NMF),它同样使用了矩阵分解 ...
- 设计模式总结篇系列:策略模式(Strategy)
前面的博文中分别介绍了Java设计模式中的创建型模式和结构型模式.从本文开始,将分别介绍设计模式中的第三大类,行为型模式.首先我们了解下分为此三大类的依据. 创建型模式:主要侧重于对象的创建过程: 结 ...
- 我是这样理解HTTP和HTTPS区别的
为何要用https? http协议的缺点 通信使用明文,内容可能被窃听(重要密码泄露) 不验证通信方身份,有可能遭遇伪装(跨站点请求伪造) 无法证明报文的完整性,有可能已遭篡改(运营商劫持) 用htt ...
- 解决虚拟机连接不上外网,不能互相ping通
0.解决虚拟机连接不上外网,不能互相ping通 直接在linux系统下,编辑修改如下文件: //编辑文件/etc/sysconfig/network-scripts/ifcfg-eth0 [root@ ...
- 第21章 登录 - Identity Server 4 中文文档(v1.0.0)
为了使IdentityServer能够代表用户发出令牌,该用户必须登录IdentityServer. 21.1 Cookie身份验证 使用由ASP.NET Core中的cookie身份验证处理程序管理 ...
- mongodb学习(入门。。。。。)
db.xs.insert({name:zhangsan}) db:当前数据库 xs:学生集合(没有的话自动创建) show collections 显示当前数据库的集合名字 show dbs ...
- 《Web安全深度剖析》
书名 <Web安全深度剖析> 图片 时间 2018-11月 总结 算是我安全的启蒙书 前五章都是工具 看完差不多算个脚本小子 后面的实战感觉很空洞没什么实战
- C# 1-2+3-4+5...+m的几种方法
class Program { //第一种(1-2)+(3-4)+(5-6)...+m public static void Test(int m) { ; == ) { z = -(m / ); } ...
- Java开发笔记(五十五)关键字static的用法
前面介绍嵌套类的时候讲到了关键字static,用static修饰类,该类就变成了嵌套类.从嵌套类的用法可知,其它地方访问嵌套类之时,无需动态创建外层类的实例,直接创建嵌套类的实例就行.其实static ...
- fork/join概述
Fork/Join是java 7 解决并发问题的解决方案. 是 java内部并行框架.核心思想分别为拆分任务和结果合并,在核心思想外,为了提高cpu多核的利用率,设计了工作窃取算法,并将工作队列设计为 ...