【Remove Duplicates from Sorted Array】cpp
题目:
https://leetcode.com/problems/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].
代码:
class Solution {
public:
int removeDuplicates(int A[], int n) {
if( n== ) return ;
int index = ;
for (unsigned int i=; i<n; i++)
{
if( A[index]!=A[i] )
{
A[++index] = A[i];
}
}
return index+;
}
};
Tips:
O(n)时间复杂度 O(1)空间复杂度
常用数组去重技巧 记住即可
=============================
第二遍刷的时候,第二次才AC。原因是++prev第一次写成了prev++,细节要注意。
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if ( nums.size()== ) return ;
int prev = ;
for ( int i=; i<nums.size(); ++i )
{
if ( nums[i]!=nums[prev] )
{
nums[++prev] = nums[i];
}
}
return prev+;
}
};
【Remove Duplicates from Sorted Array】cpp的更多相关文章
- leetcode 【 Remove Duplicates from Sorted Array 】python 实现
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 【Remove Duplicates from Sorted List 】cpp
题目: 第一次刷的时候漏掉了这道题. Given a sorted linked list, delete all duplicates such that each element appear o ...
- 【Remove Duplicates from Sorted Array II】cpp
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- leetcode 【 Remove Duplicates from Sorted Array II 】python 实现
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【Search In Rotated Sorted Array】cpp
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- leetcode 【 Remove Duplicates from Sorted List 】 python 实现
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
随机推荐
- 【Shell脚本学习25】Shell文件包含
像其他语言一样,Shell 也可以包含外部脚本,将外部脚本的内容合并到当前脚本. Shell 中包含脚本可以使用: . filename 或 source filename 两种方式的效果相同,简单起 ...
- 谷歌chrome://chrome-urls/
查看DNS解析时间 1 chrome://dns/ 查看DNS解析的地址 1 chrome://net-internals/#dns 更多功能请参考 1 chrome://chrome-urls/ 以 ...
- PHP snippets
Friendly file size string public static function bytesToSize($bytes) { if ($bytes < 1024) { retur ...
- DELL R730安装ESXI虚拟化
dell安装esxi需要dell官方提供的镜像文件地址:http://www.dell.com/support/article/us/en/04/SLN290857/dell%E5%AE%9A%E5% ...
- Python爬虫实战:爬糗事百科的段子
一个偶然的机会接触了Python,感觉很好用,但是一直在看c++啥的,也没系统学习.用过之后也荒废了许久.之前想建个公众号自动爬糗事百科的段子,但是没能建起来,真是尴尬,代码上传的服务器上之后,不能正 ...
- 让SAP云平台上的Web应用使用destination服务
首先在SAP云平台里创建一个destination,维护service的end point: 然后打开SAP云平台的WebIDE,创建一个新的文件夹和新的HTML5 Application Descr ...
- netcat 详解
简介 netcat 是一款调试 TCP/UDP 网络连接的利器,常被称作网络调试的瑞士军刀,可见其功能强大. netcat 在 Linux, Windows 等各大操作系统上都有对应等发行版,以下以 ...
- 【BZOJ3668】[NOI2014] 起床困难综合症(位运算思想)
点此看题面 大致题意: 给定一些位运算操作,让你在\(0\sim m\)范围内选一个初始值,使其在经过这些运算后得到的结果最大. 前置技能:关于位运算 作为一道位运算的题,如果你不知道什么是位运算,那 ...
- Python pep8代码规范
title: Python pep8代码规范 tags: Python --- 介绍(Introduction) 官方文档:PEP 8 -- Style Guide for Python Code 很 ...
- PHP开发框架流行度排名:Laravel居首
摘要:在PHP开发中,选择合适的框架有助于加快软件开发,节约宝贵的项目时间,让开发者专注于功能的实现上.Sitepoint网站做了一个小的调查,结果显示最流行的PHP框架前三甲为:Laravel.Ph ...