首页 > 其他分享 >leetcode

leetcode

时间:2023-05-31 10:25:11浏览次数:40  
标签:compare nums functools key new 排序 leetcode

1 python 常用函数

1.1 排序函数

原地排序 nums.sort()
不改变原列表有返回值 new = sorted(nums)

import functools
# 一维数组排序
nums = [2, 1, 3, 4, 5]


def compare_udf(x, y):
    # x - y 升序
    # y - x 降序
    return x - y 



# # python2
nums.sort(cmp=compare_udf)
# # python3
nums.sort(key=functools.cmp_to_key(compare_udf))
print(nums)
nums = [(264.0, 8, 0), (199.0, 10, 1), (230.0, 10, 2), (199.0, 9, 3)]
# 基础排序 按照一维升序排列,按照二维降序排列
new = sorted(nums, key=lambda x: (x[0], -x[1]))
print(new)

# 自定义排序
import functools


def comp(x, y):
    if x[0] != y[0]:
        return x[0] - y[0]
    else:
        return y[1] - x[1]


new = sorted(nums, key=functools.cmp_to_key(comp))
print(new)

标签:compare,nums,functools,key,new,排序,leetcode
From: https://www.cnblogs.com/liuyechang/p/17445299.html

相关文章

  • leetcode 693. Binary Number with Alternating Bits
    Givenapositiveinteger,checkwhetherithasalternatingbits:namely,iftwoadjacentbitswillalwayshavedifferentvalues.Example1:Input:5Output:TrueExplanation:Thebinaryrepresentationof5is:101Example2:Input:7Output:FalseExplanation......
  • leetcode 637. Average of Levels in Binary Tree
    Givenanon-emptybinarytree,returntheaveragevalueofthenodesoneachlevelintheformofanarray.Example1:Input:3/\920/\157Output:[3,14.5,11]Explanation:Theaveragevalueofnodesonlevel0is3,onlevel......
  • leetcode 496. Next Greater Element I
    Youaregiventwoarrays(withoutduplicates)nums1andnums2wherenums1’selementsaresubsetofnums2.Findallthenextgreaternumbersfornums1'selementsinthecorrespondingplacesofnums2.TheNextGreaterNumberofanumberxinnums1isth......
  • leetcode 463. Island Perimeter
    Youaregivenamapinformofatwo-dimensionalintegergridwhere1representslandand0representswater.Gridcellsareconnectedhorizontally/vertically(notdiagonally).Thegridiscompletelysurroundedbywater,andthereisexactlyoneisland(i......
  • leetcode 682. Baseball Game
    You'renowabaseballgamepointrecorder.Givenalistofstrings,eachstringcanbeoneofthe4followingtypes:Integer(oneround'sscore):Directlyrepresentsthenumberofpointsyougetinthisround."+"(oneround'sscor......
  • leetcode 566. Reshape the Matrix
    InMATLAB,thereisaveryusefulfunctioncalled'reshape',whichcanreshapeamatrixintoanewonewithdifferentsizebutkeepitsoriginaldata.You'regivenamatrixrepresentedbyatwo-dimensionalarray,andtwopositiveintegersr......
  • leetcode 766. Toeplitz Matrix
    AmatrixisToeplitzifeverydiagonalfromtop-lefttobottom-righthasthesameelement.NowgivenanMxNmatrix,return True ifandonlyifthematrixisToeplitz. Example1:Input:matrix=[[1,2,3,4],[5,1,2,3],[9,5,1,2]]Output:TrueExplanation:12......
  • leetcode 575. Distribute Candies
    Givenanintegerarraywithevenlength,wheredifferentnumbersinthisarrayrepresentdifferentkindsofcandies.Eachnumbermeansonecandyofthecorrespondingkind.Youneedtodistributethesecandiesequallyinnumbertobrotherandsister.Retur......
  • leetcode 412. Fizz Buzz
    Writeaprogramthatoutputsthestringrepresentationofnumbersfrom1ton.Butformultiplesofthreeitshouldoutput“Fizz”insteadofthenumberandforthemultiplesoffiveoutput“Buzz”.Fornumberswhicharemultiplesofboththreeandfiveoutp......
  • leetcode 669. Trim a Binary Search Tree
    GivenabinarysearchtreeandthelowestandhighestboundariesasLandR,trimthetreesothatallitselementsliesin[L,R](R>=L).Youmightneedtochangetherootofthetree,sotheresultshouldreturnthenewrootofthetrimmedbinarys......