python 刷 leetcode 题目(33)

160相交链表

编写一个程序,找到两个单链表相交的起始节点。

例如,下面的两个链表

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3

在节点 c1 开始相交。

注意:

  • 如果两个链表没有交点,返回 null.
  • 在返回结果后,两个链表仍须保持原有的结构。
  • 可假定整个链表结构中没有循环。
  • 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。

 思路:其实就是暴力解决,找出他们共同部分,判断这一段是否相同。

代码如下:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        #### the first method 
#         if headA == None or headB == None:
#             return None
#         def get_length(head):
#             count = 0
#             if head == None:
#                 return count
#             else:
#                 while(head):
#                     head = head.next
#                     count += 1
#             return count
#         headA_len = get_length(headA)
#         headB_len = get_length(headB)
        
#         if headA_len >= headB_len:
#             for i in range(headA_len - headB_len):
#                 headA= headA.next
#         else:
#             for i in range(headB_len - headA_len):
#                 headB = headB.next
#         min_len = min(headA_len, headB_len)
#         for i in range(min_len):
#             if headA == headB:
#                 return headA
#             else:
#                 headA= headA.next
#                 headB= headB.next
#         return None
        
        #### the second method
        p1 = headA 
        p2 = headB
        while(p1 != p2):
            p1 = headB if p1 == None else  p1.next
            p2 = headA if p2 == None else  p2.next      
        return p1
        
        

17电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

代码:

class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        # dict_digits = {"2":"abc", "3": "def", "4":"ghi", "5":"jkl", "6":"mno", "7":"pqrs", "8":"tuv", "9":"wxyz"}
        # res = []
        # if len(digits) == 0:
        #     return res
        # if len(digits) == 1:
        #     return [str for str in dict_digits[digits[0]]]
        # temp = []
        # for i in range(len(digits)):
        #     if digits[i] in dict_digits:
        #         temp.append(dict_digits.get(digits[i]))
        # for j in range(len(temp) -1):
        #     for k in range(len(temp[j])):
        #         for m in range(len(temp[j + 1])):
        #             com = temp[j][k] + temp[j+1][m]
        #             res.append(com)
        # return res
        
        ### 
        if not digits:
            return []
        dict_digits = {"2":"abc", "3": "def", "4":"ghi", "5":"jkl", "6":"mno", "7":"pqrs", "8":"tuv", "9":"wxyz"}
        res = [str for str in dict_digits[digits[0]]]
        
        for i in digits[1:]:
            res = [m+n for m in res for n in dict_digits[i]]
        return res
    

猜你喜欢

转载自blog.csdn.net/annilingmo/article/details/80778303