leetcode 26. Remove Duplicates from Sorted Array
来源:互联网 发布:用户画像建模算法 编辑:程序博客网 时间:2024/06/06 00:31
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) { int last=0, i, len=nums.size(); if(!len) return 0; for(i=0; i<len;++i) { if(nums[i]!=nums[last]) nums[++last] = nums[i]; } return last+1; }};
class Solution {public: int removeDuplicates(vector<int>& nums) { int count = 0; for(int i = 1; i < nums.size(); i++){ if(nums[i] == nums[i-1]) count++; else nums[i-count] = nums[i]; } return nums.size()-count; }};
阅读全文
0 0
- [LeetCode]26.Remove Duplicates from Sorted Array
- LeetCode 26. Remove Duplicates from Sorted Array
- LeetCode --- 26. Remove Duplicates from Sorted Array
- LeetCode 26.Remove Duplicates from Sorted Array
- [Leetcode] 26. Remove Duplicates from Sorted Array
- 【leetcode】26. Remove Duplicates from Sorted Array
- 【leetcode】 26. Remove Duplicates from Sorted Array
- [leetCode]26. Remove Duplicates from Sorted Array
- LeetCode 26.Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array LeetCode
- 【LeetCode】26. Remove Duplicates from Sorted Array
- [LeetCode]26. Remove Duplicates from Sorted Array
- leetcode 26. Remove Duplicates from Sorted Array
- LeetCode *** 26. Remove Duplicates from Sorted Array
- LeetCode 26. Remove Duplicates from Sorted Array
- LeetCode - 26. Remove Duplicates from Sorted Array
- leetcode-26. Remove Duplicates from Sorted Array
- 26.[Leetcode]Remove Duplicates from Sorted Array
- Python3.6请求网站时报错:http.client.RemoteDisconnected: Remote end closed connection without response
- tensorflow CUDA out of memory
- Servlet生命周期、工作原理
- getpwnam,getgrnam,getpwent,crypt等函数
- python 中文乱码问题深入分析
- leetcode 26. Remove Duplicates from Sorted Array
- home键源码分析
- Jquery 点击图片在弹出层显示大图
- JDBC 连接池封装
- java中的Timer用法(一) 基础知识
- LeetCode OJ 12. Integer to Roman
- MongoDB高级查询[聚合Group]
- Java知识点总结之Java Web 知识(三)
- IllegalStateException: The specified child already has a parent.