[leetcode] Remove Duplicates from Sorted Array
来源:互联网 发布:html a 调用js 编辑:程序博客网 时间:2024/05/24 07:36
From : https://leetcode.com/problems/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 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.
class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size()==0) return 0; for(vector<int>::iterator i=nums.begin(); i!=nums.end()-1; i++) { while(i!=nums.end() && i+1!=nums.end() && *i == *(i+1)) { nums.erase(i+1); } if(i == nums.end()) break; } return nums.size(); }};
public class Solution { public int removeDuplicates(int[] nums) { if(null == nums) { return 0; } int l = nums.length; if(l < 2) { return l; } int kii = nums[0]; int ki = nums[1]; int indice = 2; for(int i=2; i<l; ++i) { int n = nums[i]; if(n != kii) { nums[indice++] = n; } kii = ki; ki = n; } return indice; }}
0 0
- [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
- [leetcode]Remove Duplicates from Sorted Array
- [leetcode]Remove Duplicates from Sorted Array II
- 设计模式C++实现-解释器模式
- 《MYCAT权威指南》与MYCAT开放电商项目
- Android后台发送短信
- Struts2+ExtJS+Ajax 登录操作例子
- FIRST BLOOD
- [leetcode] Remove Duplicates from Sorted Array
- 四种点击事件
- RDIFramework.NET V2.9版本 WinFom部分新增与修正的功能
- Service和BroadcastReceiver总结
- sqlplus中导出数据到excel中
- python动态加载是否影响性能
- 搜狗日志处理程序
- 客户管理系统之模块设计(九)
- mysql命令大全