189.轮转数组
class Solution :
def rotate ( self, nums: List[ int ] , k: int ) - > None :
"""
Do not return anything, modify nums in-place instead.
"""
def reverse ( i, j) :
while i< j:
nums[ i] , nums[ j] = nums[ j] , nums[ i]
j-= 1
i+= 1
n= len ( nums)
k%= n
reverse( 0 , n- 1 )
reverse( 0 , k- 1 )
reverse( k, n- 1 )
121.买卖股票最佳时机1
class Solution :
def maxProfit ( self, prices: List[ int ] ) - > int :
minprice= float ( 'inf' )
maxprofit= 0
for price in prices:
minprice= min ( minprice, price)
maxprofit= max ( maxprofit, price- minprice)
return maxprofit
122.买卖股票最佳时机2
code1(YGGY法)
class Solution :
def maxProfit ( self, prices: List[ int ] ) - > int :
minprice= prices[ 0 ]
profit= 0
allprofit= 0
for i in range ( 1 , len ( prices) ) :
if prices[ i] < prices[ i- 1 ] :
minprice= prices[ i]
allprofit+= profit
profit= 0
else :
profit= max ( profit, prices[ i] - minprice)
return allprofit+ profit
code2(天王星法)
class Solution :
def maxProfit ( self, prices: List[ int ] ) - > int :
ans= 0
for i in range ( 1 , len ( prices) ) :
ans+= prices[ i] - prices[ i- 1 ] if prices[ i] > prices[ i- 1 ] else 0
return ans