【Leetcode】之Remove Duplicates from Sorted Array
2015-11-19 20:04
281 查看
一.问题描述
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
1and
2respectively.It doesn't matter what you leave beyond the new length.
二.我的解题思路
题目要求去除重复的字符,我采取的办法是设立两个指针,一个指向当前重复字符的第一个,一个指向当前在遍历的字符。测试通过的程序如下:class Solution {public:int removeDuplicates(vector<int>& nums) {int len = nums.size();int cnt=1;int st=0;if(len<2) return len;for(int i=1;i<len;i++){if(nums[i]!=nums[i-1]){cnt++;if(st<i-1){nums[st+1]=nums[i];}st++;}}return cnt;}};
相关文章推荐
- iOS---限制文本框只能输入数字
- Maven仓库管理-Nexus
- Intent的类型和作用
- nyoj--1023--还是回文(动态规划)
- java实现使用url传递paramString和paramStream
- [判断输入的字符串是不是纯数字
- nyoj--1023--还是回文(动态规划)
- APP硬件环境测试
- memcached相关
- poj 3660 传递性关系 floyd更新
- android studio 运行 Java Application
- 创业公司 CEO 们是如何识别人才的?怎样看学历、能力和经验等条件?
- 【leetcode】Single Number III
- HTTP/1.1 新建会话失败
- 编程数学
- HDOJ 2068 RPG的错排 【错排 组合数】
- while循环下try.....catch的运用
- 反弹shell的十种姿势
- 当dex分包遇上NoClassDefFoundError&ClassNotFoundException
- 栈区,堆区,全局区:内存结构图 http://blog.csdn.net/xlf13872135090/article/details/8828704