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. 如何将BSP应用配置成Fiori Launchpad上的一个tile

    当我们通过WebIDE或者Eclipse的插件Team Provider把一个本地开发好的UI5应用部署到了ABAP Netweaver服务器上之后,我们可以将该UI5应用配置成Fiori launc ...

  2. STL - rope 【强大的字符串处理容器】

    包含头文件: #include<ext/rope> using namespace __gnu_cxx; 申请: rope text; 基本操作: test.push_back(x); / ...

  3. Spring管理连接池的几种方式

    第一种方式:.Spring常规的数据库连接方法: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations=&qu ...

  4. hibermate一对一关联

    在hibernate.cfg.xml配置<mapping class="oneToOne.IDCard" />,以及实体类的get和set方法省略了. User类 @E ...

  5. Spring Boot 配置文件详解:Properties和YAML

    一.配置文件的生效顺序,会对值进行覆盖: 1. @TestPropertySource 注解 2. 命令行参数 3. Java系统属性(System.getProperties()) 4. 操作系统环 ...

  6. linux 中$ 意思

    grep -n sh$ text.txt   查找文件内容中以 Sh 结尾. grep -n ^a text.txt    文件文件内容中以 a 开头. grep -n ^$ text.txt     ...

  7. iOS Xcode 小技巧,提升理解查询能力,Command + 点击鼠标右键 Jump to Definition等

    前言: 介绍下Xcode 小技巧,以及一下快捷键,让你调试程序更加出类拔萃,安排! Command + 点击鼠标右键 Jump to Definition,可能你平时也在用,但是你明白全部的用法吗,试 ...

  8. iOS | AFNetworking封装

    为大家分享一个IOS处理网络请求,网络上传,网络下载等功能全面的一个第三方框架-AFNetworking,这是一个使用非常方便的网络框架. 最新的版本是基于NSURLSession,原来的NSURLC ...

  9. NSDictionary+JSON - iOS

    日常开发中常用的一个相互转换的方法; 直接创建对应的类,引用如下方法即可实现; 具体 code 如下: 声明: #import <Foundation/Foundation.h> @int ...

  10. vue+nodejs+express+mysql 建立一个在线网盘程序

    vue+nodejs+express+mysql 建立一个在线网盘程序 目录 vue+nodejs+express+mysql 建立一个在线网盘程序 第一章 开发环境准备 1.1 开发所用工具简介 1 ...