Descriptions:

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 nums = [1,1,2],

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

我写的一直有问题...用了HashSet集合,没有研究过这个类型,[1,1,2]输出结果一直是[1,1]

(问题一发现,在于,题目要求的是改变nums[]的内容,而不是输出一个新的数组)

(在小本本上记下,要研究HashSet)

import java.util.HashSet;

import java.util.Set;

public class Solution {

    public static int removeDuplicates(int[] nums) {

        Set<Integer> tempSet = new HashSet<>();

        for(int i = 0; i < nums.length; i++) {

            Integer wrap = Integer.valueOf(nums[i]);

            tempSet.add(wrap);

        }

        return tempSet.size();

    }

}

下面是优秀答案

Solutions:

public class Solution {

    public static int removeDuplicates(int[] nums) {

        int j = 0;

        for(int i = 0; i < nums.length; i++) {

            if(nums[i] != nums[j]) {

                nums[++j] = nums[i];

            }

        }

        return ++j;

    }

}

有两个点需要注意:

  1. 因为重复的可能有多个,所以不能以相等来做判定条件
  2. 注意j++++j的区别,此处用法很巧妙,也很必要!

LeetCode & Q26-Remove Duplicates from Sorted Array-Easy的更多相关文章

  1. Leetcode 26. Remove Duplicates from Sorted Array (easy)

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

  2. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  3. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  4. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  5. [array] leetCode-26. Remove Duplicates from Sorted Array - Easy

    26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

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

  7. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  8. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  9. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  10. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

随机推荐

  1. caffe简单介绍

    从四个层次来理解caffe:Blob.Layer.Net.Solver. 1.BlobBlob是caffe基本的数据结构,用四维矩阵 Batch×Channel×Height×Weight表示,存储了 ...

  2. Delphi子窗体随主窗体大小而变化

    当然办法有很多种,我建议用TRzsplitter更好点, TRzsplitter分割,在其上边放置panel,然后把align置为alClient,则可以随着主窗体的大小而一起变动 选中此控件右键ed ...

  3. 关于jstl的使用

    1.jsp中el表达式无法被解析 使用jstl标签的时候,发现el表达式无法被解析,后来查阅资料发现jsp中需要添加<%@page isELIgnored="false" % ...

  4. JavaScript的基本操作(一)

    JavaScript中有大量的方法可供我们使用,详情可参考:http://jquery.cuishifeng.cn/这也同时导致我们不可能去记住每一个的用法,且开发者每天都在新添更多的方法,所以要想掌 ...

  5. java微信公众号开发token验证失败的问题及解决办法

    本文引自http://m.blog.csdn.net/qq_32331997/article/details/72885424 微信公众平台服务器配置时,需要引入token,但是提交的时候总是提示to ...

  6. Python多线程爬虫与多种数据存储方式实现(Python爬虫实战2)

    1. 多进程爬虫 对于数据量较大的爬虫,对数据的处理要求较高时,可以采用python多进程或多线程的机制完成,多进程是指分配多个CPU处理程序,同一时刻只有一个CPU在工作,多线程是指进程内部有多个类 ...

  7. 本地mysql无法连接原来是这里有问题啊。。。。。。

    1.怎么解决localhost无法链接本地mysql数据库问题_百度经验http://jingyan.baidu.com/article/d45ad14896d1cd69542b805c.html 2 ...

  8. jdk7u79linuxx64.tar.gz下载

    jdk1.7下载: 百度云盘链接:https://pan.baidu.com/s/1cQFLnS 密码:wdek

  9. 抓取Android应用的log

    今天测试软件时,遇到一个bug,因为开发说那边不复现,所以为了更好追踪这个问题,需要抓取复现步骤地log. 在网上查了相关资料,同时结合自己遇到的问题,总结如下. 1. 抓取Android 应用log ...

  10. comfirm 方法显示对话框

    comfirm 方法显示对话框 原理: confirm() 方法用于显示一个带有指定消息和 OK 及取消按钮的对话框 confirm(message): message:要在 window 上弹出的对 ...