LeeCode初級演算法的Python實現--數組

來源:互聯網
上載者:User

標籤:reverse   nta   set   LIDS   dup   排列   turn   ali   its   

LeeCode初級演算法的Python實現--數組
# -*- coding: utf-8 -*-"""@Created on 2018/6/3 17:06@author: ZhifengFang"""# 排列數組重複資料刪除項def removeDuplicates(nums):    if len(nums) <= 1:        return len(nums)    i = 1    while len(nums) != i:        if nums[i] == nums[i - 1]:            del nums[i]            i -= 1        i += 1    return len(nums)# 買賣股票最佳時機2def maxProfit(prices):    max = 0    if len(prices) <= 1:        return 0    for i in range(len(prices) - 1):        if prices[i] < prices[i + 1]:            max += prices[i + 1] - prices[i]    return max# 旋轉數組def rotate(nums, k):    # nums = nums[-k:] + nums[:k + 1]    # print(nums)    if len(nums) > 1:        k = k % len(nums)        if k != 0:            temp = nums[-k:]            nums[k:] = nums[:len(nums) - k]            nums[0:k] = temp    print(nums)# 判斷數組中是否有重複元素def containsDuplicate(nums):    # if len(nums)>len(set(nums)):    #     return True    # return False    for num in nums:        if nums.count(num) > 1:            return True    return False# 獲得裡面只出現一次的數字def singleNumber(nums):    numCounts = {}    result = []    for num in nums:        numCounts[num] = numCounts.get(num, 0) + 1    for key in numCounts.keys():        if numCounts.get(key) == 1:            result.append(key)            break    return result[0]# 兩個數組的交集 IIdef intersect(nums1, nums2):    if len(nums2) < len(nums1):        nums1, nums2 = nums2, nums1    newNums = []    i = 0    while i < len(nums1):        j = 0        while j < len(nums2):            if nums1[i] == nums2[j]:                newNums.append(nums2[j])                del nums1[i], nums2[j]                i -= 1                j -= 1                break            j += 1        i += 1    return newNums# print(intersect([9],[7,8,3,9,0,0,9,1,5]))# 加1def plusOne(digits):    strDigits = ‘‘    for example in digits:        strDigits += str(example)    strDigits = int(strDigits) + 1    listDigits = [int(str) for str in str(strDigits)]    return listDigits# print(plusOne([1, 2, 3]))# 移動0def moveZeroes(nums):    # for i in range(len(nums)):    i = 0    zeroesCount = 0    while i + zeroesCount < len(nums):        if nums[i] == 0:            nums[i:] = nums[i + 1:] + [0]            i -= 1            zeroesCount += 1        i += 1    return nums# 兩數和def twoSum(nums, target):    d = {}    for x in range(len(nums)):        a = target - nums[x]        if nums[x] in d:            return d[nums[x]], x        else:            d[a] = xnums = [3, 2, 4]target = 6# print(twoSum(nums, target))def isXT(strs):    strSet = set(strs)    for s in strSet:        if s != ".":            if strs.count(s) > 1:                return False    return True# 有效數獨def isValidSudoku(board):    for i in range(9):        boardLie = [example[i] for example in board]        key1 = int(i / 3) * 3 + 1        key2 = 1 + (i % 3) * 3        boardGe = [board[key1 - 1][key2 - 1], board[key1 - 1][key2], board[key1 - 1][key2 + 1],                   board[key1][key2 - 1], board[key1][key2], board[key1][key2 + 1],                   board[key1 + 1][key2 - 1], board[key1 + 1][key2], board[key1 + 1][key2 + 1]]        if isXT(board[i]) == False:            return False        if isXT(boardLie) == False:            return False        if isXT(boardGe) == False:            return False    return Trueboard = [[".", ".", "4", ".", ".", ".", "6", "3", "."],         [".", ".", ".", ".", ".", ".", ".", ".", "."],         ["5", ".", ".", ".", ".", ".", ".", "9", "."],         [".", ".", ".", "5", "6", ".", ".", ".", "."],         ["4", ".", "3", ".", ".", ".", ".", ".", "1"],         [".", ".", ".", "7", ".", ".", ".", ".", "."],         [".", ".", ".", "5", ".", ".", ".", ".", "."],         [".", ".", ".", ".", ".", ".", ".", ".", "."],         [".", ".", ".", ".", ".", ".", ".", ".", "."]]# print(isValidSudoku(board))# 旋轉映像def rotate(matrix):    for i in range(len(matrix)):        for j in range(i+1,len(matrix)):            matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]        matrix[i].reverse()    print(matrix)ma = [    [1, 2, 3],    [4, 5, 6],    [7, 8, 9]]rotate(ma)

LeeCode初級演算法的Python實現--數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.