LeetCode(26) Remove Duplicates from Sorted Array
题目
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 in place with constant memory.
For example,
Given input array 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.
分析
这么一个简单的题目,竟然3次才AC。太失败了!
总结,就是只顾得求出不重复元素的个数,却忽略了输入参数是引用,必须同时使得数组中存储为不重复元素。也就是求取数目的同时必须更新数组中元素不能重复。
AC代码
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() <= 1)
return nums.size();
int i = 0, j = 1;
while (j < nums.size())
{
if (nums[i] == nums[j])
{
++j;
}
else
{
nums[++i] = nums[j++];
}
}
return i + 1;
}
};
LeetCode(26) Remove Duplicates from Sorted Array的更多相关文章
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【LeetCode算法-26】Remove Duplicates from Sorted Array
LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- leetcode第26题--Remove Duplicates from Sorted Array
problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- LeetCode(82)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- LeetCode(83)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- LeetCode(33)Search in Rotated Sorted Array
题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...
随机推荐
- jsp include
1.<%@ include file="a.jsp"%> 路径无法动态赋值,只能写成固定路径: 生成一个jsp页面,整个编译 2.<jsp:include pag ...
- Python while 1 和 while True 速度比较
References http://legacy.python.org/dev/peps/pep-0285/http://stackoverflow.com/questions/3815359/whi ...
- Queue Sequence HDU - 4441
码力不行啊... 错误记录: 171行后面对find2的使用错误,原来写的是p=find2(rt,p1),然后再加上一句能过样例但很假的特判 事实上,现在是要寻找最大的j,使得d2[1..j-1]=p ...
- centOS 部署服务器(二)
(1)安装nginx 1.下载地址: http://nginx.org/en/download.html ,并解压到目录下 2.安装依赖包 yum -y install pcre* yum -y i ...
- Hibernate通过实体对象对应数据库表信息
Hibernate通过实体对象对应数据库表信息,包括:数据库表名称.主键列名.非主键列名等. 获取对象映射缓存管理类: AbstractEntityPersister aep = (AbstractE ...
- OC的单例模式
原文: http://www.galloway.me.uk/tutorials/singleton-classes/ 在iOS开发中,单例是最有用的设计模式之一.它是在代码间共享数据而不需要手动传递参 ...
- 477 Total Hamming Distance 汉明距离总和
两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...
- Spring------自动化装配Bean(二)
上一篇是基于 @ComponentScan自动装配Bean的实现,这一篇将通过java手动装配bean来实现. 手动装配相对于自动装配的优点: 可以自行定义Bean的各个属性. 添加额外的方法调度. ...
- P1984 [SDOI2008]烧水问题
题目描述 把总质量为1kg的水分装在n个杯子里,每杯水的质量均为(1/n)kg,初始温度均为0℃.现需要把每一杯水都烧开.我们可以对任意一杯水进行加热.把一杯水的温度升高t℃所需的能量为(4200*t ...
- 借教室 线段树and二分
描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然希望 ...