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 ...
随机推荐
- ERROR 1050 (42S01): Table xxx already exists
今天遇到一个关于MySQL求助的问题,修改表结构时遇到"ERROR 1050 (42S01): table xxx already exits" mysql> ALTER ...
- c/c++ 函数模板初探
函数模板初探 1,由来:有时候,函数的逻辑是一样的,只是参数的类型不同,比如下面 int Max(int a, int b){ return a > b ? a : b; } double Ma ...
- iOS In-App Purchase(IAP)内购服务端二次验证注意事项
前端iOS完成对应的商品购买之后,会得到一个Transaction(交易)的数据结构指针,后端实际上只需要这个结构内的一个东西,那就是 transaction.transactionReceipt. ...
- td 元素属性 noWrap 防止折行、撑开及文字换行
最近调试程序,遇到如下问题: 也就是这个表格里面的文字被换行了,究其原因,主要是td中的width之和超过了100%导致的.谷歌了好久,终于发现,可以用noWrap="noWrap" ...
- 第八章 计时器(BEEPER1)
*------------------------------------- BEEPER1.C -- Timer Demo Program No. (c) Charles Petzold, ---- ...
- Python开发【第三篇】:函数&读写文件
三元运算 三元运算,是条件语句的简单的写法.如果条件为真,则返回值1,否则,返回值2. ret = 值1 if 条件 else 值2 深浅拷贝 对于数字(int)和字符串(str)而言,赋值.深拷贝. ...
- Teradata全面转型
大数据时代 Teradata全面转型 [关键点]:数据分析相关技术和方案==>帮助企业实现数据价值变现 1.所有企业达成共识 数据已经成为企业的资产,甚至是核心资产. 2.Teradata转型 ...
- display:table-cell几种应用
http://www.zhangxinxu.com/wordpress/2010/10/%E6%88%91%E6%89%80%E7%9F%A5%E9%81%93%E7%9A%84%E5%87%A0%E ...
- 「APIO2017」商旅
「APIO2017」商旅 题目描述 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所深深吸引,决定定居于此,做一个商人.科巴有 \(N\) 个集市, ...
- [转]Jquery 点击图片在弹出层显示大图
这个还行不需要别的包! https://www.cnblogs.com/antis/p/7053991.html