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 ...
随机推荐
- PHP 生成器语法
一般你在迭代一组数据的时候,需要创建一个数据,假设数组很大,则会消耗很大性能,甚至造成内存不足. //Fatal error: Allowed memory size of 1073741824 by ...
- C#-事件(十八)
概述 事件(Event) 基本上说是一个用户操作,如按键.点击.鼠标移动 使用事件,可以很方便地确定程序执行顺序 事件在类中声明且生成,且通过使用同一个类或其他类中的委托与事件处理程序关联 包含事件的 ...
- mssql sqlserver 使用sql脚本输出交替不同的背景色的html信息的方法分享
转自:http://www.maomao365.com/?p=6679 摘要: 下文将分享使用sql脚本输出交替变换的不同背景颜色的sql脚本的方法分享,如下所示: 实验环境:sqlserver 20 ...
- VS调式时出现异常,在输入法是中文状态下,输入框输入字母再回车,会造成页面关闭,vs退出调式
解决方案:关闭浏览器窗口关闭时,停止调试 .就恢复正常,无异常
- sqlserver 删除表中 指定字符串
源表T "单据编号" "航班计划日期" "航班号" "起飞航站代码&q ...
- 介绍一个比较了各种浏览器对于HTML5 等标准支持程度的网站
可以选择浏览器种类,版本,比较的功能 网站地址:https://caniuse.com/#comparison
- Python3基础-函数实例学习
内置函数 绝对值函数 x = abs(100) y = abs(-20) print('x=100的绝对值为:{}'.format(x)) print('y=-20的绝对值为:{}'.format(y ...
- 你可能不知道的printf
前言 printf可能是我们在学习C语言的过程中最早接触的库函数了.其基本使用想必我们都已经非常清楚了.但是下面的这些情况你是否已经清楚地知道了呢? 示例程序 我们来看一个示例程序,看看你能否对下面的 ...
- C#基础知识之String,Stringbuilder和Stringbuffer
String可以储存和操作字符串,即包含多个字符的字符数据.这个String类提供了存储数值不可改变的字符串. StringBuilder是线程不安全的,运行效率高,如果一个字符串变量是在方法里面定义 ...
- IE和其他浏览器内核
1.qq急速 2.qq的IE兼容模式 3.Edge 4.IE11 5.chrome js获取浏览器内核 <script language="JavaScript" type= ...