|
| 1 | +<h2><a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/"><div id="big-omega-company-tags"><div id="big-omega-topbar"><div class="companyTagsContainer" style="overflow-x: scroll; flex-wrap: nowrap;"><div class="companyTagsContainer--tag" style="background-color: rgba(0, 10, 32, 0.05);"><div>Adobe</div><div class="companyTagsContainer--tagOccurence">3</div></div></div><div class="companyTagsContainer--chevron"><div><svg version="1.1" id="icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 32 32" fill="#4087F1" xml:space="preserve" style="width: 20px;"><polygon points="16,22 6,12 7.4,10.6 16,19.2 24.6,10.6 26,12 "></polygon><rect id="_x3C_Transparent_Rectangle_x3E_" class="st0" fill="none" width="32" height="32"></rect></svg></div></div></div></div>80. Remove Duplicates from Sorted Array II</a></h2><h3>Medium</h3><hr><div><p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</strong> of the elements should be kept the <strong>same</strong>.</p> |
| 2 | + |
| 3 | +<p>Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the <strong>first part</strong> of the array <code>nums</code>. More formally, if there are <code>k</code> elements after removing the duplicates, then the first <code>k</code> elements of <code>nums</code> should hold the final result. It does not matter what you leave beyond the first <code>k</code> elements.</p> |
| 4 | + |
| 5 | +<p>Return <code>k</code><em> after placing the final result in the first </em><code>k</code><em> slots of </em><code>nums</code>.</p> |
| 6 | + |
| 7 | +<p>Do <strong>not</strong> allocate extra space for another array. You must do this by <strong>modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a></strong> with O(1) extra memory.</p> |
| 8 | + |
| 9 | +<p><strong>Custom Judge:</strong></p> |
| 10 | + |
| 11 | +<p>The judge will test your solution with the following code:</p> |
| 12 | + |
| 13 | +<pre>int[] nums = [...]; // Input array |
| 14 | +int[] expectedNums = [...]; // The expected answer with correct length |
| 15 | + |
| 16 | +int k = removeDuplicates(nums); // Calls your implementation |
| 17 | + |
| 18 | +assert k == expectedNums.length; |
| 19 | +for (int i = 0; i < k; i++) { |
| 20 | + assert nums[i] == expectedNums[i]; |
| 21 | +} |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p>If all assertions pass, then your solution will be <strong>accepted</strong>.</p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1:</strong></p> |
| 28 | + |
| 29 | +<pre><strong>Input:</strong> nums = [1,1,1,2,2,3] |
| 30 | +<strong>Output:</strong> 5, nums = [1,1,2,2,3,_] |
| 31 | +<strong>Explanation:</strong> Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. |
| 32 | +It does not matter what you leave beyond the returned k (hence they are underscores). |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong class="example">Example 2:</strong></p> |
| 36 | + |
| 37 | +<pre><strong>Input:</strong> nums = [0,0,1,1,1,1,2,3,3] |
| 38 | +<strong>Output:</strong> 7, nums = [0,0,1,1,2,3,3,_,_] |
| 39 | +<strong>Explanation:</strong> Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively. |
| 40 | +It does not matter what you leave beyond the returned k (hence they are underscores). |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | +<p><strong>Constraints:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li> |
| 48 | + <li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li> |
| 49 | + <li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li> |
| 50 | +</ul> |
| 51 | +</div> |
0 commit comments