【LEETCODE】26-Remove Duplicates from Sorted Array
2015-12-16 23:26
627 查看
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.
参考: http://m.blog.csdn.net/blog/xiaolewennofollow/45168279
题意:
一个有序数组,保持顺序挑出无重复的数字放在数组前段,返回新的长度
思路:
i向前移动,一遇到与keyvalue不同的值就:keyvalue更新,位置start与i上的值交换,start向前移动一位
start相当于新的无重复数组的终点
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.
参考: http://m.blog.csdn.net/blog/xiaolewennofollow/45168279
题意:
一个有序数组,保持顺序挑出无重复的数字放在数组前段,返回新的长度
思路:
i向前移动,一遇到与keyvalue不同的值就:keyvalue更新,位置start与i上的值交换,start向前移动一位
start相当于新的无重复数组的终点
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if nums==[]: return 0 n=len(nums) keyvalue=nums[0] start=1 for i in range(n): if nums[i]!=keyvalue: keyvalue=nums[i] nums[start]=nums[i] start+=1 return start
相关文章推荐
- css
- 关于动画培养灵感的网站
- HTML5 indexedDB数据库二之创建索引
- Python 中使用 string-escape 将带转义的字节码字符串转换为 utf-8 字符串
- 输出任意行列的乘法口诀表
- linux C++/C 动态链接库使用
- 带权有向图
- 17、osg::Camera,osg::LOD,osgSim::Impostor类函数说明
- SqlLocalDB2014使用笔记
- [bigdata] hadoop 参数配置解析
- 2.1---删除链表中重复元素(CC150)
- Unity UGUI RectTransform图解
- CentOS下安装Gitlab
- 实战c++中的vector系列--可怕的迭代器失效之二(删除vector中元素)
- 用户及权限管理
- 实战c++中的vector系列--可怕的迭代器失效之二(删除vector中元素)
- 实现strcpy , strncpy, memcpy
- 图的基本操作及BFS与DFS的实现以及飞机换乘问题
- C语言点滴
- Eclipse 的快捷键以及文档注释、多行注释的快捷键