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 ...
随机推荐
- ssas 为绑定指定的大小太小,导致一个或多个列值被截断
错误信息:ssas 为绑定指定的大小太小,导致一个或多个列值被截断 如果更改了某个维度或是事实表的字段长度,在处理CUBE时提示此错误,我们要做以下更新: 1.刷新数据源视图. 2.打开多维数据集,查 ...
- nodejs 实践:express 最佳实践(五) connect解析
nodejs 实践:express 最佳实践(五) connect解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需 ...
- 读取jar包内的文件内容
package com.chanpion.boot; import org.springframework.util.ResourceUtils; import java.io.File; impor ...
- 安装vs2013提示必须安装ie10的解决办法
虽说应该直接安装ie10,但试了下并不是很顺利,找到如下解决办法,亲测通过. 新建bat文件,内容如下,右键以管理员身份运行,vs即可正常安装. @ECHO OFF :IE10HACK REG ADD ...
- 浅析HTML的元素类型及其转换
大家都知道html是由标签元素组成的,在了解元素的类型转换之前,让我们先来了解一下html的元素类型. 一.html元素类型分为两种:块级元素和内联元素,内联元素又被称为行内元素. 常见的块级元素有 ...
- Kendo MVVM 数据绑定(六) Html
Kendo MVVM 数据绑定(六) Html Html 绑定可以使用 ViewMod e 的属性来设置 DOM 元素的 innerHTML 属性.如果 ViewModel 的属性的数据类型不是字符串 ...
- Android仿360悬浮小球自定义view实现
转载请标明出处:http://www.jianshu.com/u/a5ad093cffe8 效果图如下: 图片.png 图片.png 实现当前这种类似的效果 (360小球 悬浮桌面差不错类似).第 ...
- Jquery ajax中表单提交被拦截的问题处理方法
在实际开发项目中,由于要做支付宝的批量退款处理,需要用到ajax中去提交表单数据,项目截图如下: 由于在第二张截图“确认退款”那里需要异步ajax提交数据到服务器处理信息,处理成功后将返回的数据装载到 ...
- 用配置文件方式启动mongodb集群
- 51nod 1572 宝岛地图
题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 勇敢的水手们到达了一个小岛,在这个小岛上,曾经有海盗在这里埋下了一些宝藏.然而,我 ...