/**
     * 无额外空间,只要前n个是不重复的就行,不需要修改后面的数字
     * @param nums 已排序的数组
     * @return 去除重复数字后的长度
     */
    public int removeDuplicates(int[] nums) {
        if(nums == null || nums.length == 0) {
            return 0;
        } else if(nums.length == 1) {
            return 1;
        }

        // 使用两个指针
        int j = 0;
        for(int i = 1; i < nums.length; i++) {
            if(nums[j] != nums[i]) {
                j++;
                nums[j] = nums[i];
            }
        }
        return ++j;
    }

Array - RemoveDuplicatesfromSortedArray的更多相关文章

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

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

  2. No.026:Remove Duplicates from Sorted Array

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

  3. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  4. leetcode — remove-duplicates-from-sorted-array

    import java.util.Arrays; /** * Source : https://oj.leetcode.com/problems/remove-duplicates-from-sort ...

  5. Integer Array Ladder questions

    1.这个题不难,关键在于把题目意思理解好了.这个题问的不清楚.要求return new length,很容易晕掉.其实就是return 有多少个单独的数. import java.util.Array ...

  6. LeetCode--1、26、27、35、53 Array(Easy)

      1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to ...

  7. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  8. 【Remove Duplicates from Sorted Array】cpp

    题目: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove ...

  9. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

随机推荐

  1. UVaLive 3266 Tian Ji -- The Horse Racing (贪心)

    题意:田忌赛马,每胜一局就得200,负一局少200,问最多得多少钱. 析:贪心,如果最快的马比齐王的还快,就干掉它,如果最慢的马比齐王的马快,就干掉它,否则用最慢的马去和齐王最快的马比. 代码如下: ...

  2. C++ 定位构造

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://yiluohuanghun.blog.51cto.com/3407300/1258 ...

  3. 使用LinearLayout实现ListView,解决ListView和ScrollView滚动冲突

    在项目中,我们常常会遇到一个ScrollView里面会嵌套ListView的情况,但往往你会发现,ListView和ScrollView的滚动时间会有冲突问题,造成ListView不能完全显示.虽然网 ...

  4. solidity学习笔记

    一 pragam solidity ^0.4.23; contract helloword{ string public  name ="hello"; function getN ...

  5. 洛谷P2680 运输计划(树上差分+二分)

    传送门 考虑树上乱搞 首先这是满足二分性质的,如果在某个时间可以完成工作那么比他更长的时间肯定也能完成工作 然后考虑二分,设当前答案为$mid$,如果有一条链的长度大于$mid$,那么这条链上必须得删 ...

  6. 动手写一个简单版的谷歌TPU

    谷歌TPU是一个设计良好的矩阵计算加速单元,可以很好的加速神经网络的计算.本系列文章将利用公开的TPU V1(后简称TPU)相关资料,对其进行一定的简化.推测和修改,来实际编写一个简单版本的谷歌TPU ...

  7. Mysql相关函数使用和总结(liet、right、substring、substring_index)

    一.字段截取 1.从左开始截取字符串 用法:left(str,length),即:leift(被截取字符串,截取长度) 列子:select left(‘www.baidu.com’,8) 结果:www ...

  8. centos6上安装CDH5.7

    目录 一.环境说明及所需软件包... 1 1.环境说明... 1 2.所需软件及说明... 1 二.安装前的主机准备(所有主机都有做) 2 1.配置主机名和修改host文件... 2 2.下载系统基本 ...

  9. Count the string (KMP+DP)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...

  10. JS高级学习历程-13

    [正则表达式] \d \d\d [a-z] 什么是正则表达式:用于记录文本规则的代码 正则表达式的作用: ①       可以匹配文本,表单验证(手机号码.邮箱.qq号码) ②       赛选网页内 ...