leetcode第26题--Remove Duplicates from Sorted Array
problem: 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 A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
受上题目的启发,如果n为零,则返回零,如果不为零,则num和i从1开始,当A[i] != A[i-1]时,我们就把A[i]赋值给A[num],同时num++,最后num就是新数组的长度。
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n == )
return ;
int num = ;
for ( int i = ; i < n; i++)
{
if(A[i - ] != A[i])
A[num++] = A[i];
}
return num;
}
};
2015/03/29:
Python:
class Solution:
# @param a list of integers
# @return an integer
def removeDuplicates(self, A):
if len(A) == 0:
return 0
start = 0
for i in range(1, len(A)):
if A[i] != A[start]:
start += 1
A[start] = A[i]
return start + 1
leetcode第26题--Remove Duplicates from Sorted Array的更多相关文章
- 【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
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)
①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- [算法题] Remove Duplicates from Sorted Array ii
题目内容 本题来源LeetCode Follow up for "Remove Duplicates": What if duplicates are allowed at mos ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- [算法题] Remove Duplicates from Sorted Array
题目内容 本题来源于LeetCode Given a sorted array, remove the duplicates in place such that each element appea ...
- 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 ...
随机推荐
- 使用python+flask让你自己api(教程源代码)
1.背景 ok,这可能是很多朋友和我一样经常使用的各种api,例facebook的.github的.甚至微信api.因此,很多人都想使自己的api.在线教程在这方面它是非常小的,今天,我做了一个平稳, ...
- python 导入库问题
最终解决如下面:我不知道有没有多余的空间 from django.conf import settings from sys import path path.extend(['/home/zoues ...
- STUN协议简介
STUN简要 STUN(Simple Traversal of UDP over NATs,NAT 的UDP简单穿越)是一种网络协议.它同意位于NAT(或多重NAT)后的client找出自己的公网地址 ...
- ubunut 查看port被哪个程序占用
查看8087port被哪个程序占用 lsof -i :8087 -n
- 国外android开源站点
http://android-arsenal.com/
- MVC中,视图的Layout使用
本文目标 1.能够重用Razor模板进行页面的组件化搭建 本文目录 1.母板页_Layout.cshtml 2.用户自定义控件 3.默认Layout引用的使用(_ViewStart.cshtml) 1 ...
- VS2010-使用“预先生成事件命令行”和“后期生成事件命令行”功能
原文:VS2010-使用"预先生成事件命令行"和"后期生成事件命令行"功能 xcopy /r /y $(TargetPath) $(ProjectDir)..\ ...
- 【C++基础】类的组合
所谓类的组合是指:类中的成员数据是还有一个类的对象或者是还有一个类的指针或引用.通过类的组合能够在已有的抽象的基础上实现更复杂的抽象. 比如: 1.按值组合 #include<iostream. ...
- java_eclipse_svn 与服务器同步时 ,忽略某类型文件和文件夹
1. 在项目开发中使用svn ,带来很大的方便,有时我们会把整个项目上传的svn服务器上 这样就包含了 编译过的class文件 以及 一些 .svn,.log文件,有些文件时本地complie 的 ...
- cocos2d-x多分辨率和随后的自适应CCListView的bug修复
cocos2d-x多分辨率自适配及因此导致的CCListView的bug修复 cocos2d-x是一款众所周知的跨平台的游戏开发引擎.因为其跨平台的特性.多分辨率支持也自然就有其需求. 因此.在某一次 ...