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 ... 
随机推荐
- Mysql修改server uuid
			在主从复制的时候如果第二个虚拟机是复制过去的,需要修改 https://blog.csdn.net/pratise/article/details/80413198 1. 首先要查找到mysql的安装 ... 
- 浅谈C#解析网页
			最近做了一个项目,要求获取各大主流网页上的关键信息,本人以前了解过网页爬虫的知识,所以想到了网页爬虫了实现功能 第一次尝试: 采用webclient获取远程网页的内容,然后采用正则表达式进行过滤 但, ... 
- JS的使用
			Javascript代码在浏览器中运行,做出更流畅.优美的页面效果,增强用户体验与java是完全不同的东西,只是名称类似而已写在<script></script>标签中 大小写 ... 
- 基于TypeScript从零重构axios
			一.在GitHub上创建一个代码仓库 找到仓库地址:git@github.com:QianDingweiCharles/ts-axios.git 二.项目配置 本地新建一个文件夹axios 用VSco ... 
- Memcache笔记02-telnet操作memcached
			telnet操作Memcached 登录到telnet连接到memcached服务: telnet 127.0.0.1 11211 memcached的基本命令: //当telnet登录成功可以看到一 ... 
- python之元组,列表和字典的区别
			Python语言包含6种内建的序列,其中,有两种主要的类型:列表和元组. 列表是可以修改的,而元组不可以,如果要添加或者删除某些元素,就只能用列表,为了限制某些元素,就会用到元组.一般来说,列表可以替 ... 
- 单列表变量与字符串拆分的对照(SqlServer)
			最近遇到一个问题,在SQLServer中,需要根据用户传入的一系列ID值更新对应的记录.有两种方法,一种是将这些ID值使用逗号分隔,拼接成字符串传入,一种是以表变量的方式传入.最开始,我想当然的认为传 ... 
- Git项目管理常用命令
			安装Git Bash后,地址:https://git-scm.com/downloads 根据自己的操作系统选择对应是安装方式 可参见码云给出的文档:http://git.mydoc.io/?t=18 ... 
- (外挂破解)Cheat Engine(内存修改工具)V6.2中文版软件介绍
			Heat Engine是一款内存修改编辑工具,Cheat Engine允许你修改你的游戏,所以你将总是赢.它包括16进制编辑,反汇编程序,内存查找工具.与同类修改工具相比,它具有强大的反汇编功能,且自 ... 
- UVA 714 Copying Books 抄书 (二分)
			题意:把一个包含m个正整数的序列划分成k个非空的连续子序列.使得所有连续子序列的序列和Si的最大值尽量小. 二分,每次判断一下当前的值是否满足条件,然后修改区间.注意初始区间的范围,L应该为所有正整数 ... 
