leetcode:1-5题代码整理

时间:2023-03-08 15:35:58
leetcode:1-5题代码整理

以下是这段时间抽时间刷的前5题,都是自己想的解法,或许不是最优解,只是整理下,方便日后优化提升

1. Two Sum:

class Solution:
# @return a tuple, (index1, index2)
def twoSum(self, num, target):
dict = {}
for i in xrange(len(num)):
if dict.get(target-num[i], None) == None:
dict[num[i]] = i
else:
return (dict[target-num[i]] , i )

2. Add Two Numbers:

class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
more=0
l3=ListNode(0) l3.val=l1.val+l2.val+more
if l3.val>=10:
more=1
else:
more=0
l3.val=l3.val%10
l1_temp=l3
l1=l1.next
l2=l2.next while(l1 and l2):
temp=ListNode(0)
temp.val=l1.val+l2.val+more
if temp.val>=10:
more=1
else:
more=0
temp.val=temp.val%10 l1_temp.next=temp
l1_temp=temp
l1=l1.next
l2=l2.next if((l1 is None )and( l2 is None)):
if more==1:
temp=ListNode(0)
temp.val=1
l1_temp.next=temp
return l3 elif(l1 and ( l2 is None)):
while(l1):
temp=ListNode(0)
temp.val=more+l1.val
if temp.val>=10:
more=1
else:
more=0
temp.val=temp.val%10
l1_temp.next=temp
l1_temp=temp
l1=l1.next
if more==1:
temp=ListNode(0)
temp.val=1
l1_temp.next=temp
return l3 elif(l2 and ( l1 is None)):
while(l2):
temp=ListNode(0)
temp.val=more+l2.val
if temp.val>=10:
more=1
else:
more=0
temp.val=temp.val%10
l1_temp.next=temp
l1_temp=temp
l2=l2.next
if more==1:
temp=ListNode(0)
temp.val=1
l1_temp.next=temp
return l3

3. Longest Substring Without Repeating Characters:

class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
new_str = ''
max = 0
for ch in s:
if not ch in new_str:
new_str += ch
else:
max = len(new_str) if len(new_str) > max else max
idx = new_str.find(ch)
new_str = new_str[idx+1:] + ch
max = len(new_str) if len(new_str) > max else max
return max

4. Median of Two Sorted Arrays:

class Solution(object):
def findMedianSortedArrays(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: float
"""
end1=len(nums1)-1
end2=len(nums2)-1
if len(nums1)==0:
num=len(nums2)
if num%2: #num是一个奇数
return float(nums2[num/2])
else:
median=num/2
return (float(nums2[median-1])+nums2[median])/2 elif len(nums2)==0:
num=len(nums1)
if num%2: #num是一个奇数
median=num/2
return float(nums1[median])
else:
median=num/2
return (float(nums1[median-1])+nums1[median])/2 elif nums1[end1]<nums2[0]:
nums1.extend(nums2)
num=len(nums1)
if num%2: #num是一个奇数
median=num/2
return float(nums1[median])
else:
median=num/2
return (float(nums1[median-1])+nums1[median])/2 elif nums1[0]>nums2[end2]:
nums2.extend(nums1)
num=len(nums2)
if num%2: #num是一个奇数
return float(nums2[num/2])
else:
median=num/2
return (float(nums2[median-1])+nums2[median])/2 else:
i=0
j=0
new=[]
while i<len(nums1) and j<len(nums2):
if nums1[i]<nums2[j]:
new.append(nums1[i])
i=i+1
elif nums1[i]==nums2[j]:
new.append(nums1[i])
new.append(nums1[i])
i=i+1
j=j+1
else:
new.append(nums2[j])
j=j+1
while i<len(nums1):
new.append(nums1[i])
i=i+1
while j<len(nums2):
new.append(nums2[j])
j=j+1
num=len(new)
if num%2: #num是一个奇数
return float(new[num/2])
else:
median=num/2
return (float(new[median-1])+new[median])/2

5. Longest Palindromic Substring:

class Solution(object):

    def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if len(s)==1:
return s
elif len(s)==2 and s[0]==s[1]:
return s
else:
s_fin1=""
s_fin2="" index_l=0
index_r=1
max_length=0
left=-1
right=-1
flag=0
while index_r<len(s):
i=0
length=0
while index_l-i>=0 and index_r+i<len(s) and s[index_l-i]==s[index_r+i]:
i=i+1
length=length+2
if length>max_length:
max_length=length
left=index_l
right=index_r
index_l=index_l+1
index_r=index_r+1
if max_length:
begin=left-max_length/2+1
end=right+max_length/2
flag=2
s_fin1=s[begin:end] index=1
max_length=1
center=-1
flag=0
while index<(len(s)-1):
i=1
length=1
while index-i>=0 and index+i<len(s) and s[index-i]==s[index+i]:
i=i+1
length=length+2
if length>max_length:
max_length=length
center=index
index=index+1
if max_length>1:
begin=center-(max_length-1)/2
end=center+(max_length-1)/2+1
flag=1
s_fin2=s[begin:end] if len(s_fin1)>len(s_fin2):
return s_fin1
else:
return s_fin2