leetcode-18-remove
283. Move Zeroes

解题思路:
从nums[0]开始,如果是零就和它后面的第一个非零数交换,不是零就下一位。不贴代码了,比较简单。
27. Remove Element

解题思路:
这道题对结果的顺序无要求,所以显然可以想到交换。我的思路是设置两个指针i和j,j先从后往前扫一下,找到第一个不是val的位置。
i从前向后,找到第一个是val的位置,nums[i]和nums[j]交换。最后,nums[j]后面都是val,所以返回j+1即是想要的长度。需要注意
的是,交换完之后,j向前走,前面仍然可能是val,所以需要加一个循环,找到前方第一个不是val的位置作为j的新位置。
int removeElement(vector<int>& nums, int val) {
if (nums.size() == 0)
return 0;
if (nums.size() == 1) {
if (nums[0] == val)
return 0;
else
return 1;
}
int i,j;
j = nums.size() - 1;
while (nums[j] == val) {
j --;
if (j < 0)
return 0;
}
for (i = 0; i <= j; i++) {
if (nums[i] == val) {
nums[i] = nums[j];
nums[j] = val;
j --;
// nums[j] may be val. then move forward.
while (nums[j] == val)
j--;
continue;
}
}
return j+1;
}
26. Remove Duplicates from Sorted Array

解题思路:
考虑到nums是排好序的,所以先找到第一个与nums[i]不同的位置j,nums[i+1]=nums[j],然后i继续向前扫。如果nums[i]和nums[j]不相等,
那就继续向前扫咯。以1112222233为例。当然,如果有1111这种情况,在找j的时候,j会等于nums.size()这时候,终止就可以了。
int removeDuplicates(vector<int>& nums) {
if (nums.size() < 2)
return nums.size();
int i = 0;
int j = i + 1;
while(i < nums.size()-1) {
if (nums[j] == nums[i]) {
while (nums[j] == nums[i]) {
j++;
if (j == nums.size())
return i+1;
}
nums[i+1] = nums[j];
i++;
continue;
}
else {
i++;
j++;
}
}
return i+1;
}
203. Remove Linked List Elements

解题思路:
这道题WA好几次。。没考虑到连续几个数都是val的情况。我的思路是先处理头部是val的情况,然后处理val在second
的位置的情况。不过写的时候一定要考虑指针为空的情况,特别是用到->next赋值的时候。
ListNode* removeElements(ListNode* head, int val) {
if (head == NULL)
return head;
// if val is at head
while (head != NULL && head->val == val) {
head = head->next;
}
if (head == NULL)
return head;
ListNode* first = head;
ListNode* second = head->next;
while (second != NULL) {
if (second->val == val) {
// judge if second is NULL. serial val
while(second != NULL && second->val == val) {
second = second->next;
}
if (second == NULL) {
first->next = NULL;
}
else {
first->next = second;
first = second;
if (first != NULL)
second = first->next;
else
second = NULL;
}
continue;
}
// not val
first = second;
second = second->next;
}
return head;
}
leetcode-18-remove的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
- Leetcode练习题Remove Element
Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 660. Remove 9 移除9
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)
https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...
随机推荐
- net Core 入门实战
Asp.net Core 入门实战 Asp.Net Core 是开源,跨平台,模块化,快速而简单的Web框架. Asp.net Core官网的一个源码合集,方便一次性Clone 目录 快速入门 安 ...
- ASP.NET Core性能测试
ASP.NET Core性能测试 应用性能直接影响到托管服务的成本,因此公司在开发应用时需要格外注意应用所使用的Web框架,初创公司尤其如此.此外,糟糕的应用性能也会影响到用户体验,甚至会因此受到相关 ...
- 关系型数据库---MySQL---索引
1.概述 参考资料:http://blog.codinglabs.org/articles/theory-of-mysql-index.html 1.1 索引的本质: MySQL官方对索引的定义:索引 ...
- nginx配置SSL证书实现https服务
在前面一篇文章中,使用openssl生成了免费证书 后,我们现在使用该证书来实现我们本地node服务的https服务需求.假如我现在node基本架构如下: |----项目 | |--- static ...
- 举例实用详解sc.textFile()和wholeTextFiles()
谈清楚区别,说明白道理,从案例开始: 1 数据准备 用hdfs存放数据,且结合的hue上传准备的数据,我的hue截图: 每个文件下的数据: 以上是3个文件的数据,每一行用英文下的空格隔开: 2 测试 ...
- mysql in和exists性能比较和使用
in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的两个表大小相当,那么用i ...
- Oracle中文乱码,字符集问题处理
1. 右键计算机,选择属性,增加环境变量 NLS_LANG:SIMPLIFIED CHINESE_CHINA.ZHS16GBK 2.进入注册表,依次单击HKEY_LOCAL_MACHINE --> ...
- WPF (VisualChildren)可视化子元素详解
VisualChildrenCount 的 FrameworkElement 实现始终返回 0 或 1. 如果类所要维护的可视化子元素集合的成员数可能超过 1,则这样的类必须重写此属性和 Ge ...
- Java中的Serializable接口和transient关键字
Java中的Serializable接口和transient关键字 Table of Contents 1. 向memcached中放数据时遇到NotSerializableException异常 2 ...
- springBoot jpa 分页
1.jap中有自带的分页方法 在dao层中使用 Page<LinkUrl> findAll(Pageable pageable); 2.在controller层 public List&l ...