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. Java EE学习笔记(九)

    MyBatis的关联映射 1.关联关系概述 1).实际的开发中,对数据库的操作常常会涉及到多张表,这在面向对象中就涉及到了对象与对象之间的关联关系.针对多表之间的操作,MyBatis提供了关联映射,通 ...

  2. eclipse查看jar包源文件

    话不多说上链接 https://www.cnblogs.com/1995hxt/p/5252098.html这里介绍了完整的流程,亲自试过,可以的! 以防以后要用的时候找不到文件的下载地址,所以就先在 ...

  3. 现阶段github上的emysql编译无法通过的问题

    最近在写db引擎,今天用到了emysql,找到https://github.com/Eonblast/Emysql,拽下来,然后发现竟然编译不通过~~去网上找了下资料,在 http://erlang. ...

  4. jQuery知识点小结

    博主之前学习一段时间后做了点Demo,借此机会发出来分享,其实学jQuery只要简单看看文档即可,但有些细枝末节的东西文档会默认使用者是了解的,所以还是得系统学习系统训练:Talk is cheap, ...

  5. I/O操做总结(四))

    前面已经把java io的主要操作讲完了 这一节我们来说说关于java io的其他内容 Serializable序列化 实例1:对象的序列化 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  6. Spring的七种事务传播机制

    概述 当我们调用一个基于Spring的Service接口方法(如UserService#addUser())时,它将运行于Spring管理的事务环境中,Service接口方法可能会在内部调用其它的Se ...

  7. Python + selenium之unitest(1)

    单元测试负责对最小的软件设计单元(模块)进行验证,它使用软件设计文档中对模块的描述作为指南,对重要的程序进行测试以发现模块中的错误. 下面演示不用测试框架的单元测试: # 计算器类 class Cou ...

  8. (转载)资源字典(Pro WPF 学习)

    原地址:http://www.cnblogs.com/yxhq/archive/2012/07/09/2582508.html 1.创建资源字典 下面是一个资源字典(AppBrushes.xaml), ...

  9. 【TensorFlow入门完全指南】模型篇·线性回归模型

    首先呢,进行import,对于日常写代码来说,第二行经常写成:import numpy as np,这样会更加简洁.第三行import用于绘图. 定义了学习率.迭代数epoch,以及展示的学习步骤,三 ...

  10. java面试题(杨晓峰)---第五讲String、StringBuffer、StringBuilder有什么区别?

    线程 字符 操作频繁度 1 String (1)String的创建机制 由于String在java世界中使用过于频繁,java为了避免在一个系统中产生大量重复的String对象,引入了字符串常量池,其 ...