博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
26. Remove Duplicates from Sorted Array(代码思路新奇)
阅读量:7026 次
发布时间:2019-06-28

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

Given a sorted array, remove the duplicates  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  with O(1) extra memory.

Example:

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 new length.
class Solution {public:    int removeDuplicates(vector
& nums) { int count = 0, n = nums.size(); for(int i = 1; i < n; i++){ if(nums[i] == nums[i-1]) count++; else nums[i-count] = nums[i]; } return n-count; }};

 

转载于:https://www.cnblogs.com/Pretty9/p/8593587.html

你可能感兴趣的文章
Java Bean + 注册验证
查看>>
通过mysql 插入一句话***
查看>>
centos 分区扩容
查看>>
JBoss EAP 6 monitoring using remoting-jmx and Zabbix
查看>>
邮件服务器
查看>>
OOAD-设计模式-原型模式
查看>>
Java FAQ(2)
查看>>
JavaScript常用事件总结
查看>>
squid在企业网中的应用
查看>>
主元素 Majority Element
查看>>
关于加密的一些笔记
查看>>
我的友情链接
查看>>
Oracle 数据库查看client的用户登录信息包括ip
查看>>
Kernel Trace System
查看>>
linux文件系统详解
查看>>
我的友情链接
查看>>
ibatis mybatis sql语句配置 符号不兼容 大于号 小于号
查看>>
Alipay 开源 SofaRPC
查看>>
jquery的extend与fn.extend
查看>>
自动化测试应该学什么
查看>>