您的位置:首页 > 其它

26. Remove Duplicates from Sorted Array

2016-06-08 11:37 239 查看
题目:https://leetcode.com/problems/remove-duplicates-from-sorted-array/

代码:

public class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length==0)
return 0;
int new_length = nums.length;
int i=0;
while(i<new_length-1)
{
if(nums[i]==nums[i+1])
{
new_length--;
remove(nums,i);
continue;
}
i++;
}
return new_length;
}
public void remove(int[] nums,int i)
{
for(int j=i;j<nums.length-1;j++)
{
nums[j] = nums[j+1];
}
}
}
91ms!!!beat 1.47%
bad code!!!!!!

=============================修改===============
public class Solution {
public int removeDuplicates(int[] nums) {
int i=0;
for(int n:nums)
{
if(i<1||n>nums[i-1])
nums[i++] = n;
}
return i;
}
}
1ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: