博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
26. Remove Duplicates from Sorted Array*(快慢指针)
阅读量:4325 次
发布时间:2019-06-06

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

description:

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

Note:

Example:

Example 1:Given 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 returned length.Example 2:Given nums = [0,0,1,1,1,2,2,3,3,4],Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.It doesn't matter what values are set beyond the returned length.

my answer:

快慢指针,慢的指向的一直都是新的,也就是说来一个新的它才往前走一步,快的就是不断向前,发现和慢的不一样的就汇报给那个慢的,然后慢的更新。如果二者相等,则慢的不动,快的前进一步;如果二者不等,则慢的前进一步的同时更新那个新一步指向的数字为现在快指针指向的新数字,快的同时也往前走一步。

 

大佬的answer:

class Solution {public:    int removeDuplicates(vector
& nums) { if(nums.empty()) return 0; int n = nums.size(); int pre = 0, cur = 0; while(cur

relative point get√:

hint :

转载于:https://www.cnblogs.com/forPrometheus-jun/p/10889152.html

你可能感兴趣的文章
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
Django 学习笔记(五) --- Ajax 传输数据
查看>>
Spring boot 日志 Logback
查看>>
基于OWIN WebAPI 使用OAUTH2授权服务【授权码模式(Authorization Code)】
查看>>
[深入Maven源代码]maven绑定命令行参数到具体插件
查看>>
laravel 分页使用
查看>>
RobotFramework自动化2-自定义关键字
查看>>
centos6.4-x86-64系统更新系统自带Apache Http Server
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>
BABOK - 需求分析(Requirements Analysis)概述
查看>>
第43条:掌握GCD及操作队列的使用时机
查看>>
Windows autoKeras的下载与安装连接
查看>>
CMU Bomblab 答案
查看>>
微信支付之异步通知签名错误
查看>>