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 ...
随机推荐
- MongoDB的web可视化管理工具
现在许多应用都使用MongoDB存储大量的业务数据,MongoDB基于文档式的存储,在大数据行业的应用还是很普遍的.MongoDB的客户端工具也很多,基于web的却不多,国产的就更少了,下面介绍的是一 ...
- Netty接收到一个请求但是代码段执行了两次
这是因为HttpRequestDecoder把请求拆分成HttpRequest和HttpContent两部分, 所以在建立连接的时候建立了两次.
- JAVA设计模式详解(五)----------适配器模式
各位朋友好,本章节我们继续讲第五个设计模式. 在生活中,我们都知道手机内存卡是无法直接接电脑的,因为内存卡的卡槽比较小,而电脑只有USB插孔,此时我们需要用到读卡器.这个读卡器就相当于是适配器.这是生 ...
- Linux常用基本命令(file,chown)
1,file命令作用,查看文件的类型 ghostwu@dev:~$ .htm ./linux/rename ghostwu@dev:~$ .htm ./linux/rename/.htm: empty ...
- linux vim 行缩进,批量移动多行
显示行号用::set nu :49>5 从第49行开始,连接5行右移一个tab. :49,93> 从第49行开始到93行右移一个tab 选中多行,然后移动 https://jingy ...
- koajs项目之memcached实现session共享
在做nodejs服务的负载时要考虑到session共享的问题,一般常用的就是memcached方式实现的,本文主要介绍通过npm社区的几个模块轻松实现这个功能. 做koa的session一般会想到用k ...
- SQL2008R2数据库日志太大收缩方法
1.登陆项目平台数据库服务器.双击SQL Server Management Studio打开数据库管理.登陆数据库 2.如下图,打开数据库属性窗口 3.如下图,更改数据库恢复模式 4.如下图,收缩数 ...
- Android--自定义半圆环型进度(带动画)
package com.newair.ondrawtext; import android.animation.ValueAnimator; import android.annotation.Tar ...
- 使用redis 处理高并发场景
1.原理: 当同一个用户获取锁之后,会让该用户一直持有锁.同样 的用户再次获取,会根据原子性 ,lock返回true. /** * 获取锁(非公平锁), 默认获取超时为2分钟 */ public bo ...
- Python 数据分析基础小结
一.数据读取 1.读写数据库数据 读取函数: pandas.read_sql_table(table_name, con, schema=None, index_col=None, coerce_fl ...