80:删除有序数组中的重复项 II
This commit is contained in:
parent
d85429aad4
commit
efb142b920
@ -0,0 +1,98 @@
|
|||||||
|
//给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度。
|
||||||
|
//
|
||||||
|
// 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 说明:
|
||||||
|
//
|
||||||
|
// 为什么返回数值是整数,但输出的答案是数组呢?
|
||||||
|
//
|
||||||
|
// 请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
|
||||||
|
//
|
||||||
|
// 你可以想象内部操作如下:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
|
||||||
|
//int len = removeDuplicates(nums);
|
||||||
|
//
|
||||||
|
//// 在函数里修改输入数组对于调用者是可见的。
|
||||||
|
//// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
|
||||||
|
//for (int i = 0; i < len; i++) {
|
||||||
|
// print(nums[i]);
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [1,1,1,2,2,3]
|
||||||
|
//输出:5, nums = [1,1,2,2,3]
|
||||||
|
//解释:函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 不需要考虑数组中超出新长度后面的元素。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:nums = [0,0,1,1,1,1,2,3,3]
|
||||||
|
//输出:7, nums = [0,0,1,1,2,3,3]
|
||||||
|
//解释:函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。 不需要考虑数组中超出新长度后面的
|
||||||
|
//元素。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= nums.length <= 3 * 104
|
||||||
|
// -104 <= nums[i] <= 104
|
||||||
|
// nums 已按升序排列
|
||||||
|
//
|
||||||
|
// Related Topics 数组 双指针
|
||||||
|
// 👍 438 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//80:删除有序数组中的重复项 II
|
||||||
|
public class RemoveDuplicatesFromSortedArrayIi {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new RemoveDuplicatesFromSortedArrayIi().new Solution();
|
||||||
|
solution.removeDuplicates(new int[]{1,1,1,2,2,3});
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int removeDuplicates(int[] nums) {
|
||||||
|
int length = nums.length;
|
||||||
|
if (length < 3) {
|
||||||
|
return nums.length;
|
||||||
|
}
|
||||||
|
int front = 0;
|
||||||
|
int count = 0;
|
||||||
|
int result = length;
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
if (i == 0) {
|
||||||
|
front = nums[i];
|
||||||
|
count = 1;
|
||||||
|
} else {
|
||||||
|
count = front == nums[i] ? count + 1 : 1;
|
||||||
|
front = front == nums[i] ? front : nums[i];
|
||||||
|
if (count == 3) {
|
||||||
|
count--;
|
||||||
|
result--;
|
||||||
|
} else {
|
||||||
|
nums[i + result - length] = nums[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
<p>给你一个有序数组 <code>nums</code> ,请你<strong><a href="http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank"> 原地</a></strong> 删除重复出现的元素,使每个元素 <strong>最多出现两次</strong> ,返回删除后数组的新长度。</p>
|
||||||
|
|
||||||
|
<p>不要使用额外的数组空间,你必须在 <strong><a href="https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95" target="_blank">原地 </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>说明:</strong></p>
|
||||||
|
|
||||||
|
<p>为什么返回数值是整数,但输出的答案是数组呢?</p>
|
||||||
|
|
||||||
|
<p>请注意,输入数组是以<strong>「引用」</strong>方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p>
|
||||||
|
|
||||||
|
<p>你可以想象内部操作如下:</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
// <strong>nums</strong> 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
|
||||||
|
int len = removeDuplicates(nums);
|
||||||
|
|
||||||
|
// 在函数里修改输入数组对于调用者是可见的。
|
||||||
|
// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
print(nums[i]);
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [1,1,1,2,2,3]
|
||||||
|
<strong>输出:</strong>5, nums = [1,1,2,2,3]
|
||||||
|
<strong>解释:</strong>函数应返回新长度 length = <strong><code>5</code></strong>, 并且原数组的前五个元素被修改为 <strong><code>1, 1, 2, 2,</code></strong> <strong>3 </strong>。 不需要考虑数组中超出新长度后面的元素。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>nums = [0,0,1,1,1,1,2,3,3]
|
||||||
|
<strong>输出:</strong>7, nums = [0,0,1,1,2,3,3]
|
||||||
|
<strong>解释:</strong>函数应返回新长度 length = <strong><code>7</code></strong>, 并且原数组的前五个元素被修改为 <strong><code>0</code></strong>, <strong>0</strong>, <strong>1</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong>, <strong>3 。</strong> 不需要考虑数组中超出新长度后面的元素。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
|
||||||
|
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
|
||||||
|
<li><code>nums</code> 已按升序排列</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>双指针</li></div></div>\n<div><li>👍 438</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user