LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array

题目难度:Easy

题目:

Given a sorted array, remove the duplicates in-place such that each element appear only once 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.

翻译:

给定一个排好序的数组后,删除重复的元素,这样每个元素只出现一次,并返回新的长度。

不要新建另一个数组分配额外的空间,只能通过修改原有数组,且空间复杂度为O(1)。

示例:[1,1,2]——[1,2]  2

思路:一看见消除重复,就想到了Set,然后写了代码如下

1     public int removeDuplicates(int[] nums) {
2         Set s = new HashSet();
3         for (int i = 0; i < nums.length; i++) {
4             s.add(nums[i]);
5         }
6         return s.size();
7     }

但是答案说错误??

因为此题比较特殊,它不仅仅检查最后的结果,而且检查nums最后的值是否是期望的(只截取最后返回结果的大小)

然后我在后面加了个迭代器循环赋值,结果还是不对?

1         for (Iterator iterator = s.iterator(); iterator.hasNext();) {
2             nums[i++] = (Integer) iterator.next();
3         }

顺序也要管?  好吧HashSet无序的,那就用TreeSet吧:

 1     public int removeDuplicates(int[] nums) {
 2         Set<Integer> s = new TreeSet<Integer>();
 3         for (int i = 0; i < nums.length; i++) {
 4             s.add(nums[i]);
 5         }
 6         int i = 0;
 7         for (Iterator iterator = s.iterator(); iterator.hasNext();) {
 8             nums[i++] = (Integer) iterator.next();
 9         }
10         return s.size();
11     }

161 / 161 test cases passed. Status: Accepted Runtime: 24 ms   beats 5.31%

由于此处采用Set占用了额外的空间,空间复杂度为O(N),虽然结果正确,但是不符合原题意图,下面是参考答案。

 1 public int removeDuplicates(int[] nums) {
 2     if (nums.length == 0) return 0;
 3     int i = 0;
 4     for (int j = 1; j < nums.length; j++) {
 5         if (nums[j] != nums[i]) {
 6             i++;
 7             nums[i] = nums[j];
 8         }
 9     }
10     return i + 1;
11 }

一开始有往这方面想,但是运行后发现边界问题总是处理不了,就放弃了,

此处巧妙地采用了一个循环外的int做指针对原数组进行修改,同时循环内部从第二个开始与指针所指做比较,跳过重复的元素。

期间编译错误:

1. if 后面的括号内的判断“==”写成了“=”;

2. Set的toArray方法只能利用传参toArray( new int[set.size()] ) 这样才不会有转型错误,但是这样也只能转为非基本类型(Integer而不能是nt),像转为int数组只能利用迭代器进行循环赋值;

3. 忘记写return。

原文地址:https://www.cnblogs.com/Xieyang-blog/p/8481521.html

时间: 2024-07-29 08:36:58

LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array的相关文章

【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 思路 对于链表类的题目主要考的是指针的操作,他需要对指针的指向节点有正确的操作.这道题我们可以使用从

【LeetCode每天一题】 Remove Duplicates from Sorted List II(移除有序链表中重复的节点)

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbers from the original list. Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Example 2: Input: 1->1->1->2->3 Output

[LeetCode][Java] Remove Duplicates from Sorted Array

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array 

[LeetCode][Java] Remove Duplicates from Sorted List II

题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2-&

leetcode修炼之路——83. Remove Duplicates from Sorted List

哈哈,我又来了.昨天发现题目太简单就没有放上来,今天来了一道有序链表的题.题目如下: Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 题目分析:简单来说就是去除有序链

【Leetcode】【Medium】Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.

【LeetCode从零单刷】Remove Duplicates from Sorted List

题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 解答: 很简单的模拟,因为是 sorted,所以找到不重复的下一个元素即可. /** * Definition

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次

26. Remove Duplicates from Sorted Array【leetcode】,数组,array,java,算法

26. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant mem