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. 国外流行的共享网站实现:facebook,twitter,google+1,tumblr等待

    近期需要做相关的国外几个站点共享,本来我以为它会和weibo.在同样的烦恼空间,什么appkey啦,apptoken啦.api啦.结果非常意外的发现并非如此恼火. Twitter分享: https:/ ...

  2. C++随机数的使用方法

         学过别的高级语言的都知道,产生随机数用的都是相似于random这种字符,c++也不例外,在C++中使用的是rand()函数,可是不同的是,假设在C++中仅仅使用了比如 "int i ...

  3. ASN.1 Encode an Object Identifier (OID) with OpenSSL

    OID(Object Identifier) denotes an object. Examples: ------------------------------------------------ ...

  4. Linux查看网卡流量(转)

    sar 这个工具RHEL5自带有,默认也安装. 一个强大的工具(好像这些工具都蛮强的),参数很多,有时间man一下. -n参数很有用,他有6个不同的开关:DEV | EDEV | NFS | NFSD ...

  5. avalonjs1.5 入门教程

    迷你MVVM框架 avalonjs1.5 入门教程 avalon经过几年以后,已成为国内一个举足轻重的框架.它提供了多种不同的版本,满足不同人群的需要.比如avalon.js支持IE6等老旧浏览器,让 ...

  6. urlrewrite使用地址重写

    地址重写: 主要是为了站点的安全. 比如我们平时的地址请求 地址重写前,訪问路径是: /read.egov?action=read&bid=2 地址重写后,訪问路径是:/read-read-2 ...

  7. 使 IIS 6.0 可以在 64 位 Windows 上运行 32 位应用程序 试图加载格式不正确的程序。

    原文 使 IIS 6.0 可以在 64 位 Windows 上运行 32 位应用程序 试图加载格式不正确的程序. win7 64位操作系统上边运行IIS网站应用的时候,提示错误"试图加载格式 ...

  8. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(12)-实现用户异步登录和T4模板

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(12)-实现用户异步登录和T4模板 ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建  ...

  9. uva10718 - Bit Mask(贪心)

    题目:uva10718 - Bit Mask(贪心) 题目大意:给出32位无符号的整数n ,给定边界L和R,要求在这个边界里面找出一个整数,它和N做或运算得到的值最大. 解题思路:要求做或运算得到的值 ...

  10. JavaBean中DAO设计模式介绍

    一.信息系统的开发架构 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层就是client,简单的来说就是浏览器. 2.显示层:JSP/ ...