[LeetCode]题解(python):026-Remove Duplicates from Sorted Array

题目来源:

  https://leetcode.com/problems/remove-duplicates-from-sorted-array/



题意分析:

  给定一个排好序的数组,去除重复的数,返回新数组的长度,不能申请额外的空间,超过新数组长度部分是什么数都无所谓。



题目思路:

  这是一个很简单的题目,由于给定的数组已经排序,那么用i,j两个下标,i记录新数组的下标,j是原来数组下标,如果nums[j] != nums[j - 1],那么nums[i] = nums[j],i 和j 都+ 1。最后返回i。



代码(python):

  

 1 class Solution(object):
 2     def removeDuplicates(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: int
 6         """
 7         i = 1
 8         j = 1
 9         size = len(nums)
10         while j < size:
11             if nums[j] == nums[i - 1]:
12                 j += 1
13             else:
14                 nums[i] = nums[j]
15                 i += 1
16                 j += 1
17         return min(i,size)



转载请注明出处:http://www.cnblogs.com/chruny/p/4885113.html

时间: 2024-10-27 02:58:57

[LeetCode]题解(python):026-Remove Duplicates from Sorted Array的相关文章

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

LeetCode 026 Remove Duplicates from Sorted Array

题目描述:Remove Duplicates from Sorted Array 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 me

LeetCode(2) Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array 题目 考察数组 描述 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 const

[Leetcode][Python]26: Remove Duplicates from Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 26: Remove Duplicates from Sorted Arrayhttps://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear

【LeetCode】026. Remove Duplicates from Sorted Array

题目: 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 n

Java for LeetCode 026 Remove Duplicates from Sorted Array

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

LeetCode(26) Remove Duplicates from Sorted Array

题目 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 n

leetcode第26题--Remove Duplicates from Sorted Array

problem: 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 ar

026. Remove Duplicates from Sorted Array

1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 if (nums.size() == 0) return 0; 5 int j = 0; 6 for (int i = 1; i < nums.size(); ++i) { 7 if (nums[i] != nums[j]) { 8 nums[++j] = nums[i]; 9 } 10 } 11 return j + 1; 12