# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None
classSolution(object): defdetectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ ifnot head ornot head.next: returnNone slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: break else: returnNone while head != slow: slow = slow.next head = head.next return head
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None
classSolution(object): defreorderList(self, head): """ :type head: ListNode :rtype: None Do not return anything, modify head in-place instead. """ ifnot head ornot head.next: return s = f = head while f and f.next: s = s.next f = f.next.next head2 = s.next s.next = None p = None cur = head2 while cur: tmp = cur.next cur.next = p p = cur cur = tmp l1 = head l2 = p while l1 and l2: tmp = l2.next l2.next = l1.next l1.next = l2 l1 = l1.next.next l2 = tmp
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classSolution(object): defpostorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ res = [] ifnot root: return [] if root.left: res += self.postorderTraversal(root.left) if root.right: res += self.postorderTraversal(root.right) res.append(root.val) return res
6. LRU缓存机制(Medium)
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
defget(self, key): """ :type key: int :rtype: int """ if key in self.stack: self.cache.remove(key) self.cache.append(key) return self.stack[key] else: return -1 defput(self, key, value): """ :type key: int :type value: int :rtype: None """ if key in self.stack: self.cache.remove(key) else: iflen(self.stack) == self.capacity: del self.stack[self.cache[0]] self.cache.pop(0) self.cache.append(key) self.stack[key] = value # Your LRUCache object will be instantiated and called as such: # obj = LRUCache(capacity) # param_1 = obj.get(key) # obj.put(key,value)
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None
classSolution(object): defsortList(self, head): """ :type head: ListNode :rtype: ListNode """ ifnot head ornot head.next: return head slow,fast = head,head.next while fast and fast.next: fast,slow = fast.next.next,slow.next mid ,slow.next = slow.next,None left,right = self.sortList(head),self.sortList(mid) h = res = ListNode(0) while left and right: if left.val < right.val: h.next = left left = left.next else: h.next = right right = right.next h = h.next h.next = left if left else right return res.next
9. 直线上最多的点(Medium)
记录给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。
示例 1:
1 2 3 4 5 6 7 8 9 10
输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | o +-------------> 0 1 2 3 4
示例 2:
1 2 3 4 5 6 7 8 9 10 11
输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] 输出: 4 解释: ^ | | o | o o | o | o o +-------------------> 0 1 2 3 4 5 6
输入: "a good example" 输出: "example good a" 解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
说明:
无空格字符构成一个单词。
输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
进阶:
请选用 C 语言的用户尝试使用 O(1) 额外空间复杂度的原地解法。
解答:
思路:
双指针做法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution(object): defreverseWords(self, s): """ :type s: str :rtype: str """ s = s.strip() res = '' i,j = len(s)-1,len(s) while i > 0: if s[i] == ' ': res += s[i+1:j] + ' ' while s[i] == ' ': i -= 1 j = i +1 i -= 1 return res + s[:j]
classSolution(object): deflengthOfLongestSubstringTwoDistinct(self, s): """ :type s: str :rtype: int """ from collections import defaultdict lookup = defaultdict(int) start = end = 0 max_len = 0 cnt = 0 while end < len(s): if lookup[s[end]] == 0: cnt += 1 lookup[s[end]] += 1 end +=1 while cnt > 2: if lookup[s[start]] == 1: cnt -= 1 lookup[s[start]] -= 1 start += 1 max_len = max(max_len,end-start) return max_len
20. 相交链表(Easy)
编写一个程序,找到两个单链表相交的起始节点。
如下面的两个链表:
在节点 c1 开始相交。
示例 1:
1 2 3
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 输出:Reference of the node with value = 8 输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
示例 2:
1 2 3
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 输出:Reference of the node with value = 2 输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。