Remove Duplicates from Sorted Array
来源:互联网 发布:自助点餐软件 编辑:程序博客网 时间:2024/06/05 16:35
【题目描述】
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.
水题,要注意的题目给出的output仍然是vector。
【代码】
class Solution {public: int removeDuplicates(vector<int>& nums) { int n=nums.size(); if(n==1||n==0) return n; int tmp=nums[0]; int ans=0; for(int i=1;i<n;i++){ if(nums[i]!=tmp){ ans++; tmp=nums[i]; nums[ans]=tmp; } } nums.resize(ans+1); return ans+1; }};
0 0
- 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
- 汽车租赁管理系统及所涉及面向对象的一般步骤
- 苹果的玫瑰金与小米的粉色版
- 位运算及其应用详解
- [Leetcode]Closest Binary Search Tree Value
- 数据挖掘(8):朴素贝叶斯分类算法原理与实践
- Remove Duplicates from Sorted Array
- CSS小贴士(垂直居中和百分比下调整盒模型填充)
- 数据挖掘(9):BP神经网络算法与实践
- tablebview滚动到顶部
- 通过串口终端操作开发板
- 个人学习笔记---linux内核同步
- 查询表上的约束
- hdu 5014 贪心
- Remove Element