Q5: 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].
解决原理:
(若输入数组无序,则先对数组进行排序,则相同值相邻)
两个游标len,i
0~len是元素不重复的子数组,即len是目标子数组的最后一个元素的索引
游标i遍历数组
若A[i]!=A[len],则为目标子数组增添一个元素
代码:
class Solution {
public:
int removeDuplicates(int A[], int n) {
int len = ;
if(n == ) return ;
//sort(A,A+n);
for(int i = ; i < n; i++){
if(A[i] != A[len]){
len++;
A[len] = A[i];
}
}
return len+;
}
};
Q5: Remove Duplicates from Sorted Array的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
随机推荐
- UIWebView stringByEvaluatingJavaScriptFromString的使用方法
stringByEvaluatingJavaScriptFromString 使用stringByEvaluatingJavaScriptFromString方法,需要等UIWebView中的页面加载 ...
- MySQL创建数据表
* 创建数据表 * * * 一.什么是数据表 * * * * 二.创建数据表的SQL语句模型 * * DDL * * ...
- iOS GET、POST数据解析
在实际开发中,JSON数据解析更简单易行,一般均使用json数据解析,因此,程序猿们请务必和后台搞好关系,让他给你json数据. XML解析: ios SDK提供了NSXMLParser和lib ...
- [转]<Unity3D>Unity3D的四种坐标系
http://blog.csdn.net/zuoyamin/article/details/8813424 World Space(世界坐标):我们在场景中添加物体(如:Cube),他们都是以世界坐标 ...
- magento currency magento头部增加币种切换选择
magento currency magento头部增加币种切换选择 默认magento 货币选择切换是显示在左边 有时候我们需要让其显示在头部 Step 1. Create a new file a ...
- 【代码分享】简单html5足球射门游戏分享
之前空余时间想玩玩html5, 于是使用2.2.2的cocos2d-html5 制作了个简单的足球射门游戏 ,美术是自己在纸上画完用手机拍下再ps扣的图,哈哈,赞一下自己的创意. 在我的主页可以玩这个 ...
- sphinx搜索引擎架构图
- jq中如何阻止元素的默认行为?
阻止网页元素的默认行为: event.preventDefault(); 或者:return false;
- js部分---数组及练习题;
数据存储--数组: 强类型语言数组 1.同一类型的数据存储的集合,在内存中是连续的 2.定义的时候需要制定长度 弱类型语言数组 1.可以存储任意类型的数据 2.在内存中不连续,不需要制定长度 定义一个 ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...