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的更多相关文章

  1. 【LeetCode算法-26】Remove Duplicates from Sorted Array

    LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  2. LeetCode(26) Remove Duplicates from Sorted Array

    题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  3. [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)

    ①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...

  4. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  5. [算法题] Remove Duplicates from Sorted Array ii

    题目内容 本题来源LeetCode Follow up for "Remove Duplicates": What if duplicates are allowed at mos ...

  6. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  7. [算法题] Remove Duplicates from Sorted Array

    题目内容 本题来源于LeetCode Given a sorted array, remove the duplicates in place such that each element appea ...

  8. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  9. LeetCode(80)Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. Android处理延迟加载的方法

    在项目开发,通过延时载入来实现满足我们的项目要求.那究竟如何来实现延时.以下结合java与android的相关方法来实现延时问题. 一.利用线程的Sleep方法 <span style=&quo ...

  2. html不常见问题汇总

    写html已经好长一段时间了,也遇到了不少问题,跟大家分享下 form是不可以嵌套的 说明:如果嵌套会有很多问题 但是可以并列 <html> <head> </head& ...

  3. 移动端 延迟加载echo.js的使用

    浏览器支持ie8+   <img src="img/blank.gif" alt="" data-echo="img/album-1.jpg&q ...

  4. ASP.NET Identity 身份验证和基于角色的授权

    ASP.NET Identity 身份验证和基于角色的授权 阅读目录 探索身份验证与授权 使用ASP.NET Identity 身份验证 使用角色进行授权 初始化数据,Seeding 数据库 小结 在 ...

  5. CSS小记(持续更新......)

    1 内联元素和块级元素的区别以及display的三种属性区别 在说起display属性之前,就必须先说说什么是文档流,以及文档流中的内敛元素和块级元素又是指什么? 一直觉得理解了文档流的概念,一堆CS ...

  6. NET ERP系统架构设计

    解析大型.NET ERP系统架构设计 Framework+ Application 设计模式 我对大型系统的理解,从数量上面来讲,源代码超过百万行以上,系统有超过300个以上的功能,从质量上来讲系统应 ...

  7. DHot.exe 热点新闻

    别人的电脑上的今日插件U菜,打开几个PPT文件,和一个视频文件(默认的音频和视频打开百度),结果突然弹出一个热点广告信息表,形式与风格QQ非常相似,例如下面的附图: 托盘图标: 经过搜索.得到例如以下 ...

  8. asp.net 百度编辑器 UEditor 上传图片 图片上传配置 编辑器配置 网络连接错误,请检查配置后重试

    1.配置ueditor/editor_config.js文件,将 //图片上传配置区 ,imageUrl:URL+"net/imageUp.ashx" //图片上传提交地址 ,im ...

  9. CSS定位与层叠

    position:static(静态定位)     当position属性定义为static时,可以将元素定义为静态位置,所谓静态位置就是各个元素在HTML文档流中应有的位置 podisition定位 ...

  10. android控制文件:ViewPager+Fragment+GridView使用(与AndroidQuery框架结合)

    最近我看到一个AndroidQuery该框架.里面Demo,有一个屏幕,让博主喜欢.很顺利的左右滑动,感觉非常好,所以拿来和大家分享一下.看看结果图.: 从图中能够看出.上面的布局是一个Layout里 ...