最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)刪除排序數(shù)組中重復(fù)項(xiàng)的兩種方法示例

 更新時(shí)間:2019年01月31日 09:54:12   作者:求兵  
這篇文章主要介紹了Python實(shí)現(xiàn)刪除排序數(shù)組中重復(fù)項(xiàng)的兩種方法,涉及Python數(shù)組元素的遍歷、判斷、刪除等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)刪除排序數(shù)組中重復(fù)項(xiàng)的兩種方法。分享給大家供大家參考,具體如下:

對(duì)于給定的有序數(shù)組nums,移除數(shù)組中存在的重復(fù)數(shù)字,確保每個(gè)數(shù)字只出現(xiàn)一次并返回新數(shù)組的長(zhǎng)度

注意:不能為新數(shù)組申請(qǐng)額外的空間,只允許申請(qǐng)O(1)的額外空間修改輸入數(shù)組

Example 1:

Given 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

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.

說(shuō)明:為什么返回列表長(zhǎng)度而不用返回列表?因?yàn)榱斜韨魅牒瘮?shù)是以引用的方式傳遞的,函數(shù)中對(duì)列表進(jìn)行的修改會(huì)被保留。

 // nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
  print(nums[i]);
}

1. 簡(jiǎn)單判斷列表中元素是否相等,相等就刪除多余元素

def removeDuplicates(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    if not nums:
      return 0
    if len(nums)==1:  #單獨(dú)判斷列表長(zhǎng)度為1的情況,因?yàn)橹蟮膄or循環(huán)從下標(biāo)1開(kāi)始
      return 1
    temp_num = nums[0]
    count =0     #for循環(huán)中動(dòng)態(tài)刪除列表元素,列表縮短,為了防止下標(biāo)溢出需要用count標(biāo)記刪除元素個(gè)數(shù)
    for index, num in enumerate(nums[1:]):
      if temp_num == num:   #元素相等就刪除
        del nums[index-count]
        count += 1
      else:
        temp_num = num
    return len(nums)
def removeDuplicates(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    forth = 0
    back = 1
    while back <= len(nums)-1:
      if nums[forth] == nums[back]:
        nums.pop(back)
      else:
        forth += 1
        back += 1
    return len(nums)

2. 修改數(shù)組,保證數(shù)組的前幾個(gè)數(shù)字互不相同,且這幾個(gè)數(shù)字的長(zhǎng)度同返回長(zhǎng)度相等

def removeDuplicates(self, nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    if not nums:
      return 0
    length = 0   #不存在重復(fù)數(shù)字的數(shù)組長(zhǎng)度
    for index in range(1,len(nums)):   #遍歷數(shù)組
      if nums[index] != nums[length]:
        length += 1
        nums[length] = nums[index]
    return length+1

算法題來(lái)自:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/

PS:本站還有兩款比較簡(jiǎn)單實(shí)用的在線文本去重復(fù)工具,推薦給大家使用:

在線去除重復(fù)項(xiàng)工具:
http://tools.jb51.net/code/quchong

在線文本去重復(fù)工具:
http://tools.jb51.net/aideddesign/txt_quchong

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python字典操作技巧匯總》、《Python字符串操作技巧匯總》、《Python常用遍歷技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

来凤县| 仙桃市| 岫岩| 自贡市| 沅陵县| 海阳市| 连城县| 湖州市| 沙湾县| 信宜市| 城市| 玉龙| 高尔夫| 江都市| 香河县| 桃源县| 株洲市| 郯城县| 昭苏县| 红河县| 竹山县| 若羌县| 武冈市| 宁陵县| 巴林右旗| 莒南县| 长岛县| 吴旗县| 金堂县| 西宁市| 宣武区| 丰宁| 东光县| 西安市| 白朗县| 万山特区| 三河市| 通化市| 沧州市| 边坝县| 团风县|