Getting started with brushing questions 20201112

Use leetcode to brush questions

python3 version

I have never understood how to write the answer in leetcode, because there is a classsolution on the right, and I don't know what it should be written. like this:

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:

I can barely understand the previous problem. A solution class defines a method twoSum, but it is not clear which arrow to the right of the arrow at the back means.

Now I searched it and found out that it is, in fact, something similar to a comment that tells you the return value type.
The following code is written normally, as if it is in a function, implemented in the function, and finally returned to a salvation that satisfies the return value type.

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        n = len(nums)
        for i in range(n):
            for j in range(i + 1, n):
                if nums[i] + nums[j] == target:
                    return [i, j]
        
        return []

Note that there are points to note here: do it yourself in the function, just write it as normal. The return of the return. But I don’t know why he has two retuns

Guess you like

Origin blog.csdn.net/weixin_45211473/article/details/109639973