leetcode-algorithms-26 Remove Duplicates from Sorted Array
leetcode-algorithms-26 Remove Duplicates from Sorted Array
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]);
}
解法
class Solution {
public:
int removeDuplicates(vector<int>& nums)
{
auto iter = nums.begin();
while(iter != nums.end())
{
int first = *iter;
++iter;
while(iter != nums.end() && first == *iter)
iter = nums.erase(iter);
}
return nums.size();
}
};
时间复杂度: O(n).
空间复杂度: O(1).
leetcode-algorithms-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 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. 题目要求 给定一个已 ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- C#Winform工具箱简介
BindingSource:指定支持事务处理初始化Button:[按钮]用户单击它时引发事件 CheckBox:[复选框]允许用户选择或清除关联选项 CheckedListBox:[复选列表框]显示一 ...
- 【ASP.NET】System.Web.Routing - PageRouteHandler Class
用于提供一些属性和方法来定义如何将URL匹配到一个物理文件上面. public PageRouteHandler (string virtualPath, bool checkPhysicalUrlA ...
- Kylin工作原理、体系架构
核心思想:预计算. 对多维分析可能用到的度量进行预计算,将计算好的结果保存成Cube,并存在HBase中,供查询时直接访问 将高复杂度的聚合运算.多表连接……操作转换成对预计算结果的查询.决定了Kyl ...
- 程序修改图标后显示未更新——强制刷新windows图标缓存
http://blog.csdn.net/vvlowkey/article/details/51133486 20160412 问题:修改兴迪局放测量软件图标后,release文件夹中生成文件的小图标 ...
- 【Luogu P2764】最小路径覆盖问题
网络流 \(24\) 题之一. Problem Description 给出一个 \(n\) 个点 \(m\) 条边的 \(DAG\) ,求最小路径点覆盖,并输出路径选择方案. Input Forma ...
- python线程 有问题?
- windows 7 系统下,用户每次登录都是以临时配置文件的形式存在于users文件夹下
windows 7 系统下,用户每次登录都是以临时配置文件的形式存在于users文件夹下 当用户登录系统后,在users文件夹下创建的是一个临时文件夹,如果当前用户log off,那么当前用户的所有设 ...
- 主键非自增列 EF 插入数据库引起的 ID 列不能为 NULL 的错误
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<PostBo ...
- 【Selenium2】【selenium之 定位以及切换frame(iframe)】
参考:http://blog.csdn.net/huilan_same/article/details/52200586 总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切, ...
- Pandas中关于accessor的骚操作
来自:Python那些事 pandas中accessor功能很强大,可以将它理解为一种属性接口,通过它获得额外的方法. 下面用代码和实例理解一下: import pandas as pd pd.Ser ...