题目1
题解1
class Solution :
"""
时间复杂度: O(n^2)
空间复杂度: O(n)
"""
def singleNumber ( self, nums: List[ int ] ) - > int :
temp = [ ]
for num in nums:
if num in temp:
temp. remove( num)
else :
temp. append( num)
return temp[ 0 ]
def singleNumber ( self, nums: List[ int ] ) - > int :
"""
时间复杂度: O(n)
空间复杂度: O(n)
"""
from collections import Counter
datas = Counter( nums)
for data in datas. keys( ) :
if datas[ data] == 1 :
return data
附上题目链接
题目2
题解2
class Solution :
def maxProfit ( self, prices: List[ int ] ) - > int :
"""
贪心算法
"""
profit = 0
for i in range ( 1 , len ( prices) ) :
temp = prices[ i] - prices[ i - 1 ]
if temp > 0 :
profit += temp
return profit
def maxProfit ( self, prices: List[ int ] ) - > int :
"""
动态规划
"""
n = len ( prices)
DP = [ None ] * n
if n <= 0 :
return 0
DP[ 0 ] = 0
for i in range ( 1 , n) :
DP[ i] = DP[ i- 1 ] + prices[ i] - prices[ i- 1 ] if prices[ i] - prices[ i- 1 ] > 0 else DP[ i- 1 ]
return DP[ - 1 ]
附上题目链接
题目3
题解3
class Solution :
def isPalindrome ( self, s: str ) - > bool :
snew = ''
s = s. lower( )
for i in s:
if i in 'abcdefghijklmnopqrstuvwxyz0123456789' :
snew = snew + i
n = len ( snew)
if n <= 0 :
return True
for i in range ( n// 2 ) :
if snew[ i] != snew[ n- i- 1 ] :
return False
return True
附上题目链接