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 ...
随机推荐
- 漫说996icu黑名单
以实际行动声援996icu项目. https://github.com/996icu/996.ICU/blob/master/blacklist/blacklist.md 996公司黑名单,京东,华为 ...
- C和C++头文件大全
C.传统 C++ #include <assert.h> //设定插入点#include <ctype.h> //字符处理#include <errno.h> ...
- Greenplum hostname和address不一致导致配置文件无法加载
最近又遇到了几个坑,逐一记录分析下. 1.主机名hostname和address不一致 在又一次部署压测环境交由测试组进行压测时,同事修改了pg_hba.conf文件重新加载配置文件时报错.(找不到l ...
- ASP.NET -- 一般处理程序ashx
ASP.NET -- 一般处理程序ashx 如果在一个html页面向服务器端请求数据,可用ashx作为后台页面处理数据.ashx适合用作数据后台处理,相当于WebForm中的aspx.cs文件或 ...
- ubuntu16.04如何安装多个版本的CUDA
我的机器是CUDA16.04的,之前装过CUDA10.0,因为一些原因,现在需要安转CUDA9.0. 1.首先https://developer.nvidia.com/cuda-90-download ...
- java 对象属性复制,将一个对象的属性值赋值给另一个对象, 属性名需要相同
import org.springframework.beans.BeanUtils; BeanUtils.copyProperties(源对象, 目标对象);
- CISCO ACL配置
ACL:access(访问)control(控制)list(列表),用来实现防火墙规则. 访问控制列表的原理对路由器接口来说有两个方向出:已经经路由器的处理,正离开路由器接口的数据包入:已经到达路由器 ...
- node基础—模块系统
模块的概念 为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块加载系统. 在 Node.js 中,文件和模块是一一对应的(每个文件被视为一个独立的模块),换言之,一个 Node ...
- Linux系统高树攀登之路
Linux系统高树攀登之路 Linux作为一种操作系统已经发展了这么长的时间,已然有了很多的“粉丝”,其中不乏有“通天之力”的Linux大牛,也有能在Linux世界里“快乐畅游”的强者,同时也有想要攀 ...
- 深度学习之Attention Model(注意力模型)
1.Attention Model 概述 深度学习里的Attention model其实模拟的是人脑的注意力模型,举个例子来说,当我们观赏一幅画时,虽然我们可以看到整幅画的全貌,但是在我们深入仔细地观 ...