这周也是完成了5道题目的小李.
17 电话号码的组合 :
给定一个仅包含数字 2-9
的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回
这样写 一开始我是拒绝的 可是他们说 可以加特技 时间很快,方法简单,duang的 很快很好 打败99.95 暴力判断长度
class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ nto = {'2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz'} rel = [] com = '' n = len(digits) if n == 0: pass if n == 1: for each in nto[digits[0]]: rel.append(each) if n == 2: for i in nto[digits[0]]: for j in nto[digits[1]]: com = i+j rel.append(com) if n == 3: for i in nto[digits[0]]: for j in nto[digits[1]]: for k in nto[digits[2]]: com = i+j+k rel.append(com) if n == 4: for i in nto[digits[0]]: for j in nto[digits[1]]: for k in nto[digits[2]]: for l in nto[digits[3]]: com = i+j+k+l rel.append(com) return rel 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/solution/dui-bu-qi-dui-bu-qi-dui-bu-qi-dui-bu-qi-zbfrr/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
18: 四数之和
直接对三数之和一个模仿
N3方
class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[List[int]] """ nums = sorted(nums) n = len(nums) ll = 0 rr= n-1 rel = [] while ll < n-3: rr= n-1 while ll < rr - 2: l = ll+1 r = rr-1 while l < r: numsum = nums[ll]+ nums[rr]+ nums [l] +nums [r] if numsum == target: rel.append([nums[ll],nums [l],nums [r],nums[rr]]) l += 1 while l < r and nums[l] == nums[l-1]: l += 1 r -= 1 while l < r and nums[r] == nums[r+1]: r -= 1 elif numsum > target: r -= 1 while l < r and nums[r] == nums[r+1]: r -= 1 else: l += 1 while l < r and nums[l] == nums[l-1]: l += 1 rr -= 1 while ll < rr-2 and nums[rr] == nums[rr+1]: rr -= 1 ll += 1 while ll < n-3 and nums[ll] == nums[ll-1]: ll += 1 return rel 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/4sum/solution/dui-san-shu-zhi-he-de-yi-ge-zhuo-lie-de-ht9ly/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
19 :链表想难道我一个考研的 怕是有点难 ,经典双指针
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ l = head r = head while(n>0): n -= 1 r = r.next if r == None: #想删除第一个结点了 return head.next while r.next != None: #找到删除节点的前一个节点 r = r.next l = l.next l.next = l.next.next return head 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/solution/jing-dian-shuang-zhi-zhen-by-yizhu-jia-bydl/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
20:括号的合法性
用了一个字典 看起来好看 空间直接爆炸
class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ dic1 = {'(':1,'[':2,'{':3,')':-1,']':-2,'}':-3} stack = [] for each in s: if dic1[each] > 0 : #如果是左括号 就入栈 stack.append(dic1[each]) else: #如果是右括号 先判断栈空不空 再判断是否匹配 if not stack: return False if dic1[each] + stack[-1] != 0: return False stack.pop() #匹配就出栈 if stack: return False #遍历完栈里还不空 就返回失败 return True 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/valid-parentheses/solution/zhe-ge-xie-fa-ying-gai-suan-shi-jian-ji-dsd0d/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
21 合并有序表
不谈了 没头节点好麻烦
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if l1 == None: return l2 if l2 == None: return l1 if l1.val > l2.val : #让l1 指向的节点值小 因为我准备把L2拼接到l1 p = l1 l1 = l2 l2 = p p = l1 q = l2 #定义两个遍历指针 # 如果l1的小 就不动 继续往后 #L2的小于等于l1的就加入l1 while p.next != None and q != None: if p.next.val < q.val : p = p.next else: r = q.next q.next = p.next p.next = q q = r if p.next == None: p.next = q return l1 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/solution/mei-you-tou-jie-dian-hao-ma-fan-ya-by-yi-ziym/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。