classSolution:deftwoSum(self, nums, target):
hashtable =dict()for i, num inenumerate(nums):if target - num in hashtable:return[hashtable[target - num], i]
hashtable[nums[i]]= i
return[]#a =Solution#b = a.twoSum(0,[3,3,4,5,6],7)#print(b)
classSolution:deftwoSum(self, nums: List[int], target:int)-> List[int]:for i in nums:
j = target - i
start_index = nums.index(i)
next_index = start_index +1
temp_nums = nums[next_index:]if j in temp_nums:return(nums.index(i),next_index + temp_nums.index(j))
classSolution:deftwoSum(self, nums: List[int], target:int)-> List[int]:dict={}for i inrange(len(nums)):if target - nums[i]notindict:dict[nums[i]]= i
else:return[dict[target - nums[i]],i]
13.罗马数字转整数
classSolution:defromanToInt(self, s:str)->int:
numeral_map ={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
result =0for i inrange(len(s)):if i >0and numeral_map[s[i]]> numeral_map[s[i-1]]:
result += numeral_map[s[i]]-2* numeral_map[s[i-1]]else:
result += numeral_map[s[i]]return result
classSolution:defromanToInt(self, s:str)->int:
a ={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
ans=0for i inrange(len(s)):if i<len(s)-1and a[s[i]]<a[s[i+1]]:
ans-=a[s[i]]else:
ans+=a[s[i]]return ans
给定两个字符串 s 和 t,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
示例 1:
输入:s ="abcd", t ="abcde"
输出:"e"
解释:'e' 是那个被添加的字母。
# ord() 函数是 chr() 函数(对于8位的ASCII字符串)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的 ASCII 数值# map() 函数会根据提供的函数对指定序列做映射。 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。# 比较每个字符在两个字符串中出现的次数classSolution:deffindTheDifference(self, s:str, t:str)->str:for i in t:if t.count(i)-s.count(i)==1:return i
classSolution:deffindTheDifference(self, s:str, t:str)->str:for i in t:if i notin s or t.count(i)!= s.count(i):return i
# 因为字符串 t 是由字符串 s 随机重排,然后在随机位置添加一个字母。# 即对s和t中所有字符进行异或运算会剩下一字母也就是添加进去的那一个classSolution:deffindTheDifference(self, s:str, t:str)->str:# 利用字典# d = {}# for i in s:# if i in d:# d[i] += 1# else:# d[i] = 1 # for i in t:# if i in d:# if d[i] == 0:return i# d[i] -= 1# else:# return i# 位运算
ans =0for i in s:
ans ^=ord(i)for i in t:
ans ^=ord(i)returnchr(ans)
classSolution:defgenerateParenthesis(self, n:int)-> List[str]:if n ==0:return[]
result =[]
self.helpler(n,n,'',result)return result
defhelpler(self,l,r,item,result):if r < l:returnif l ==0and r ==0:
result.append(item)if l >0:
self.helpler(l-1,r,item+'(',result)if r >0:
self.helpler(l,r-1,item+')',result)
21.合并两个有序链表
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4,1->3->4
输出:1->1->2->3->4->4
# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclassSolution:defmergeTwoLists(self, l1, l2):"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
cur = head =ListNode(0)while l1 and l2:if l1.val <= l2.val:
cur.next= l1
l1 = l1.nextelse:
cur.next= l2
l2 = l2.next
cur = cur.next
cur.next= l1 or l2
return head.next
88.合并两个有序数组
给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。
说明:
初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。
你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。
示例:
输入:
nums1 =[1,2,3,0,0,0], m =3
nums2 =[2,5,6], n =3
输出:[1,2,2,3,5,6]
classSolution:defmerge(self, nums1: List[int], m:int, nums2: List[int], n:int)->None:"""
Do not return anything, modify nums1 in-place instead.
"""while m >0and n >0:if nums1[m-1]< nums2[n-1]:
nums1[m-1+n]= nums2[n-1]
n = n-1else:
nums1[m-1+n], nums1[m-1]= nums1[m-1], nums1[m-1+n]
m = m-1if m ==0and n >0:
nums1[:n]= nums2[:n]
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = NoneclassSolution:defdeleteDuplicates(self, head: ListNode)-> ListNode:
cur = head
while cur:
runner = cur.nextwhile runner and cur.val == runner.val:
runner = runner.next
cur.next= runner
cur = cur.nextreturn head
118.杨辉三角
classSolution:defgenerate(self, numRows:int)-> List[List[int]]:if numRows ==0:return[]
res =[[1]]whilelen(res)< numRows:
newRow =[a+b for a,b inzip([0]+res[-1],res[-1]+[0])]
res.append(newRow)return res