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. C# 委托Delegate的使用 笔记

    使用delegate总是一头雾水,记录一下笔记,备忘. 主要用于线程间操作UI上的控件,以便使用.或者是大家统一操作入口使用. using System.Windows.Forms; namespac ...

  2. Unity中List的随机排序(乱序)

    为什么要给List排序做一个Unity限定条件呢 首先,是C#中的List泛型,若是Java,直接调用Collection.shuffle()就OK了 而Unity的C#版本较低,不能使用Random ...

  3. Elasticsearch就这么简单

    一.前言 最近有点想弄一个站内搜索的功能,之前学过了Lucene,后来又听过Solr这个名词.接着在了解全文搜索的时候就发现了Elasticsearch这个,他也是以Lucene为基础的. 我去搜了几 ...

  4. BAT脚本/Dos 改ip地址

    BAT脚本/Dos 改ip 经常换地方上网,总改这些很麻烦,直接写三个bat,点一下就换了.需要管理员权限.之前用python的wmi写过,但是没起作用. ip:10.10.41.15 子网掩码:25 ...

  5. angularJS 指令解释

    本文引自 http://blog.csdn.net/kongjiea/article/details/49840035 指令,很重要 AngularJS与jQuery最大的区别在哪里?我认为,表现在数 ...

  6. TCP和UDP协议的区别

    TCP和UDP都是传输层的协议 UDP协议的特点: UDP协议是一种无连接的.不可靠的传输层协议(尽力而为的协议) 为什么说UDP是一种无连接.不可靠的协议呢?UDP协议在传输报文之前不需要在双方之间 ...

  7. Vue中axios的使用技巧配置项详解

    使用axios首先要下载axios模块包 npm install axios --save 其次需要在使用的文件中引入 import axios from 'axios' 一.调用axios常见两种方 ...

  8. ES6中export及export default的区别

    相信很多人都使用过export.export default.import,然而它们到底有什么区别呢? 在JavaScript ES6中,export与export default均可用于导出常量.函 ...

  9. 通过jersey-client客户端调用Jersey的Web服务模拟CURD

    一.总体说明 通过jersey-client接口,创建客户端程序,来调用Jersey实现的RESTful服务,实现增.删.改.查等操作. 服务端主要是通过内存的方式,来模拟用户的增加.删除.修改.查询 ...

  10. [bzoj3173]最长上升子序列_非旋转Treap

    最长上升子序列 bzoj-3173 题目大意:有1-n,n个数,第i次操作是将i加入到原有序列中制定的位置,后查询当前序列中最长上升子序列长度. 注释:1<=n<=10,000,开始序列为 ...