Chinaunix首页 | 论坛 | 博客

AAA

  • 博客访问: 18400
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 330
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-17 08:23
个人简介

好记性不如烂笔头,记录学习历程和随想云云,欢迎各位大虾拍砖

文章分类

全部博文(33)

文章存档

2015年(33)

我的朋友
最近访客

分类: C/C++

2015-05-07 23:28:19

【问题描述】

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.


【解决方案】

点击(此处)折叠或打开

  1. int removeDuplicates(int A[], int n) {
  2.     int i,j=1;
  3.     
  4.     if(n < 2)
  5.     {
  6.         return n;
  7.     }
  8.     
  9.     for(i = 1; i < n; i++)
  10.     {
  11.         if(A[i] != A[i-1])
  12.         {
  13.             A[j++] = A[i];
  14.         }
  15.     }
  16.     
  17.     return j;
  18. }

阅读(291) | 评论(0) | 转发(0) |
0

上一篇:Path Sum

下一篇:没有了

给主人留下些什么吧!~~