题目难度: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,然后写了代码如下

     public int removeDuplicates(int[] nums) {
Set s = new HashSet();
for (int i = 0; i < nums.length; i++) {
s.add(nums[i]);
}
return s.size();
}

但是答案说错误??

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

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

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

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

     public int removeDuplicates(int[] nums) {
Set<Integer> s = new TreeSet<Integer>();
for (int i = 0; i < nums.length; i++) {
s.add(nums[i]);
}
int i = 0;
for (Iterator iterator = s.iterator(); iterator.hasNext();) {
nums[i++] = (Integer) iterator.next();
}
return s.size();
}

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

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

 public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}

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

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

期间编译错误:

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

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

3. 忘记写return。

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

  1. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

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

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  4. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

  5. 算法题丨Remove Duplicates from Sorted Array

    描述 Given a sorted array, remove the duplicates in-place such that each element appear only once and ...

  6. 【Leetcode】【Medium】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

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

    题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct  ...

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

    哈哈,我又来了.昨天发现题目太简单就没有放上来,今天来了一道有序链表的题.题目如下: Given a sorted linked list, delete all duplicates such th ...

  9. 【Leetcode】【Easy】Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. 巨蟒python全栈开发数据库攻略6:索引2&重要内容汇总

    1.索引的添加和删除 2.正确命中索引举例,explain优化神奇的简单使用 3.联合索引 4.简述慢日志记录 5.用户创建和权限分配 6.mysqldump逻辑备份,浅谈主从复制和读写分离 7.浅谈 ...

  2. 阿里巴巴Java开发规约IDEA插件安装及使用

    技术交流群:233513714   一.通过Jetbrains官方仓库安装 1. 打开 Settings >> Plugins >> Browse repositories.. ...

  3. shell脚本读取文件+读取命令行参数+读取标准输入+变量赋值+输出到文件

    读取url_list文件批量下载网页 url_list http://www.tianyancha.com/company/2412078287 http://www.4399.com/special ...

  4. 正向代理、Nginx(反向代理、负载均衡、静态资源服务器)

    淘宝tengine文档(本质就是淘宝版的Nginx) http://tengine.taobao.org/book/index.html

  5. php源码安装,并配置apache支持php

    一.php安装准备环境 yum install zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxml2-deve ...

  6. Visio Yoeman

    Visio需要确定文件位置才能运行的 Yo主要是用来生成框架的,相当于是一个框架生成器 new install -g generator-django 然后在Visio按F5,选择要创建的环境,就可以 ...

  7. PHP如何实现验证码

    现在来说说简单的纯数字验证码吧. 如果是初学者,建议按照我代码的注释 //数字 一步步来.最简单的方法,还是把整个代码复制走了. 新建一个captcha.php: <?php //11>设 ...

  8. 08 Spring框架 AOP (一)

    首先我们先来介绍一下AOP: AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,面向切面编程是从动态的 ...

  9. POJ 1659 Frogs' Neighborhood (Havel定理构造图)

    题意:根据图的度数列构造图 分析:该题可根据Havel定理来构造图.Havel定理对可图化的判定: 把序列排成不增序,即d1>=d2>=……>=dn,则d可简单图化当且仅当d’={d ...

  10. RPC数据通信

    RPC全称为Remote Procedure Call,翻译过来为“远程过程调用”.目前,主流的平台中都支持各种远程调用技术,以满足分布式系统架构中不同的系统之间的远程通信和相互调用.远程调用的应用场 ...