- Pow(x, n), find the nth power of x
class Solution(object):
def myPow(self, x:float, n:int):
"""
:type x: float
:type n: int
:rtype: float
"""
if n==0:
return 1
if n<0:
return 1/self.myPow(x,-n)
if n%2==1:
return x*self.myPow(x,n-1)
return self.myPow(x*x,n/2)
a=Solution()
print(a.myPow(2.0000,10))
- 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.
Divide and conquer:
class Solution:
def maxSubArray(self, nums: list) -> int:
n = len(nums)
# 递归终止条件
if n == 1:
return nums[0]
else:
# 递归计算左半边最大子序和
max_left = self.maxSubArray(nums[0:len(nums) // 2])
# 递归计算右半边最大子序和
max_right = self.maxSubArray(nums[len(nums) // 2:len(nums)])
# 计算中间的最大子序和,从右到左计算左边的最大子序和,从左到右计算右边的最大子序和,再相加
max_l = nums[len(nums) // 2 - 1]
tmp = 0
for i in range(len(nums) // 2 - 1, -1, -1):
tmp += nums[i]
max_l = max(tmp, max_l)
max_r = nums[len(nums) // 2]
tmp = 0
for i in range(len(nums) // 2, len(nums)):
tmp += nums[i]
max_r = max(tmp, max_r)
# 返回三个中的最大值
return max(max_right, max_left, max_l + max_r)
a=Solution();
print(a.maxSubArray([-2,1,-3]))
Dynamic programming method:
class Solution:
def maxSubArray(self, nums: list) -> int:
n=len(nums)
for i in range(1,n):
if nums[i-1]>0:
nums[i]+=nums[i-1]
return max(nums)
a=Solution()
print(a.maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))
Greedy method:
class Solution:
def maxSubArray(self, nums: list) -> int:
if not nums:
return -2147483648
cur_sum = max_sum=nums[0]
for i in range(1,len(nums)):
cur_sum=max(cur_sum+nums[i],nums[i])
max_sum=max(max_sum,cur_sum)
return max_sum
a=Solution()
print(a.maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))
- Majority elements
Given an array of size n, find the majority of elements in it. Most elements refer to elements that appear more than ⌊ n/2 ⌋ in the array.
Method 1:
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
b=set(nums)
for i in b:
n=0
for j in nums:
if j == i:
n+=1
if n>len(nums)//2:
return i
a=Solution()
print(a.majorityElement([2,3,2,2,1,2,2,3,3,3,3,3,3,3,3]))
Method two, hash into the dictionary
from collections import Counter
class Solution:
def majorityElement(self, nums):
counts = Counter(nums)
return max(counts.keys(), key=counts.get)
a=Solution()
print(a.majorityElement([2,2,3]))
Method three, sort
class Solution:
def majorityElement(self, nums):
nums.sort()
return nums[len(nums)//2]
a=Solution()
print(a.majorityElement([2,3,2,2,1,2,2,3]))
For this algorithm, we sort the nums array first, and then return the elements corresponding to the subscripts mentioned above.
class Solution:
def majorityElement(self, nums, lo=0, hi=None):
def majority_element_rec(lo, hi):
# base case; the only element in an array of size 1 is the majority
# element.
if lo == hi:
return nums[lo]
# recurse on left and right halves of this slice.
mid = (hi-lo)//2 + lo
left = majority_element_rec(lo, mid)
right = majority_element_rec(mid+1, hi)
# if the two halves agree on the majority element, return it.
if left == right:
return left
# otherwise, count each element and return the "winner".
left_count = sum(1 for i in range(lo, hi+1) if nums[i] == left)
right_count = sum(1 for i in range(lo, hi+1) if nums[i] == right)
return left if left_count > right_count else right
return majority_element_rec(0, len(nums)-1)
a=Solution()
print(a.majorityElement([1,2,3,5,6,2]))