[Week 3] [Leetcode] [Array] Remove Duplicates from Sorted Array
来源:互联网 发布:手机怎么连接网络电视 编辑:程序博客网 时间:2024/06/03 09:10
- Question:
Given a sorted array, remove the duplicates in place such that each element appear onlyonce 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 being1
and2
respectively. It doesn't matter what you leave beyond the new length. - Analysis:
目标:根据题目了解到需要去除数组中重复的元素,并且整个运算过程不重新分配内存。
条件分析:首先这是一个已经排好序的数组,所以首先判断数组是否为空,如为空则直接返回0.其次,当数组不为空时,
- Code:
class Solution {public: int removeDuplicates(vector<int>& nums) { //考虑数组为空数组的情况,返回0 if (nums.empty()) return 0; int index = 0; for (int i = 1;i<nums.size();i++) { if (nums[index] != nums[i]) nums[++index] = nums[i]; } return index+1; }};
阅读全文
0 0
- [Week 3] [Leetcode] [Array] Remove Duplicates from Sorted Array
- leetcode(3)remove-duplicates-from-sorted-array
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- leetcode 35: Remove Duplicates from Sorted Array
- [LeetCode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array
- Leetcode: Remove Duplicates from Sorted Array
- [leetcode] Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array
- [LeetCode]Remove Duplicates from Sorted Array II
- pexpect库学习之ssh专用类详解
- Ubuntu下Eclipse的安装方法(图文详解)
- 使用 UMEditor-jsp 图片上传失败解决方法
- android监听SD卡状态
- 电商产品评论数据情感分析代码详解
- [Week 3] [Leetcode] [Array] Remove Duplicates from Sorted Array
- Apache Zeppelin 中 Cassandra CQL 解释器
- 【算法作业17】LeetCode 303. Range Sum Query
- Idea中Git的使用和两种类型的冲突解决
- 使用决策树预测隐形眼镜类型
- SVN分支合并的原理及操作
- eclipse启动卡死解决方法
- 【poj 1003】Hangover 题解&代码(c++)
- 更新tensorflow到1.2版本