LeetCode 26 Remove Duplicates from Sorted Array
Problem:
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 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.
Summary:
去除已排序数组中得重复数字,并返回最终数组所含数字个数。
Solution:
用两个指针,指针i遍历数组,指针j标记当前已去重数组的结尾位置,每遍历到一个新数字将其放入j处。
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int len = nums.size();
if (!len) {
return ;
}
int j = ;
for (int i = ; i < len; i++) {
if (nums[i - ] != nums[i]) {
nums[j++] = nums[i];
}
}
return j;
}
};
LeetCode 26 Remove Duplicates from Sorted Array的更多相关文章
- 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] 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] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Java [leetcode 26]Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- Leetcode 26. Remove Duplicates from Sorted Array (easy)
Given a sorted array, remove the duplicates in-place such that each element appear only once and ret ...
- [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 26—Remove Duplicates from Sorted Array
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
随机推荐
- Prototype原型(创建型模式)
依赖关系的倒置:抽象不应该依赖于实现的细节,实现细节应该依赖于抽象. 原型模式的定义 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.prototype模式允许一个对象再创建另外一个可 ...
- 【JavaScript】javascript 方法 test()
个人理解:var b = x.test(y); y是否存在模式x中,返回true或false:x可以是正则,字符串,
- Android Studio 工具插件
1.Android Studio 翻译插件,可以将英文翻译为中文. https://github.com/Skykai521/ECTranslation 2.Android Studio之Androi ...
- Android只能动态注册的广播Action
只能动态注册的广播(部分): android.intent.action.SCREEN_ON android.intent.action.SCREEN_OFF android.intent.actio ...
- nginx 报错 HTTP ERROR 500 (PHP数组简写模式)
同样的代码放在Apache上执行可以执行,在nginx上面就报错了. 百度出来一堆结果貌似都不对,然后只有注释代码->运行程序,一步步找到问题所在 $buffer = []; 这一步报错了 原来 ...
- offsetHeight, clientHeight与scrollHeight的区别
在网上搜了一下,结论非常笼统,讲IE从不讲版本,因此自己做了测试并上传结论.以下结论皆是在标准模式下测试通过的,没有测试quirk模式. clientHeight 大部分浏览器对 clientHe ...
- 从A文件拿B文件的某一个值
- 表格不被内容撑大,且超出的内容变为省略号(css)
今天写代码,发现表格会被内容撑大,影响到了网页的整体布局. 百度了一解决方法,下面是代码和我的备注 table{table-layout: fixed;} //固定表格 table td ...
- python 2.6 与 2.4 区别
class 要把 class config(): 改成 class config: except Exception as e # only works in python 2.4 to 2.7 tr ...
- Windows 7个性化配置,关闭Win7动画效果,设置窗口背景为“ 豆绿色”
减少眼睛疲劳配色(豆绿色): RGB:, , ,颜色名称:#C7EDCC 1.任务栏设置 2.关闭Win7动画效果 控制面板 -> 轻松访问 -> 优化视频显示 3.去掉窗口阴影 右键单击 ...