【LeetCode算法-26】Remove Duplicates from Sorted Array
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的更多相关文章
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- 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 ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array
一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【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 ...
- 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 ...
- 【算法】LeetCode算法题-Remove Duplicates from Sorted Array
这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...
- 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- ...
- LeetCode:26. Remove Duplicates from Sorted Array(Easy)
1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...
随机推荐
- etcd数据备份和恢复--转发
对于etcd api v3数据备份与恢复方法 # export ETCDCTL_API=3 # etcdctl --endpoints localhost:2379 snapshot save sna ...
- js获取ip内网地址
<script type="text/javascript"> function getUserIP(onNewIP) { // onNewIp - your list ...
- linux卸载mysql误删mysql.pm
操作步骤如下 linux卸载mysql:yum remove mysql 查找mysql所有的文件并删除: 查找:find / -name mysql 删除:rm -rf xxx 误操作删除mysql ...
- js插件---videojs的使用
js插件---videojs的使用 一.总结 一句话总结: 网上有各种细致的现成的代码可以拿来用,没必要自己死专 1.video.js有两种初始化方式? 一种是在video的html标签之中 一种是使 ...
- Spring容器、BeanFactory和ApplicationContext,及3种装配Bean的方式
目录 一. spring容器理解 二. BeanFactory和ApplicationContext之间的关系 三. BeanFactory详情介绍 四.ApplicationContext介绍 五. ...
- postgresql从库提升为主库
一.停主库 1.查看当前连接 select pid,datname,usename,client_addr,client_port, application_name from pg_stat_act ...
- centos7最小化安装无法tab补全
yum install -y bash-completion 安装完后reboot重启生效
- 【luoguP2997】[USACO10NOV]旗帜Banner
题目链接 长和宽的gcd(x,y)=1,就没有中间结点,一种线段有两种方向,暴力统计一下就好了 注意x=0或y=0时的线段只有一种方向 #include<iostream> #includ ...
- 洛谷P2744 量取牛奶
题目 DP或者迭代加深搜索,比较考验递归的搜索. 题目第一问可以用迭代加深搜索限制层数. 第二问需要满足字典序最小,所以我们可以在搜索的时候把比当前答案字典序大的情况剪枝掉. 然后考虑怎么搜索,对于每 ...
- 模板 - 部分C++库
__builtin系列 据说是GCC自带的系列,在本地装有 GNU GCC Compiler 的 Codeblocks 和 Codeforces 等平台都可以使用这些.但是没办法从 Codeblock ...