DW&LeetCode_day6(43、46、53)

DW&LeetCode_day6(43、46、53)


Write in front:

  • It's the sixth day, let's start cheering today!

Open source content

Open source content


table of Contents

DW&LeetCode_day6(43、46、53)

Write in front:

Open source content

table of Contents

Study outline 

43. String multiplication

answer:

 46. ​​Full arrangement

answer:

 53. Maximum suborder sum


 


Study outline 


43. String multiplication

Given two non-negative integers num1 and num2 represented in string form, return the product of num1 and num2, and their product is also represented in string form.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"


Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"


Description:

The length of num1 and num2 is less than 110.
num1 and num2 only contain the numbers 0-9.
Neither num1 nor num2 start with a zero, unless it is the number 0 itself.
You cannot use any large number types of the standard library (such as BigInteger) or directly convert the input to an integer for processing.


Link: topic link

answer:

# 转化为整数
class Solution:
    def multiply(self, num1: str, num2: str) -> str:
        return str(int(num1) * int(num2))

 46. ​​Full arrangement

Given a sequence without repeated numbers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2 ],
  [3,2,1]
]

 
Link: topic link

answer:

itertoolsProvides very useful functions for manipulating iteration objects.

>>> import itertools
>>> natuals = itertools.count(1)
>>> for n in natuals:
...     print(n)
...
1
2
3
...
# itertools 
import itertools  
class Solution:
    def permute(self, a: List[int]) -> List[List[int]]:
        if not a: return []
        return [list(i) for i in permutations(a, len(a))]

 53. Maximum suborder 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 the largest sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The sum of consecutive sub-arrays [4,-1,2,1] is the largest, which is 6.


Advanced:

If you have already implemented a solution with O(n) complexity, try to use a more subtle divide-and-conquer solution.

Link: topic link

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)

 

Guess you like

Origin blog.csdn.net/adminkeys/article/details/112749291