[LeetCode] 10.Remove Duplicates from Sorted ArrayⅡ

2021. 1. 4. 12:10

leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

 

Remove Duplicates from Sorted Array II - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

1. Problem

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.

Do not allocate extra space for another array; you must do this by modifying the input array in-place with O(1) extra memory.

 

Clarification:

Confused why the returned value is an integer, but your answer is an array?

Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) {     print(nums[i]); }

 

Example 1:

Input: nums = [1,1,1,2,2,3]

Output: 5, nums = [1,1,2,2,3]

Explanation:

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

 

Example 2:

Input: nums = [0,0,1,1,1,1,2,3,3]

Output: 7, nums = [0,0,1,1,2,3,3]

Explanation:

Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively. It doesn't matter what values are set beyond the returned length.

 

Constraints:

  • 1 <= nums.length <= 3 * 104
  • -104 <= nums[i] <= 104
  • nums is sorted in ascending order.

2. Code

package leetCode;

import java.util.ArrayList;

public class RemoveDuplicatesFromSortedArray2 {
	final static int[] nums = {1,1,1,2,2,3};
	public static void main(String[] args) {
		removeDuplicates(nums);
	}
	
    public static int removeDuplicates(int[] nums) {
        boolean overlap = false;
        ArrayList<Integer> numsList = new ArrayList<Integer>();     
		for(int i = 0; i < nums.length; i++) {
        	if(i == 0) { 
        		numsList.add(nums[i]);
        	} else { // i는 1부터 nums크기-1까지
        		if(nums[i] == nums[i-1]) { //중복값일때
        			if(!overlap) { //중복값이 한번도 안들어갔으면
        				numsList.add(nums[i]);
        				overlap = true; // 다음 중복값은 추가X
        			}
        		} else { //중복값이 아닐때
        			overlap = false;
        			numsList.add(nums[i]);
        		}
        	}
        }
		
		for(int i = 0; i < numsList.size(); i++) {
			nums[i] = numsList.get(i);
		}
		return numsList.size();
    }
}

 

3. Report

지난 포스팅인 Remove Duplicates from Sorted Array 의 심화버전이다.

 

[LeetCode] 9.Remove Duplicates from Sorted Array

leetcode.com/problems/remove-duplicates-from-sorted-array/ Remove Duplicates from Sorted Array - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your kn..

bbo-blog.tistory.com

다른 점은

boolean overlap을 이용하여 중복값을 한번 넣었는지 확인한다.

'Algorithm > 문제' 카테고리의 다른 글

[LeetCode] 12.Length of Last Word  (0) 2021.01.14
[LeetCode] 11.Implement strStr()  (0) 2021.01.08
[LeetCode] 9.Remove Duplicates from Sorted Array  (0) 2021.01.04
[LeetCode] 8.Remove Element  (0) 2021.01.04
[LeetCode] 7.Longest Common Prefix  (0) 2020.12.30

BELATED ARTICLES

more