leetcode 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 ofnumsbeing1and2respectively. 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 ofnumsbeing modified to0,1,2,3, and4respectively. It doesn't matter what values are set beyond the returned length.
想法:直接使用STL相关函数,先使用unique去掉重复。此时重复的元素并没有被删掉,只是移到了后面,该函数返回无重复元素的最后一个元素的地址
,然后在使用distance()函数得到个数
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
return distance(nums.begin(), unique(nums.begin(), nums.end()));
}
};
另外手动实现版本
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty())
{
;
}
;
; i < nums.size(); i++ )
{
if ( nums[index] != nums[i] )
{
nums[ ++index ] = nums[i];
}
}
;
}
};
leetcode 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 有序数组中去除重复项
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 ...
随机推荐
- 设计模式-原型(prototype)
一.概念 用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. 二.模式动机 当已有一个对像,暂且称之为原型对象,需要一个新的对像,该对像和已有的原型对像具有相同的类型,且里面的属性大部分 ...
- webhttpbinding、basichttpbinding和wshttpbinding的区别
webhttpbinding是REST风格的绑定,您只需点击一个URL,然后从Web服务中获取大量XML或JSON. basichttpbinding和wshttpbinding是两个基于SOAP的绑 ...
- Codeforces339D(SummerTrainingDay06-A 线段树)
D. Xenia and Bit Operations time limit per test:2 seconds memory limit per test:256 megabytes input: ...
- python学习之老男孩python全栈第九期_day009作业
1. 写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. 答: l1 = [] def odd(li): for i in range(1,len(li),2 ...
- 前端面试(原生js篇) - DOM
根据我的面试经历,一般小公司的面试环节,比较关心框架的熟练程度,以及独立开发组件的能力 但大厂通常有五轮以上的面试,而且对 js 基础语法很是看重 于是我总结了一些关于 js 基础的面试对话,有的当时 ...
- enum 的使用方法(java)
作者QQ:1095737364 QQ群:123300273 欢迎加入! enum很像特殊的class,实际上enum声明定义的类型就是一个类.而这些类都是类库中Enum类的子类(java ...
- 【代码笔记】iOS-NSJSONSerializationDemo
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...
- Hystrix 框架
雪崩效应的产生原因:当一个服务突然受到高并发的请求,tomcat服务器承受不了的情况下会产生服务堆积,可能导致其他的服务也不可用. 服务保护:当服务产生堆积的时候,对服务实现保护功能. 服务隔离:每个 ...
- Java String 和JSON转换
Java项目中经常会使用到JSON格式和String格式的数据,所以二者之间的转换也是一个重要的步骤. Sting类型的数据.如: 转化为JSONObject的步骤如下: 1).把字符串转成 JSON ...
- Android 状态栏开发
又好久没写了...还是记个笔记吧.这次关于Android手机App状态栏的各种处理做一个笔记. 场景一:需要做全屏,不看到手机状态栏信息(手机电量,信号等) 这种需求一般用的比较多的地方是App的Sp ...