【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 ...
随机推荐
- 构建第一个Spring Boot2.0应用之Controller(三)
Controller控制器主要是接收浏览器请求.下面说一说@Controller注解和@RestController的区别: (1)@Controller类中的方法可以直接通过返回String跳转到j ...
- Windows系统HTTP身份验证方法
当Windows客户端尝试使用HTTP协议访问基于Web的资源时,会在客户端和服务器之间建立"对话".换句话说,服务器告诉客户端,访问资源之前进行身份验证 ,并且服务器还告诉客户端 ...
- 美国绿卡基础知识:I-539和I-129表格的应用回复新帖
美国绿卡基础知识:I-539和I-129表格的应用 发布于: 2011/07/25 8:43 am 引用 I-539,就是和万金油类似的表格.不管你是要延期,还是转换身份:不管你是 B-2 ...
- 用rem实现h5页面的编写
一 静态页面的布局 将这段代码加到script中 (function(doc, win) { var docEl = doc.documentElement, resizeEvt = 'orienta ...
- [VC]vc中socket编程步骤
sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW): 基于TCP的socket编程是采用的流式套接字.在这个 ...
- IOS 当一个控件被添加到父控件中会调用(didMoveToSuperview)
/** * 当一个控件被添加到父控件中就会调用 */ - (void)didMoveToSuperview { if (self.group.opened) { self.nameView.image ...
- UVA Live Archive 4394 String painter(区间dp)
区间dp,两个str一起考虑很难转移. 看了别人题解以后才知道是做两次dp. dp1.str1最坏情况下和str2完全不相同,相当于从空白串开始刷. 对于一个区间,有两种刷法,一起刷,或者分开来刷. ...
- objective C 内存管理及属性方法具体解释
oc为每一个对象提供一个内部计数器.这个计数器跟踪对象的引用计数,当对象被创建或拷贝时.引用计数为1.每次保持对象时,调用retain接口.引用计数加1.假设不需要这个对象时调用release,引用计 ...
- 关于profile集合
profile集合是mongodb的慢操作日志 > db.getProfilingStatus() { , , } 可以通过getProfilingStatus来查看当前profile设置 pr ...
- Problem D: 双向冒泡排序
Problem D: 双向冒泡排序 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 447 Solved: 197[Submit][Status][We ...