LeetCode 刷題記錄(1 5題)

2022-08-01 02:30:15 字數 3778 閱讀 3771

1 兩數之和(題目鏈結)

class

solution: # 一次雜湊法

deftwosum(self, nums, target):

""":type nums: list[int]

:type target: int

:rtype: list[int]

"""m ={}

for i in

range(len(nums)):

minus = target -nums[i]

if minus in m and i !=m[minus]:

return

[i, m[minus]]

m[nums[i]] =i

return none

2 兩數相加(題目鏈結)

class

solution:

defaddtwonumbers(self, l1, l2):

""":type l1: listnode

:type l2: listnode

:rtype: listnode

"""up =0

l3 =listnode(0)

l3_head =l3

while l1 or

l2:

if l1 is

none:

l1 =listnode(0)

if l2 is

none:

l2 =listnode(0)

sum = l1.val + l2.val +up

if sum > 9:

sum = sum - 10l3.next =listnode(sum)

up = 1

else

: l3.next =listnode(sum)

up =0

l1 =l1.next

l2 =l2.next

l3 =l3.next

if up >0:

l3.next =listnode(up)

return l3_head.next

3 無重複字元的最長子串(題目鏈結)

class

solution:

deflengthoflongestsubstring(self, s):

""":type s: str

:rtype: int

"""num =0

temp =

for item_s in

s:

if item_s in

temp:

index = temp.index(item_s) + 1temp =temp[index:]

else

:

if len(temp) >num:

num =len(temp)

return num

4 尋找兩個有序陣列的中位數(題目鏈結)

class

solution:

deffindmediansortedarrays(self, nums1, nums2):

""":type nums1: list[int]

:type nums2: list[int]

:rtype: float

"""sum = (len(nums1) +len(nums2))

index = sum//2 + 1p1 =0

p2 =0

max1 =0

max2 =0

for item in

range(index):

max2 =max1

if p1 ==len(nums1):

max1 =nums2[p2]

p2 += 1

elif p2 ==len(nums2):

max1 =nums1[p1]

p1 += 1

elif nums1[p1] >nums2[p2]:

max1 =nums2[p2]

p2 += 1

else

: max1 =nums1[p1]

p1 += 1

if sum%2 == 1:

return

max1

else

:

return (max1 + max2)/2

5 最長回文子串(題目鏈結)

manacher演算法(「馬拉車演算法」,中心擴充套件法+不重複判斷)

class

solution:

deflongestpalindrome(self, s):

""":type s: str

:rtype: str

"""if s == ''

:

return

''sp = ''

sp_len = 2*len(s)+2radius = [0 for o in range(sp_len)] #

回文半徑列表

index_max = -1 #

最大回文子串中間所在index

r_max = -1 #

最大回文子串右邊界

def plalindrome(index, r_i=1):

while(sp[index-r_i] != '$'

and sp[index+r_i] != '$'

and sp[index-r_i] == sp[index+r_i]):

r_i += 1

return

r_i

for item in range(len(s)): #

字串的字元間加'#'與'$'

sp += ''.join(['#'

, s[item]])

sp = ''.join(['

$', sp, '#$'

])

for i in range(sp_len): #

計算回文半徑,填充半徑列表

if i >=r_max:

radius[i] =plalindrome(i)

if r_max r_max =radius[i]

index_max =i

elif r_max - i <= radius[2 * index_max -i]:

radius[i] = radius[2 * index_max -i]

else

: radius[i] = plalindrome(i, radius[2 * index_max - i] + 1)

if r_max r_max =radius[i]

index_max =i

result = sp[index_max - radius[index_max] + 1: index_max +radius[index_max]]

return result.replace('

#', '')

LeetCode刷題記錄1 5

題解dicts for index,value in enumerate nums temp target value if temp in dicts return dicts temp index dicts value index 也可以直接使用陣列遍歷,效果差不多 enumerate 函式用...

leetcode刷題記錄

我覺得每天來兩道,練習練習,再看看人家是怎麼優化的。1.給定乙個整數陣列 nums 和乙個目標值 target,請你在該陣列中找出和為目標值的那 兩個 整數,並返回他們的陣列下標。c 暴力求解,204ms,9.1m class solution for index,num in enumerate ...

LeetCode刷題記錄

動態規劃和貪心演算法的異同點 class solution throw newruntimeexception 時間複雜度 o n 2 對於每個元素,我們試圖通過遍歷陣列的其餘部分來尋找它所對應的目標元素,這將耗費 o n o n 的時間。因此時間複雜度為 o n 2 需要一種方法,尋找符合要求的元...