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的更多相关文章

  1. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  2. 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++> 给出排序好的 ...

  3. [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% ...

  4. [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 ...

  5. Leetcode练习题Remove Element

    Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  10. leetcode@ [316] Remove Duplicate Letters (Stack & Greedy)

    https://leetcode.com/problems/remove-duplicate-letters/ Given a string which contains only lowercase ...

随机推荐

  1. Ubuntu新服务器安装lnmp

    版本: nginx(无要求,最新) mysql(5.6.xx) php(5.6.xx) ubuntu(16.04,其他版本也并无过多差异) 准备: #apt-get update #apt-get i ...

  2. 066 Plus One

    给定一个非负整数组成的非空数组,给整数加一.可以假设整数不包含任何前导零,除了数字0本身.最高位数字存放在列表的首位.详见:https://leetcode.com/problems/plus-one ...

  3. python 遇到的一些问题和解决方法

    安装crypto  python3里面这个改成了pycryptodome 1. pip3 install pycryptodome 或者 pip3 install -i https://pypi.do ...

  4. 浮点数据与IEE754

    在计算机系统(包括单片机)中,浮点数(单精度float和双精度的double)对采用IEE-754标准.该标准为 32 位浮点和 64 位双精度浮点二进制小数定义了二进制标准. IEEE 754 用科 ...

  5. Java热启动

    1.在项目中的pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> <art ...

  6. 采用React+Ant Design组件化开发前端界面(一)

    react-start 基础知识 1.使用脚手架创建项目并启动 ​ 1.1 安装脚手架: npm install -g create-react-app ​ 1.2 使用脚手架创建项目: create ...

  7. CA、公钥、私钥 概要

    CA.公钥.私钥 概要 现在在开发中遇到一个需求,需要使用tls加密技术,之前并没有了解过,这里来做一个关于CA,公钥,密钥的总结,至于怎么生成这儿就不讲了,如果有机会可以再开一个单章来讲一下. 现在 ...

  8. Flash图表FusionCharts如何自定义图表导出菜单或界面

    FusionCharts的导出组件界面有两种模式: Compact Mode: 用于保存单张图片,每一个单独的导出组件实例都代表单独的图表.在这种模式下,只有一个按钮和标题是可见的. Full Mod ...

  9. MATLAB中容易忽略却经常遇到的小技巧总结

    1       如何产生一个列向量相同的矩阵 例如,列向量x=[1;2;3],要产生矩阵A=[x,x,x],即[1,1,1;2,2,2;3,3,3]. A = repmat(x,1,n) 2      ...

  10. rhythmbox插件开发笔记3:安装 makefile && schema && po

    本篇主要讲通过makefile方式来安装rhythmbox插件的相关知识. makefile 如果makefile是什么,请自行谷歌 参考了pandasunny同学的rhythmbox-baidu-m ...