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 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.
思路:此题比较简单,大意是将数组中重复点删除,然后返回新数组的长度。数组中前n个数就是新的数组。唯一难点是不能用额外空间。
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() <= ){
return nums.size();
}
int len = ; // 记录当前的新的数组的长度
for (int i = ; i < nums.size(); i++){
if (nums[i] != nums[i - ]){
nums[len] = nums[i]; //将新的元素装到前len个
len++;
}
}
return len;
}
};
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有序数组去重(单个元素只出现一次)
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 ...
随机推荐
- hivesql优化的深入解析
转载:https://www.csdn.net/article/2015-01-13/2823530 一个Hive查询生成多个Map Reduce Job,一个Map Reduce Job又有Map, ...
- c/ c++ 多态
多态 1.多态用途 为了代码可以简单的重复使用,添加一个功能时,接口不需要修改. #include <iostream> using namespace std; class A{ pub ...
- 2016某知名互联网公司PHP面试题及答案(续)
1 写出mysql中,插入数据,读出数据,更新数据的语句 INSERT INTO 表名 VALUES ("",""): SELECT * FROM 表名:. U ...
- ASP.NET -- WebForm -- 缓存Cache的使用
ASP.NET -- WebForm -- 缓存Cache的使用 把数据从数据库或文件中读取出来,放在内存中,后面的用户直接从内存中取数据,速度快.适用于经常被查询.但不经常变动的数据. 1. Te ...
- Golang 并发简介
并发概要 随着多核CPU的普及, 为了更快的处理任务, 出现了各种并发编程的模型, 主要有以下几种: 模型名称 优点 缺点 多进程 简单, 隔离性好, 进程间几乎无影响 开销最大 多线程 目前使用最多 ...
- June 17. 2018, Week 25th. Sunday
Dad is and always will be my living, breathing superhero. 在我眼里,爸爸是现实版的超级英雄,现在.将来,永远都是. From Bindi Ir ...
- 阿里云ECS配置踩坑之路
1.利用shadowsocks配置SVN(用于软件部署环境) 2.安全组设置 3.FTP搭建 https://www.cnblogs.com/hexige/p/7809481.html
- 「APIO2017」商旅
「APIO2017」商旅 题目描述 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所深深吸引,决定定居于此,做一个商人.科巴有 \(N\) 个集市, ...
- UVA12627-Erratic Expansion(递归)
Problem UVA12627-Erratic Expansion Accept: 465 Submit: 2487Time Limit: 3000 mSec Problem Descriptio ...
- ubuntu18.04 下 使用conda安装requirement.txt指定的依赖包
首先创建特定的虚拟环境 conda create -n temp_test python=3.5 conda install anaconda 切换到该环境 conda activate temp_t ...