Java实现LeetCode_0026_RemoveDuplicatesFromSortedArray

package javaLeetCode.primary;

public class RemoveDuplicatesFromSortedArray_26 {
	public static void main(String[] args) {
		int []nums = {1,1,2};
		System.out.println(removeDuplicates(nums));
	}//end main()
	
	
	/**
	 * 
	 * */
	/*
	 * Test Data:
	 * [1,1,2]--2
	 * [0,0,1,1,1,2,2,3,3,4]--5
	 * */
public static int removeDuplicates(int[] nums) {
       
	if(nums==null||nums.length==0) {
		return 0;
	}
	int j=0;
	int i=1;
	while(i<nums.length)
	{
		if(nums[j]!=nums[i])
			nums[++j]=nums[i++];
		else
			i++;
	}//end while
	return j+1;
    }//end removeDuplicates()

}//end RemoveDuplicatesFromSortedArray_26


posted @ 2019-07-30 21:09  南墙1  阅读(40)  评论(0编辑  收藏  举报