leetcode題庫解答原始碼(python3)

2022-08-27 13:09:11 字數 4178 閱讀 9591

下面和大家分享本人在leetcode上已經ace的題目原始碼(python3): 本人會持續更新!~

class leetcode_solution(object):

def twosum_1(self,nums, target):

""":type nums: list[int]

:type target: int

:rtype: list[int]

"""'''

# 此解法複雜度為o(n^2)

new_nums =

for i in range(len(nums)):

for j in range(i+1,len(nums)):

if nums[i] + nums[j] == target:

return new_nums

'''# 此解法複雜度為o(n)

# 拓展:若解不唯一,可先將nums排序後進行下面操作,將全部符合對輸出

if len(nums)<= 1:

return false

else:

dict = {}

for i in range(len(nums)):

# 字典底層是用hash表實現的,無論字典中有多少元素,查詢的平雲複雜度均為o(1)

if num[i] in dict:

return [dict[nums[i]], i]

else:

dict[target - nums[i]] = i

def reverse_7(self,x):

""":type x: int

:rtype: int

"""max = 2**31 - 1

min = -1*2**31

if x < 0:

y = -1*int(str(-x)[::-1])

else:

y = int(str(x)[::-1])

if y > max or y < min:

return 0

return y

def ispalindrome_9(self, x):

renum = 0

if x < 0 or (x % 10 == 0 and x != 0):

return false

while x > renum:

renum = renum * 10 + x % 10

x /= 10

return x == renum or x == renum/10

def romantoint_13(self, s):

""":type s: str

:rtype: int

"""dic =

sum = 0

for i in range(len(s)-1):

if dic[s[i]] < dic[s[i+1]]:

sum -= dic[s[i]]

else:

sum += dic[s[i]]

return sum + dic[s[-1]]

def longestcommonprefix_14(self, strs):

""":type strs: list[str]

:rtype: str

"""if len(strs) == 0: # horizontal scanning/////another way: vertical scanning

return ''

prefix = strs[0]

for i in range(1,len(strs)):

while strs[i].find(prefix) != 0:

prefix = prefix[0:len(prefix)-1]

if prefix == '':

return ''

return prefix

def isvalid_20(self, s):

""":type s: str

:rtype: bool

"""'''

list =

a = b = c = 0

if len(s) == 0:

return true

for i in range(len(s)):

if s[i] == '(':

a += 1

if s[i] == '':

if len(list) != 0 and list[-1] == '','[':']'}

stack =

for i in s:

if i in dic.values():

elif i in dic.keys():

if stack == or dic[i] != stack.pop():

return false

else:

return false

return stack ==

def mergetwolists_21(self, l1, l2):

""":type l1: listnode

:type l2: listnode

:rtype: listnode

"""# definition for singly-linked list.

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

head = rear = listnode(0)

while l1 and l2:

if l1.val < l2.val:

rear.next = l1

l1 = l1.next

else:

rear.next = l2

l2 = l2.next

rear = rear.next

rear.next = l1 or l2

return head.next

def removeduplicates_26(self, nums):

""":type nums: list[int]

:rtype: int

"""if len(nums) == 0:

return 0

newtail = 0

for i in range(1,len(nums)):

if nums[i] != nums[newtail]:

newtail += 1

nums[newtail] = nums[i]

return newtail + 1

def removeelement_27(self, nums, val):

""":type nums: list[int]

:type val: int

:rtype: int

"""i = len(nums)

j = 0

if i == 0:

return 0

while j < i:

if nums[j] == val:

nums.pop(j)

i -= 1

else:

j += 1

return len(nums)

def strstr_28(self, haystack, needle):

""":type haystack: str

:type needle: str

:rtype: int

"""for i in range(len(haystack) - len(needle) +1):

if haystack[i:i+len(needle)] == needle:

return i

return -1

def searchinsert_35(self, nums, target):

""":type nums: list[int]

:type target: int

:rtype: int

"""return len([x for x in nums if x < target])

def countandsay(self, n):

""":type n: int

:rtype: str

"""

leetcode題庫 回文數

判斷乙個整數是否是回文數。回文數是指正序 從左向右 和倒序 從右向左 讀都是一樣的整數。輸入 121 輸出 true輸入 121 輸出 false 解釋 從左向右讀,為 121 從右向左讀,為 121 因此它不是乙個回文數。輸入 10 輸出 false 解釋 從右向左讀,為 01 因此它不是乙個回文...

LeetCode題庫 簡單題

leetcode 題目鏈結 反轉一半 負數不可能是回文 個位數一定是回文 0是回文 問題 如何反轉一半的數字 演算法例項 bool ispalindrome int x if x 10 x為個位數 一定是回文 return1 int revernum 0 while x revernum retur...

LeetCode 題庫練習 2

題目 給出兩個 非空 的鍊錶用來表示兩個非負的整數。其中,它們各自的位數是按照 逆序 的方式儲存的,並且它們的每個節點只能儲存 一位 數字。如果,我們將這兩個數相加起來,則會返回乙個新的鍊錶來表示它們的和。您可以假設除了數字 0 之外,這兩個數都不會以 0 開頭。示例 輸入 2 4 3 5 6 4 ...