[leetcode] Remove Duplicates from Sorted Array
来源:互联网 发布:淘宝卖家一个钻 编辑:程序博客网 时间:2024/06/05 20:32
描述
Given a sorted array, remove the duplicates in place such that each element appear only once
and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2],
Given a sorted array, remove the duplicates in place such that each element appear only once
and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
基本思路: 既然要利用原来的数组,那么可以把非重复的字符向前移位 覆盖掉重复的字符,得到一个新数组,新数组的最后的位置Index+1 就是长度。
算法解释:循环访问原数组元素,设置一个Index 标示需要覆盖的字符。 遇到重复相等的字符时 index 保持不变, 继续i++, 当遇到第一个不等的字符 data[i] 则向后移动index (可以被覆盖的重复位置),然后用位置 i 的新字符data[i] 覆盖这个index 位置(第一个重复)的字符
public class RemoveDuplicates {public static int removeDuplicates(int[] data){if(data == null || data.length<1){return 0;}int index = 0;for(int i = 1; i < data.length ; i++){if(data[index] != data[i]){index++;data[index] = data[i];}}return index +1 ;}}
最后的index 标示新array的最后一个字符位置,问题是返回长度所以加1.
思考题,最后的Index之后的数组空间还剩余哪些垃圾元素?
另外一个题:如果允许重复两次如何处理?
/* * Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3] * */public static int removeDuplicates2(int[] data){if(data == null || data.length<1){return 0;}int index = 0; int dup = 0;for(int i = 1; i < data.length ; i++){if(data[index] != data[i]){index++; data[index] = data[i];dup = 0;}else {dup ++;if(dup <2){index ++;}}}return index +1 ;}
0 0
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- leetcode 35: Remove Duplicates from Sorted Array
- [LeetCode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array
- Leetcode: Remove Duplicates from Sorted Array
- [leetcode] Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array
- [LeetCode]Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array
- [leetcode]Remove Duplicates from Sorted Array II
- Linux/UNIX进程关系
- 类标签缺失 weka的StringToWordVector类解析
- Point类android.graphics.Point
- python os.path模块常用方法详解
- POJ 2761 Feed the dogs(Treap名次树+离线处理)
- [leetcode] Remove Duplicates from Sorted Array
- ios即时通讯客户端开发之-mac上搭建openfire服务器
- 使用Snoopy进行模拟登陆、采集
- 数据库优化中如何提高SQL查询效率
- Java 8:HashMap的性能提升
- 0xc0000428 winload.exe无法验证其数字签名的解决方法
- POJ 2985 The k-th LargestGroup(Treap+并查集)
- PHP curl 抓取页面时的 cookie 问题
- vector的用法