Leetcode 26 Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only onceand 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].
1)使用set進行一重篩選後將陣列中元素進行修改
class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int count = 0;
Set<Integer> set = new HashSet<>();
for(int i = 0 ; i < nums.length ; i++){
if(!set.contains(nums[i])){
nums[count++] = nums[i];
set.add(nums[i]);
}
}
return count;
}
}
2)使用雙指標進行標記,比較奇妙(100%)
public class Solution {
public int removeDuplicates(int[] nums) {
int i = 0;
for(int j = 1; j < nums.length;j++){
if(nums[j] != nums[i]){
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
相關文章
- 26. Remove Duplicates from Sorted ArrayREM
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ArrayLeetCodeREM
- Remove Duplicates from Sorted Array leetcode javaREMLeetCodeJava
- Remove-duplicates-from-sorted-arrayREM
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- Leetcode-Remove Duplicates from Sorted Array IILeetCodeREM
- Remove Duplicates from Sorted Array II leetcode javaREMLeetCodeJava
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted ListREM
- Leetcode Remove Duplicates from Sorted List IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted List leetcode javaREMLeetCodeJava
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- 【Leetcode】83. Remove Duplicates from Sorted ListLeetCodeREM
- Leetcode-Remove Duplicates from Sorted List IILeetCodeREM
- Remove Duplicates from Sorted List II leetcode javaREMLeetCodeJava
- Remove-duplicates-from-sorted-listREM
- [LeetCode] Remove Duplicates from Sorted Array 有序陣列中去除重複項LeetCodeREM陣列
- 【Leetcode】82. Remove Duplicates from Sorted List IILeetCodeREM
- 83. Remove Duplicates from Sorted ListREM
- 82. Remove Duplicates from Sorted List IIREM
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted List 去除連結串列中重複值節點REM
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode Merge Sorted ArrayLeetCode
- Leetcode 442. Find All Duplicates in an ArrayLeetCode
- Leetcode Remove Duplicates型別題目 (python)LeetCodeREM型別Python
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- Leetcode Search in Rotated Sorted Array IILeetCode
- Leetcode-Merge Sorted ArrayLeetCode
- Leetcode-Search in Rotated Sorted ArrayLeetCode
- Search in Rotated Sorted Array leetcode javaLeetCodeJava