- Leetcode-26Remove Duplicates fro
- LeetCode 26. Remove Duplicates f
- ZXAlgorithm - Leetcode Top Inter
- Remove Duplicates from Sorted Ar
- 26 Remove Duplicates From Sorted
- 83 Remove Duplicates From Sorted
- Remove Duplicates from Sorted Ar
- Remove Duplicates from Sorted Ar
- Remove Duplicates from Sorted Ar
- Remove Duplicates from Sorted Ar
- 描述
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].
solution:
public class Solution {
public int remove(int[] arr) {
if(arr == null || arr.length == 0)
return 0;
if (arr.length == 1)
return 1;
int idx = 0;
for(int i=1; i < arr.length; ++i) {
if(arr[idx] != arr[i]) {
arr[++idx] = arr[i]; # 注意‘++’的位置
}
}
return (idx + 1);
}
}
时间复杂度O(N),空间复杂度O(1)
网友评论