26. Remove Duplicates from Sorted Array

来源:互联网 发布:初唐网络 编辑:程序博客网 时间:2024/06/07 02:17

题目: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
0 0
原创粉丝点击