Java for Leetcode 080 Remove duplicates from Sorted Array II

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.

Problem Solving Ideas:

There are many ways to do this, and here is another way to open an array, Java implementation is as follows:

    public int removeduplicates (int[] nums) {if (Nums.length <= 2) return nums.length;int[] numscopy = new Int[nums.length]; Numscopy[0] = nums[0];numscopy[1] = Nums[1];int index = 2;for (int i = 2; i < nums.length; i++) if (nums[i]! = numscopy [Index-2]) {Numscopy[index] = nums[i];index++;} for (int i = 0; i < index; i++) nums[i] = Numscopy[i];return index;    }

Java for Leetcode 080 Remove duplicates from Sorted Array II

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.