leetcode题目及答案python_leetcode题库解答源码(python3)

下面和大家分享本人在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:

new_nums.append(i)

new_nums.append(j)

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 = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}

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] == '(':

list.append(s[i])

a += 1

if s[i] == '{':

list.append(s[i])

b += 1

if s[i] == '[':

list.append(s[i])

c += 1

if s[i] == ')':

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

list.pop()

a -= 1

else:

return False

if s[i] == '}':

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

list.pop()

b -= 1

else:

return False

if s[i] == ']':

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

list.pop()

c -= 1

else:

return False

if len(list) == 0 and a == b == c == 0:

return True

else:

return False

'''

dic = {')':'(','{':'}','[':']'}

stack = []

for i in s:

if i in dic.values():

stack.append(i)

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题目及答案python_leetcode题库解答源码(python3))