(LeetCode)Remove Duplicates from Sorted Array --- 有序列表去重
2016-08-15 10:02
525 查看
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 =
Your function should return length =
It doesn't matter what you leave beyond the new length.
Subscribe to see which companies asked this question
解题分析:
此处题目简单,去掉重复的数据,然后返回去重后的长度。
# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
def removeDuplicates(self, nums):
L = len(nums)
i = 0
while i < L - 1:
if nums[i] == nums[i + 1]:
del nums[i + 1]
L -= 1
else:
i += 1
return L
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.
Subscribe to see which companies asked this question
解题分析:
此处题目简单,去掉重复的数据,然后返回去重后的长度。
# -*- coding:utf-8 -*-
__author__ = 'jiuzhang'
class Solution(object):
def removeDuplicates(self, nums):
L = len(nums)
i = 0
while i < L - 1:
if nums[i] == nums[i + 1]:
del nums[i + 1]
L -= 1
else:
i += 1
return L
相关文章推荐
- LeetCode 26. Remove Duplicates from Sorted Array(移除有序数组的重复元素)
- LeetCode-Remove Duplicates from Sorted Array-从有序数组移除重复-简单逻辑
- 【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)
- Remove Duplicates from Sorted Array II 从有序数组里移除重复出现元素,最多重复出现2次 @LeetCode
- leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array
- 27.[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
- LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)
- ※ Leetcode - Array -Remove Duplicates from Sorted Array(就地有序数组去重)
- LeetCode | Remove Duplicates from Sorted Array(删除有序数组的重复元素)
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
- [LeetCode] Remove Duplicates from Sorted Array 从有序数组中删除重复项
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
- leetcode26 Remove Duplicates from Sorted Array 去重已排序列表
- [C++]LeetCode 26: Remove Duplicates from Sorted Array(有序数组去重)
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
- [leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted Array
- [Leetcode 1] 26 Remove Duplicates from Sorted Array