Skip to content

Commit 7a6950a

Browse files
committed
2019.3.16
1 parent 8fb99f6 commit 7a6950a

8 files changed

+338
-12
lines changed
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'''
2+
3+
Write a program to find the node at which the intersection of two singly linked lists begins.
4+
5+
For example, the following two linked lists:
6+
7+
8+
begin to intersect at node c1.
9+
10+
11+
12+
Example 1:
13+
14+
15+
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
16+
Output: Reference of the node with value = 8
17+
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
18+
19+
20+
Example 2:
21+
22+
23+
Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
24+
Output: Reference of the node with value = 2
25+
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.
26+
27+
28+
Example 3:
29+
30+
31+
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
32+
Output: null
33+
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
34+
Explanation: The two lists do not intersect, so return null.
35+
36+
37+
Notes:
38+
39+
If the two linked lists have no intersection at all, return null.
40+
The linked lists must retain their original structure after the function returns.
41+
You may assume there are no cycles anywhere in the entire linked structure.
42+
Your code should preferably run in O(n) time and use only O(1) memory.
43+
44+
'''
45+
46+
47+
48+
# Definition for singly-linked list.
49+
# class ListNode(object):
50+
# def __init__(self, x):
51+
# self.val = x
52+
# self.next = None
53+
54+
class Solution(object):
55+
def getIntersectionNode(self, headA, headB):
56+
"""
57+
:type head1, head1: ListNode
58+
:rtype: ListNode
59+
"""
60+
61+
# Approach one 循环两次,消除相交点之前的长度差
62+
if not headA or not headB : return None
63+
p1, p2 = headA, headB
64+
while(p1 != p2):
65+
p1 = headB if not p1 else p1.next
66+
p2 = headA if not p2 else p2.next
67+
return p1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'''
2+
3+
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
4+
5+
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
6+
7+
Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]
8+
9+
10+
11+
12+
Example 1:
13+
14+
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
15+
Output: 6
16+
Explanation: The LCA of nodes 2 and 8 is 6.
17+
Example 2:
18+
19+
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
20+
Output: 2
21+
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
22+
23+
24+
Note:
25+
26+
All of the nodes' values will be unique.
27+
p and q are different and both values will exist in the BST.
28+
29+
'''
30+
31+
32+
# Definition for a binary tree node.
33+
# class TreeNode:
34+
# def __init__(self, x):
35+
# self.val = x
36+
# self.left = None
37+
# self.right = None
38+
39+
class Solution:
40+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
41+
42+
# # Approach one 递归求解
43+
# if p.val < root.val > q.val:
44+
# return self.lowestCommonAncestor(root.left, p, q)
45+
# if p.val > root.val < q.val:
46+
# return self.lowestCommonAncestor(root.right, p, q)
47+
# return root
48+
49+
50+
# Approach two 迭代求解
51+
while True:
52+
if p.val < root.val > q.val:
53+
root = root.left
54+
elif p.val > root.val < q.val:
55+
root = root.right
56+
else:
57+
return root

Python/maximum-depth-of-binary-tree.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,5 @@ def maxDepth(self, root):
3333
:type root: TreeNode
3434
:rtype: int
3535
"""
36-
# root 结点为空的时候,单独处理
37-
if root is None:
38-
return 0
39-
40-
# 注意,加一操作放在外边可以减少加法运算,不影响比大小的结果。
36+
if not root : return 0
4137
return max(self.maxDepth(root.left), self.maxDepth(root.right))+1
42-

Python/maximum-subarray.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
3+
4+
Example:
5+
6+
Input: [-2,1,-3,4,-1,2,1,-5,4],
7+
Output: 6
8+
Explanation: [4,-1,2,1] has the largest sum = 6.
9+
Follow up:
10+
11+
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
12+
13+
14+
15+
'''
16+
17+
18+
19+
class Solution:
20+
def maxSubArray(self, nums: List[int]) -> int:
21+
22+
# Approach one 暴力滑窗 TLE
23+
# res = [nums[0]]
24+
# for i in range(len(nums)):
25+
# for j in range(i+1, len(nums)+1):
26+
# res.append(sum(nums[i:j]))
27+
# return max(res)
28+
29+
30+
# Approach two DP
31+
res = nums[0]
32+
num = 0
33+
for i in range(len(nums)):
34+
num += nums[i]
35+
if num > res:
36+
res = num
37+
if num < 0:
38+
num = 0
39+
return res

Python/spiral-matrix-ii.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'''
2+
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
3+
4+
Example:
5+
6+
Input: 3
7+
Output:
8+
[
9+
[ 1, 2, 3 ],
10+
[ 8, 9, 4 ],
11+
[ 7, 6, 5 ]
12+
]
13+
14+
15+
'''
16+
17+
18+
class Solution(object):
19+
def generateMatrix(self, n):
20+
"""
21+
:type n: int
22+
:rtype: List[List[int]]
23+
"""
24+
25+
# Approach one
26+
res = [[ 0 for _ in range(n)] for _ in range(n)] # 注意, 不可用 n *[n *[0]] 初始化
27+
right , down , up ,left = True, False, False, False
28+
i , j = 0 , 0
29+
for num in range(1 , n*n+1):
30+
if right:
31+
res[i][j] = num
32+
j += 1
33+
if j == n or res[i][j] != 0:
34+
right, down = False, True
35+
j -= 1
36+
i += 1
37+
continue
38+
if down:
39+
res[i][j] = num
40+
i += 1
41+
if i == n or res[i][j] != 0:
42+
down, left = False, True
43+
i -= 1
44+
j -= 1
45+
continue
46+
if left:
47+
res[i][j] = num
48+
j -= 1
49+
if j == -1 or res[i][j] != 0:
50+
left, up = False, True
51+
j += 1
52+
i -= 1
53+
continue
54+
if up:
55+
res[i][j] = num
56+
i -= 1
57+
if i == -1 or res[i][j] != 0:
58+
up, right = False, True
59+
i += 1
60+
j += 1
61+
continue
62+
return res

Python/subsets.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
3+
Given a set of distinct integers, nums, return all possible subsets (the power set).
4+
5+
Note: The solution set must not contain duplicate subsets.
6+
7+
Example:
8+
9+
Input: nums = [1,2,3]
10+
Output:
11+
[
12+
[3],
13+
[1],
14+
[2],
15+
[1,2,3],
16+
[1,3],
17+
[2,3],
18+
[1,2],
19+
[]
20+
]
21+
22+
23+
24+
'''
25+
26+
27+
class Solution:
28+
def subsets(self, nums: List[int]) -> List[List[int]]:
29+
30+
# Approach one 将nums拆解,递归求解, 相当于每次将nums[0] 添加到其他所有的已有子集中。
31+
# if not nums: return [[]]
32+
# res = self.subsets(nums[1:])
33+
# return res + [[nums[0]] + s for s in res]
34+
35+
36+
# Approach two 迭代求解, 消耗栈空间少
37+
# res = [[]]
38+
# for i in nums:
39+
# res += [ n + [i] for n in res]
40+
# return res
41+
42+
43+
# Approach three 华丽但不好想到的位运算处理法, 效率较低
44+
return [[nums[j] for j in range(len(nums)) if i>>j&1] for i in range(2**len(nums))]

Python/symmetric-tree.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'''
2+
3+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
4+
5+
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
6+
7+
1
8+
/ \
9+
2 2
10+
/ \ / \
11+
3 4 4 3
12+
But the following [1,2,2,null,3,null,3] is not:
13+
1
14+
/ \
15+
2 2
16+
\ \
17+
3 3
18+
Note:
19+
Bonus points if you could solve it both recursively and iteratively.
20+
21+
22+
'''
23+
24+
25+
# Definition for a binary tree node.
26+
# class TreeNode:
27+
# def __init__(self, x):
28+
# self.val = x
29+
# self.left = None
30+
# self.right = None
31+
32+
class Solution:
33+
34+
# Approach one 递归法
35+
# def isSymmetric(self, root: TreeNode) -> bool:
36+
# if not root : return True
37+
# return self.judge_tree(root.left , root.right)
38+
39+
40+
# def judge_tree(self, left , right):
41+
# if not left and not right : return True
42+
# if left and right :
43+
# if left.val == right.val :
44+
# return self.judge_tree(left.left, right.right) and self.judge_tree(left.right , right.left)
45+
# return False
46+
47+
48+
# Approach two 迭代
49+
def isSymmetric(self, root: TreeNode) -> bool:
50+
if not root : return True
51+
qlist=[root.left, root.right]
52+
while len(qlist)!=0:
53+
t1=qlist.pop()
54+
t2=qlist.pop()
55+
if not t1 and not t2: continue
56+
if not t1 or not t2: return False
57+
if t1.val != t2.val : return False
58+
qlist.append(t1.left)
59+
qlist.append(t2.right)
60+
qlist.append(t1.right)
61+
qlist.append(t2.left)
62+
return True

0 commit comments

Comments
 (0)