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. Jerry的WebClient UI 42篇原创文章合集

    我要感谢CRM On Premise, 因为在这个产品上做开发让我得以使用WebClient UI框架.有些朋友觉得这个SAP自己发明的基于HTML+ABAP的MVC框架,和现在流行的三驾马车(Ang ...

  2. Java里面String的编码问题

    Java里面内置字符串全部是utf-16编码,详细的编码方式看这里 import java.nio.charset.Charset; import java.util.Arrays; import j ...

  3. Consider defining a bean of type 'package' in your configuration [Spring-Boot]

    https://stackoverflow.com/questions/40384056/consider-defining-a-bean-of-type-package-in-your-config ...

  4. Android学习笔记_68_ android 9patch 图片

    http://meiyitianabc.blog.163.com/blog/static/10502212720115354948909/

  5. css块元素的 display 属性 inline-block 的应用

    <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> < ...

  6. 根据GB2312编码表求汉字字节

    java中有8种基本数据类型,byte,short,int,long,float,double,boolean byte用1个字节表示,占8比特,取值范围 负2的7次方至正2的7次方减1 二进制000 ...

  7. 【题解】POJ1845 Sumdiv(乘法逆元+约数和)

    POJ1845:http://poj.org/problem?id=1845 思路: AB可以表示成多个质数的幂相乘的形式:AB=(a1n1)*(a2n2)* ...*(amnm) 根据算数基本定理可 ...

  8. LINQ 方法

    过滤操作符 Where 运算符(Linq扩展方法)根据给定条件过滤集合. 在其中扩展方法有以下两个重载.一个过载需要Func <TSource,bool>输入参数和第二个重载方法需要Fun ...

  9. swiper插件使用技巧

    1.加载插件: <!DOCTYPE html> <html> <head> ... <link rel="stylesheet" href ...

  10. 分组函数group by和Oracle中分析函数partition by的用法以及区别

    1.分组函数group by和Oracle中分析函数partition by的用法以及区别 2.开窗函数.