Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]
题目:
Given a sorted array, 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 in place with constant memory.
For example,
Given input array 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 new length.
翻译:
给定一个排序好的数组,就地移除反复的元素。来使得每一个元素仅仅出现一次,而且返回新的长度。
不要分配额外的空间给还有一个数组。你必须完毕它在原数组上。而且仅仅使用常数级的内存。
分析:
因为须要移除反复的元素,所以必须有移动元素的操作。当遍历的i指针指向的元素与上一个元素反复时。须要採用一个指针nextEmpty来记录当前这个位置(须要被移除的位置,也就是要把后面的元素复制过来的位置),当遍历到下一个不反复的元素时。再拷贝到这个位置。
代码:
public class Solution {
public int removeDuplicates(int[] nums) {
int nextEmpty=1;
for(int i=1;i<nums.length;i++){
if(nums[i]!=nums[i-1]){
nums[nextEmpty]=nums[i];
nextEmpty++;
}
}
return nextEmpty;
}
}
Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: Easy]的更多相关文章
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 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:26. Remove Duplicates from Sorted Array(Easy)
1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 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 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
随机推荐
- jQuery学习笔记(2)-选择器的使用
一.选择器是什么 有了jQuery的选择器,我们几乎可以获取页面上任意一个或一组对象 二.Dom对象和jQuery包装集 1.Dom对象 JavaScript中获取Dom对象的方式 <div i ...
- P1044 栈
题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈). 栈的重要性不言自明,任何 ...
- 我的DBDA类
<?php class DBDA { public $host="localhost"; public $uid="root"; public $pwd= ...
- 【译】x86程序员手册22-6.4页级保护
6.4 Page-Level Protection 页级保护 Two kinds of protection are related to pages: 与页相关的保护有两类: Restriction ...
- clipboard.min.js 复制表格内容
<script type="text/javascript" src="js/clipboard.min.js"></script> & ...
- nginx--提供一键安装脚本
nginx特点 基于进程池实现的fastcgi 单一进程即可实现处理上千的连接 易于扩展的插件系统 安装篇 *下载源码 curl -O http://nginx.org/download/nginx- ...
- CAD控件:QT开发使用控件入门
1. 环境搭建: 3 1.1. 安装Qt 3 1.2. 安装Microsoft Windows SDK的调试包 6 2. QT中使用MxDraw控件 7 1.3. 引入控件 7 3. 打开DWG文件 ...
- Redis系列(十)--集群cluster
在之前学习了Master-Slave.Sentinel模式,但是在某些情况下还是无法满足系统对QPS等要求,这时候就需要Cluster,Redis3.0支持了cluster 一.为什么使用Cluste ...
- ThinkPHP---拓展之jQuery的ajax
[前言] 用Sublime开发时,推荐下载一个jQuery插件,可以智能化创建基本函数格式,支持自动生成,可以提高开发效率 (1)jQuery里ajax方法有几个? 答:有4个,分别为post.get ...
- 00The C Programming Language
The C Programming Language C语言是一门面向过程.抽象化的通用程序设计语言,广泛应用于底层开发.C语言能以简易的方式编译.处理低级存储器.C语言是仅产生少量的机器语言以及不需 ...