LeetCode OJ--Remove Duplicates from Sorted Array
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/
删除数组中的重复元素,要求为原地算法。
进行一遍遍历,记录下一个可用位置,也就是用来存储不重复元素的位置。
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n == )
return ;
if(n == )
return ;
int preOne = A[];
int nextAvaliablePosition = ;
for(int i = ;i<=n-;i++)
{
if(A[i] != preOne)
{
preOne = A[i];
A[nextAvaliablePosition] = A[i];
nextAvaliablePosition ++;
}
}
return nextAvaliablePosition;
}
};
LeetCode OJ--Remove Duplicates from Sorted Array的更多相关文章
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
随机推荐
- GoogleTest 之路3-Mocking Framework
当你写一个原型或者测试的时候,依赖整个object 是不可行和明智的.一个 mock object和 real object 有同样的接口(所以它可以像同一个使用),但是让你在运行时进行指定它应该如何 ...
- Python9-网络编程4-day33
解决黏包问题: 在传输大量数据之前,先告诉接收端要发送数据大小 如果想更漂亮的解决问题,可以通过struct模块来定制协议为什么会出现黏包现象: 首先只有在tcp协议中才会出现黏包现象 是因为tcp协 ...
- perl-basic-分支&循环
if elsif shorter if: if+condition放在句子尾部. use strict; use warnings; my $word = "antidisestablish ...
- HDU - 1251 统计难题(Trie树)
有很多单词(只有小写字母组成,不会有重复的单词出现) 要统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 每个单词长度不会超过10. Trie树的模板题.这个题内存把控不好容易MLE. ...
- T-SQL中的indexof函数
在C#字符串中查找字符有indexof方法,那么在T-SQL与之相对应的是CHARINDEX方法,其语法为CHARINDEX(要查找的字符,字符串),返回一个数字. CHARINDEX(',','aa ...
- 大数据学习——scala的wordCount小例子
val lines=List("hello tom hello jerry","hello tom hello kitty hello china") //方法 ...
- TCP 中的三次握手和四次挥手
Table of Contents 前言 数据报头部 三次握手 SYN 攻击 四次挥手 半连接 TIME_WAIT 结语 参考链接 前言 TCP 中的三次握手和四次挥手应该是非常著名的两个问题了,一方 ...
- JS使用onerror进行默认图像显示,可代替alt
JS代码 //图像加载出错时的处理 function errorImg(img) { img.src = "默认图片.jpg"; img.onerror = null; } HTM ...
- 51nod1832 先序遍历与后序遍历
对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的二叉树.两棵二叉树不同当且仅当对于某个x,x的左儿子编号不同或x的右儿子编号不同. Input 第一行一个正整数n(3<=n< ...
- PHP变量的生命周期
变量不仅有其特定的作用范围,还有其存活的周期--生命周期.变量的生命周期指的是变量可被使用的一个时间段,在这个时间段内变量是有效的,一旦超出这个时间段变量就会失效,我们就不能够再访问到该变量的值了. ...