topic:
2679.In the matrix and the harmony.md
Thought:
One -line
First of all, the meaning is to find the largest number of each sub -list,Thenpopgo out,Finally ask for peace。
The effect of traversing again and again is too bad,So I thought of using itzip一次性Traversal多个子Array。
于yes先对每个子ArraySort,Then usezipTraversal,Find the maximum。
for example:nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
Sort后:nums = [[1,2,7],[2,4,6],[3,5,6],[1,2,3]]
Then usezipTraversal得到:[(1,2,3,1),(2,4,5,2),(7,6,6,3)]
Find the maximum:[3,5,7]
Context:15
Code:
1 | class Solution: |
*The role and the rolezip()explain
1 | nums = [[1,2,7],[2,4,6],[3,5,6],[1,2,3]] |
zip()
The function corresponds to one -to -one elements in multiple lists,Then return onezipObject,Can uselist()Function convert to list。
1 | for i in zip(num1, num2, num3, num4): |
*nums
The role ispythonNot a pointer,InsteadnumsEach element in the parameter is passed into the function。I understand here as a list。
1 |
|
zip(*nums)
WillnumsEach element is passed in as a parameterzip()In the function,Then return onezipObject,Can uselist()Function convert to list。zip(*nums)
Equivalent tozip(num1, num2, num3, num4)
,innum1, num2, num3, num4yesnumsElement。
1 | for i in zip(*nums): |