【LeetCode從零單排】No26.Remove Duplicates from Sorted Array
題目
題目要求:去除sort int陣列中的重複項。
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]
.
程式碼
public class Solution {
public int removeDuplicates(int[] A) {
if (A.length==0) return 0;
int length =A.length;
int[] B=new int[length];
int index=0;//B指向下一個位數
int temp=0;//B已經賦值的位置
for(int i=0;i<length;i++){
//如果第一項是零的情況{0,0,2}
if(A[i]==0 && index==0){
B[index]=0;
index+=1;
}
if(A[i]!=B[temp]){
B[index]=A[i];
temp=index;
index+=1;
}
}
//給A賦值
for(int k=0;k<index;k++){
A[k]=B[k];
}
return index;
}
}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode 26 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
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- Leetcode-Remove Duplicates from Sorted Array IILeetCodeREM
- Remove Duplicates from Sorted Array II leetcode javaREMLeetCodeJava
- Remove-duplicates-from-sorted-arrayREM
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- 【LeetCode從零單排】No88.Merge Sorted ArrayLeetCode
- 26. Remove Duplicates from Sorted ArrayREM
- Leetcode Remove Duplicates from Sorted ListLeetCodeREM
- Leetcode Remove Duplicates from Sorted List IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted List leetcode javaREMLeetCodeJava
- [LeetCode] 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】82. Remove Duplicates from Sorted List IILeetCodeREM
- Remove-duplicates-from-sorted-listREM
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- 【LeetCode從零單排】No189 .Rotate ArrayLeetCode
- 83. Remove Duplicates from Sorted ListREM
- 82. Remove Duplicates from Sorted List IIREM
- 【LeetCode從零單排】No38.CountAndSayLeetCode
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode Merge Sorted ArrayLeetCode
- 【LeetCode從零單排】No.7 Reverse IntegerLeetCode
- 【LeetCode從零單排】No20.ValidParenthesesLeetCode
- 【LeetCode從零單排】No19.RemoveNthNodeFromEndofListLeetCodeREM
- 【LeetCode從零單排】No21.MergeTwoSortedListsLeetCode
- 【LeetCode從零單排】No27.Remove ElementLeetCodeREM
- 【LeetCode從零單排】No28 Implement strStr()LeetCode