FTPrep, 26 Remove Duplicates from Sorted Array
来源:互联网 发布:sql2000还原数据库 编辑:程序博客网 时间:2024/05/22 08:28
easy的题,给点时间总能搞出来,速度的话就看熟悉程度了。
这题和下一道,remove target val异曲同工。有一定的迷惑性,不易直接写出来就是因为有remove的字眼。
真正对的思路其实应该是 看keep哪些元素,选择keep下来的元素是什么判断条件,这样的话思路才顺。一个scan指针,一个keep指针。
public class Solution { public int removeDuplicates(int[] nums) { int len=nums.length; if(len==0) return len; int keep=0; for(int scan=0; scan<len; ++scan){ if(nums[keep]!=nums[scan]) nums[++keep]=nums[scan]; } return keep+1; }}
把下一题的代码也贴在这,可以对比,其实思路是一样的,代码结构都一样,细微差别在于,keep的条件。
public class Solution { public int removeElement(int[] nums, int val) { int len=nums.length; if(len==0) return 0; int keep=0; for(int scan=0; scan<len; ++scan){ if(nums[scan]!=val) nums[keep++] = nums[scan]; } return keep; }}
阅读全文
0 0
- FTPrep, 26 Remove Duplicates from Sorted Array
- FTPrep, 80 Remove Duplicates from Sorted Array II
- FTPrep, 83 Remove Duplicates from Sorted List
- LeetCode(26)Remove Duplicates From Sorted Array
- [leetcode 26] Remove Duplicates from Sorted Array
- 26、Remove Duplicates from Sorted Array
- [Leetcode] 26 - Remove Duplicates from Sorted Array
- leetcode|26|Remove Duplicates from Sorted Array
- leetcode-26 Remove Duplicates from Sorted Array
- leetcode 26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- LeetCode 26 Remove Duplicates from Sorted Array
- leetcode 26 Remove Duplicates from Sorted Array
- #26 Remove Duplicates from Sorted Array
- LeetCode---(26) Remove Duplicates from Sorted Array
- Leetcode 26 Remove Duplicates from Sorted Array
- LeetCode 26 - Remove Duplicates from Sorted Array
- Baidu Map 自动定位
- js -- 设置、获取、删除cookie
- Android6.0 Log的工作机制
- 判断应用是否在后台
- ios json解析
- FTPrep, 26 Remove Duplicates from Sorted Array
- java抽取PDF指定范围页做新的PDF文件
- leetcode 78. Subsets
- zookeeper学习
- android 异步图片加载组件ImageLoader封装使用
- 步进电机有没有零位?步进电机转矩归零的方法
- CocoaPods安装、使用及常见问题解决
- POJ 2104 K-th Number 划分树
- Webpack2 完整踩坑教程(一)