Leetcode Remove duplicates from Sorted Array II-----java

Source: Internet
Author: User

Follow up for "Remove duplicates":
What if duplicates is allowed at the most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3] ,

Your function should return length =, with the first 5 five elements of nums being 1 , 1 2 2 ,, and 3 . It doesn ' t matter what are you leave beyond the new length.

is the 26th version of the extension, 26 is to give a sorted array, and then delete all the duplicate numbers, so that all the numbers appear only 1 times, and then return the length.

This problem refers to allowing a number to appear up to 2 times. Then seek the length len2. And the number of LEN2 before the returned array should be the array after the collation.

 Public classSolution { Public intRemoveDuplicates (int[] nums) {        intLen =nums.length; if(Len < 2 )            returnLen; intFlag = Nums[0]; intTimes = 1; intres =Len;  for(inti = 1, j = 1;i<len;i++){            if(Flag = =Nums[i]) {                if(Times = = 1) Times++; Else{res--; Continue; }            }Else{flag=Nums[i]; Times= 1; } Nums[j]=Nums[i]; J++; }                returnRes; }}

Leetcode Remove duplicates from Sorted Array II-----java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.