LeetCode第26题

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

Example 1:

Given 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 returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}

翻译:

给定一个已排序的数组,删除其中重复的部分,保证每个数字只出现一次,返回一个新的数组长度。

不要申明额外的数组空间,必须保证算法复杂度为O(1)

思路:

既然不能申明额外的数组,那只能在原来的数组上做变动

变动前:[1,1,2,3,3]

变动后:[1,2,3,3,3]

前3个值[1,2,3]就是我们所需要的

代码:

class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length == 0) return 0;
int j =0;
for(int i = 0;i<nums.length;i++){
if(nums[j]!=nums[i]){
j++;
nums[j] = nums[i];
}
}
return j+1;
}
}

原数组遍历一遍后,将不重复的数字保存在数组的前面,j就是我们需要的数据的最大下标,那么j+1就是我们需要的长度

欢迎关注我的微信公众号:安卓圈

【LeetCode算法-26】Remove Duplicates from Sorted Array的更多相关文章

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

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

  2. 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 ...

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

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

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

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

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

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

  6. 【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 ...

  7. 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 ...

  8. 【算法】LeetCode算法题-Remove Duplicates from Sorted Array

    这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...

  9. 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- ...

  10. LeetCode:26. Remove Duplicates from Sorted Array(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...

随机推荐

  1. springboot整合mybatis及封装curd操作-配置文件

    1 配置文件  application.properties  #server server.port=8090 server.address=127.0.0.1 server.session.tim ...

  2. mysql根据某一个字段查询数量大于1的数据

    分组条件:org_code select count(1) from qyt_company_info t GROUP BY t.org_code HAVING count(1)>1;

  3. scrapy框架爬取糗妹妹网站妹子图分类的所有图片

    爬取所有图片,一个页面的图片建一个文件夹.难点,图片中有不少.gif图片,需要重写下载规则, 创建scrapy项目 scrapy startproject qiumeimei 创建爬虫应用 cd qi ...

  4. ThreadLocal源码原理以及防止内存泄露

    ThreadLocal的原理图: 在线程任务Runnable中,使用一个及其以上ThreadLocal对象保存多个线程的一个及其以上私有值,即一个ThreadLocal对象可以保存多个线程一个私有值. ...

  5. 《BUG创造队》第四次作业:基于原型的团队项目需求调研与分析

    项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验八 团队作业4:基于原型的团队项目需求调研与分析 团队名称 BUG创造队 作业学习目标 (1)体验以原型设计为基础的团队 ...

  6. selenium获取元素信息方法(转载)

    1.获取当前页面的Url函数 方法:current_url 实例: driver.current_url 2.获取元素坐标 方法:location 解释:首先查找到你要获取元素的,然后调用locati ...

  7. Gym-100648B: Hie with the Pie(状态DP)

    题意:外卖员开始在0号节点,有N个人点了外卖,(N<=10),现在告诉两两间距离,问怎么配送,使得每个人的外卖都送外,然后回到0号点的总时间最短,注意,同一个点可以多次经过. 思路:TSP问题( ...

  8. stm32flash的读写特性

    在使用stm32自带的flash保存数据时候,如下特点必须知道: 1.必须是先擦除一个扇区,才能写入 2.读数据没有限制 3.写数据必须是2字节,同时写入地址以一定要考虑字节对齐, 4.一般都是在最后 ...

  9. python中的object

    继承 object 类的是新式类,不继承 object 类的是经典类,在 Python 2.7 里面新式类和经典类在多继承方面会有差异: class A: def foo(self): print(' ...

  10. Mysql 创建只读账户

    mysql 创建只读账户: 1.查询所有账号信息 SELECT DISTINCT a.`User`,a.`Host`,a.password_expired,a.password_last_change ...