LeetCode 127 Remove Duplicates from Sorted Array
来源:互联网 发布:js 自定义日期选择器 编辑:程序博客网 时间:2024/06/05 19:58
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],
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].
分析:
两个指针,i顺序往后遍历,j指向有效新数组尾。
public class Solution { public int removeDuplicates(int[] A) { if(A.length < 2) return A.length; int i=1; int j=0; while(i<A.length){ if(A[i] == A[j]) i++; else{ j++; A[j] = A[i]; i++; } } A = Arrays.copyOf(A, j+1); return A.length; }}
0 0
- LeetCode 127 Remove Duplicates from Sorted Array
- [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
- 表达式求值
- 2012年数据库技术大会演讲PPT打包
- 第三章 栈和队列
- Audio Effect的名词解释
- matlab求矩阵、向量的模
- LeetCode 127 Remove Duplicates from Sorted Array
- android service中bindService是异步绑定
- 项目五 输出完数
- 在myeclipse中代码修改不起作用:当在myeclipse中修改处理方法的代码时,重新发布Tomcat,在运行时,jsp页面显示内容没变,还是按修改前的代码执行
- 2014-10深圳全球架构师峰会
- 605662545微信公众号可以做广告了
- 大数的加减乘除(转赵老四)
- Radar Installation
- ural1989(字符串hash+线段树)