[array] leetCode-26. Remove Duplicates from Sorted Array - Easy
26. Remove Duplicates from Sorted Array - Easy
descrition
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 by modifying the input array in-place with O(1) extra memory.
Example
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 new length.
解析
注意题目的要求,in_place,即原址,不能借助额外的辅助空间。
再关注到另一个有利的前提条件,数组是有序的,因此重复的数字一定挨着,这是达到最优解的关键,时间复杂度 O(n)。
code
#include <vector>
#include <algorithm>
using namespace std;
class Solution{
public:
int removeDuplicates(vector<int>& nums){
if(nums.empty()) // don't forget the special case!!!!
return 0;
int cur = 0; // the end of new nums
for(int i=1; i<nums.size(); i++){
if(nums[i] != nums[cur]){
nums[++cur] = nums[i];
}
}
return cur+1; // return the new length
}
};
int main()
{
return 0;
}
[array] leetCode-26. Remove Duplicates from Sorted Array - Easy的更多相关文章
- 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 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- 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 26 Remove Duplicates from Sorted Array
Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- Java [leetcode 26]Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- Leetcode 26. Remove Duplicates from Sorted Array (easy)
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有序数组去重(单个元素只出现一次)
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 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 ...
随机推荐
- Entity Framework的原理及使用方式
ADO.NET Entity Framework操作数据库的过程对用户是透明的(当然我们可以通过一些工具或方法了解发送到数据库的SQL语句等).我们唯一能做的是操作EDM,EDM会将这个操作请求发往数 ...
- DG的数据保护模式
DG的数据保护模式 数据保护膜有三种: – Maximum protection – Maximum availability – Maximum performance Maximum protec ...
- CSS 文本样式
1. 文本样式 text <!--文本颜色color --> .text {color:red;} 2. 文本对齐方式 text-align <style> .text ...
- Java中的继承和接口
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 这是个老话题,继承和接口是实现多态的两种方式,如果对象很多,其中一对一对的有共同点,建议用继承,如果 ...
- 优化HTML代码的多种技巧
怎样提升Web页面的性能,非常多开发者从多个方面来下手如JavaScript.图像优化.server配置,文件压缩或是调整CSS. 非常显然HTML 已经达到了一个瓶颈.虽然它是开发Web 界面必备的 ...
- 玩转Bash脚本:选择结构之case
总第5篇 之前,我们谈到了if. 这次我们来谈还有一种选择结构--case. case与if if用于选择的条件,不是非常多的情况,假设选择的条件太多.一系列的if.elif,.也是醉了. 没错,ca ...
- animation- 动画效果实现(xml形式实现)
1.定义xml动画 1)在anim文件夹下定义xml文件 解释:这个文件夹下面的文件会被默认为动画文件,如果这个文件不存在,需要自己创建 display_result_anim.xml <?xm ...
- 28.Node.js 函数和匿名函数
转自:http://www.runoob.com/nodejs/nodejs-module-system.html 在JavaScript中,一个函数可以作为另一个函数的参数.我们可以先定义一个函数, ...
- nginx安装部署+增加媒体播放模块
nginx安装很简单,但是有的时候是已经安装的nginx ,升级增加nginx 模块功能. 最近公司要nginx增加一个可以播放 MP4的模块,安装还算顺利,不说废话上命令. 1 安装依赖 yum i ...
- Linux的一些简单命令操作
防火墙 查看防火墙状态 systemctl status iptables (或service iptables status) 关闭防火墙 systemctl stop iptables(或serv ...