Coding Practice (04/09)

Date: 2021/4/9

Source: Leetcode No.154 寻找旋转排序数组中的最小值 II

Language: Python 3

Coding:

 1 class Solution:
 2     def findMin(self, nums: List[int]) -> int:
 3         n = len(nums)
 4         low, high = 0, n-1
 5         while low < high:
 6             mid = (low + high) // 2 
 7             if nums[mid] > nums[high]:
 8                 low = mid + 1
 9             elif nums[mid] == nums[high]:
10                 # 当nums[mid] = nums[high]时,根据mid的位置确定low和high的取值
11                 k = mid
12                 check = False
13                 while k < high: #改循环用于判断从mid到high之间的所有取值是否都相等
14                     if nums[k] != nums[high]: 
15                     #若mid到high之间有值不等于nums[mid],说明mid位于最小值左侧,chec为True
16                         check = True
17                         break
18                     k += 1
19                 if check:
20                     # 当check为True,应取mid后面的数组继续做二分法
21                     low = mid + 1
22                 else:
23                     # 否则,取mid左侧的数组做二分法
24                     high = mid
25             else:
26                 high = mid
27         return nums[low]

 

Date: 2021/4/9

Source: Leetcode No.43 字符串相乘

Language: Python 3

Coding:

 1 class Solution:
 2     def multiply(self, num1: str, num2: str) -> str:
 3         n1, n2 = 0, 0
 4         for i in range(len(num1)):
 5             n1 = 10*n1 + int(num1[i])
 6         for i in range(len(num2)):
 7             n2 = 10*n2 + int(num2[i])
 8         ans = n1 * n2 
 9         arr = []
10         while ans > 10:
11             arr.append(str(ans//10))
12             ans = ans % 10
13         arr.append(str(ans))
14         ansarr = "".join(arr)
15         return str(ansarr)

 

posted @ 2021-04-09 16:07  飞奔的可亦  阅读(50)  评论(0编辑  收藏  举报