博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Remove Duplicates from Sorted Array
阅读量:7070 次
发布时间:2019-06-28

本文共 648 字,大约阅读时间需要 2 分钟。

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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

主要思路是,有几个不相同的元素,它的下标就是不相同的元素个数减一,因此,当元素相同的时候,下标不变化,但是元素向后移动。

C++代码实现:

#include
using namespace std;class Solution {public: int removeDuplicates(int A[], int n) { int i; int count=1; //注意数组为空的情况 if(n==0) return 0; for(i=1;i

运行结果:

转载地址:http://mmzml.baihongyu.com/

你可能感兴趣的文章
java环境变量设置
查看>>
为Redmine的项目加上起止时间
查看>>
win7系统中任务计划程序的使用与查询
查看>>
站在OC的基础上快速理解Swift的类与结构体
查看>>
解决因特网和xshell考虑到问题
查看>>
hdoj Scaena Felix
查看>>
Android之Handler的postDelayed()使用方法
查看>>
iOS UI进阶-2.0 CALayer
查看>>
hadoop中遇到的问题。
查看>>
结构体
查看>>
从头开始编写项目Makefile(八):型号规则
查看>>
lintcode 中等题:k Sum ii k数和 II
查看>>
七天来学习ASP.NET MVC (两)——ASP.NET MVC 数据传输
查看>>
CSS+DIV定位分析(relative,absolute,static,fixed) (转载)
查看>>
Linux下*.tar.gz文件解压缩命令
查看>>
Android 系统稳定性 - ANR(一)
查看>>
Meta 的两个 相关属性
查看>>
hdu 5562 Clarke and food(贪心)
查看>>
css案例学习之全局声明*{} 与body{}的区别
查看>>
junit测试时,出现java.lang.IllegalStateException: Failed to load ApplicationContext
查看>>