Leetcode 26) Remove Duplicates from Sorted Array
in Algorithms
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length <=1){
return nums.length;
}
int i=1;
int prev= nums[0];
int modiInd =0;
while(i<nums.length){
if(prev==nums[i]){
modiInd++;
}else{
nums[i-modiInd]=nums[i];
}
prev=nums[i];
i++;
}
return i-modiInd;
}
}
다른 답안
class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}