1. 原题链接

https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/

2. 题目要求

给定一个已经排序的整数数组nums[ ],返回除去重复元素后的数组长度

注意:不能重新创建一个数组,空间复杂度为O(1)

3. 解题思路

使用指针j来遍历数组,i用来计数。初始时,i指向nums[0],j指向nums[1]。

当nums[i] != nums[j]时,i++,且nums[i]=nums[j],从j所在元素位置继续比较。

最后返回 i+1

4. 代码实现

public class RemoveDuplicatesFromSortedArray26 {
public static void main(String[] args) {
int nums[] = {2, 2, 3, 4, 5, 5, 6};
System.out.println(removeDuplicates(nums));
} public static 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++
i++;
nums[i] = nums[j];
}
} return i + 1;
}
}

 运行结果:

LeetCode:26. Remove Duplicates from Sorted Array(Easy)的更多相关文章

  1. Leetcode No.26 Remove Duplicates from Sorted Array(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums sorted in non-decreasing order, remove the duplicates in- ...

  2. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

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

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

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

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

  5. LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)

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

  6. 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array

    一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...

  7. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  8. 【LeetCode】26. Remove Duplicates from Sorted Array

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

  9. LeetCode OJ 26. Remove Duplicates from Sorted Array

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

随机推荐

  1. 【转载】#470 Define Your Own Custom Attribute

    You can use predefined attributes to attach metadata to type members. You can also define a custom a ...

  2. 大整数乘法(POJ2389)

    题目链接:http://poj.org/problem?id=2389 #include <stdio.h> #include <string.h> #define Max 1 ...

  3. 【[SCOI2010]生成字符串】

    \(n=m\)时候经典的卡特兰 那\(n!=m\)呢,还是按照卡特兰的方式来推 首先总情况数就是\(\binom{n+m}{n}\),在\(n+m\)个里选择\(n\)个\(1\) 显然有不合法的情况 ...

  4. 【字符串】跳来跳去的KMP匹配

    原理: 不给予证明啦(懒得一批 但是代码中有给还算详细的注释 参考:https://www.cnblogs.com/yjiyjige/p/3263858.html 模板题: 洛谷P3375: http ...

  5. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f4-hystrix/ 本文出自方志朋的博客 在微服务架构中, ...

  6. linux保留旧版本python,安装python3

    1.备份老版本 mv /usr/bin/python /usr/bin/python.bak 2. 下载python3 wget https://www.python.org/ftp/python/3 ...

  7. Python基础—02-数据类型

    数据类型 存储单位 最小单位是bit,表示二进制的0或1,一般写作b 最小的存储单位是字节,用byte表示,1B = 8b 1024B = 1KB 1024KB = 1MB 1024MB = 1GB ...

  8. python之yield表达式

    yield表达式用于generator function 调用generator function时,返回一个iterator(函数内语句不被会执行),调用iterator函数时,执行到yield表达 ...

  9. lvs集群实现lvs-dr模型和lvs-nat模型

    ipvsadm ipvsadm命令是lvs集群在应用层的管理工具,我们可以通过此ipvsadm来管理lvs的配置,其实现了集群服务管理:增.删.改,集群服务的RS管理:增.删.改以及查看集群状态. 管 ...

  10. Linux下文件字符编码格式检测和转换

    目前多数情况下, 我们遇到的非英文字符文件都是使用UTF-8编码的, 这时一般我们查看这些文件的内容都不会有问题. 不过有时, 我们有可能会遇到非UTF-8编码的文件, 比如中文的GBK编码, 或者俄 ...