LeetCode Tencent Featured Exercise 50-Day 6

Question 53: Maximum sub-order sum
Given an integer array nums, find a continuous sub-array with the largest sum (the sub-array contains at least one element), and return its largest sum.
answer:

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        for i in range(1,len(nums)):
            nums[i] = nums[i] + max(nums[i-1],0)
        return max(nums)

In the for loop, from the second item to the last item, each item is equal to the sum of the item plus the previous item or 0, so the maximum continuous sum is the maximum value in the new array.
Operation result:
Insert picture description here
Question 43 : String multiplication
Given two non-negative integers num1 and num2 expressed in string form, return the product of num1 and num2, and their product is also expressed in string form.
answer:

class Solution:
    def multiply(self, num1: str, num2: str) -> str:
        res = int(num1)*int(num2)
        return str(res)

Running result:
Insert picture description here
Question 46: All permutations
Given a sequence without repeated numbers, return all possible permutations.
answer:

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        from itertools import permutations
        if not nums: return []
        return [list(i) for i in permutations(nums,len(nums))]

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44315884/article/details/112758244