【LEETCODE】26-Remove Duplicates from Sorted Array
来源:互联网 发布:阿迪达斯网络授权书 编辑:程序博客网 时间:2024/06/01 15:13
Given a sorted array, remove the duplicates in place such that each element appear onlyonce 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 ofnums 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
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
- 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
- Peek and Pop新功能展示
- 3GPP 36211-c70-9
- Python 中使用 string-escape 将带转义的字节码字符串转换为 utf-8 字符串
- HTML5 indexedDB数据库二之创建索引
- 带权有向图
- 【LEETCODE】26-Remove Duplicates from Sorted Array
- Android: JNI动态注册
- Android 事件分发机制View篇
- Windows10下JDK、eclipse与SDK及ADT的配置
- 关于TCP的滑动窗口和拥塞控制
- Android广播机制-BroadcastReceiver
- OCP课程之ORACLE审计
- 【jQuery】:visible过滤选择器
- Linux之hello驱动编写