#100 Remove Duplicates from Sorted Array
来源:互联网 发布:数组指针和指针数组 编辑:程序博客网 时间:2024/05/16 10:00
题目描述:
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.
Example
题目思路:Given input array A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
这题还是用two pointers,一个i去遍历,一个idx守在原地。如果A[i] == A[idx],就不取A[i];否则,A[idx + 1] = A[i]。
Mycode(AC = 42ms):
class Solution {public: /** * @param A: a list of integers * @return : return an integer */ int removeDuplicates(vector<int> &nums) { // write your code here if (nums.size() <= 1) return nums.size(); int idx = 0; for (int i = 1; i < nums.size(); i++) { if (nums[i] != nums[idx]) { nums[idx + 1] = nums[i]; idx++; } } return idx + 1; }};
0 0
- #100 Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- 如何用两个栈形成一个队列
- lua中的table简单应用
- Objective-C: Foundation——NSArray、NSMutableArray
- Android中给Listview的HeadView加ViewPager自动轮播图,解决滑动冲突
- Spring 加载xml配置文件路径问题
- #100 Remove Duplicates from Sorted Array
- EasyUI容易被js脚本攻击的基本处理
- 简单实用的进度条加载组件loader.js
- VC中的双缓冲绘图技术
- mysql中间件Atlas安装
- Effective Modern C++ 条款19 用std::shared_ptr管理共享所有权的资源
- 5.Chrome开发者工具不完全指南:(三、性能篇)
- 桌子放硬币
- html5 autofocus属性