Skip to content

Commit 1a2eb1f

Browse files
author
Jiayang Wu
committed
2019-08-30
1 parent 193c214 commit 1a2eb1f

File tree

5 files changed

+112
-21
lines changed

5 files changed

+112
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def closestValue(self, root, target):
10+
"""
11+
:type root: TreeNode
12+
:type target: float
13+
:rtype: int
14+
"""
15+
16+
def inOrder(node):
17+
if not node:
18+
return []
19+
return inOrder(node.left) + [node.val] + inOrder(node.right)
20+
21+
l = inOrder(root)
22+
res = 0
23+
min_sub = float("inf")
24+
25+
for num in l:
26+
if abs(num - target) < min_sub:
27+
min_sub = abs(num - target)
28+
res = num
29+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Codec:
2+
3+
def encode(self, strs):
4+
"""Encodes a list of strings to a single string.
5+
6+
:type strs: List[str]
7+
:rtype: str
8+
"""
9+
return strs
10+
11+
def decode(self, s):
12+
"""Decodes a single string to a list of strings.
13+
14+
:type s: str
15+
:rtype: List[str]
16+
"""
17+
return s
18+
19+
# Your Codec object will be instantiated and called as such:
20+
# codec = Codec()
21+
# codec.decode(codec.encode(strs))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
from heapq import *
8+
class Solution(object):
9+
def closestKValues(self, root, target, k):
10+
"""
11+
:type root: TreeNode
12+
:type target: float
13+
:type k: int
14+
:rtype: List[int]
15+
"""
16+
def inOrder(node):
17+
if not node:
18+
return []
19+
return inOrder(node.left) + [node.val] + inOrder(node.right)
20+
21+
l = inOrder(root)
22+
subs = []
23+
heapify(subs)
24+
for num in l:
25+
sub = abs(target - num)
26+
heappush(subs, (-sub, num))
27+
if len(subs) > k:
28+
heappop(subs)
29+
30+
res = []
31+
for sub, num in subs:
32+
res.append(num)
33+
return res
34+

0274.H指数/0274-H指数.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@ def hIndex(self, citations):
44
:type citations: List[int]
55
:rtype: int
66
"""
7+
# 0 1 3 6 5
78
citations.sort()
8-
l = len(citations)
9-
lo, hi = 0, l - 1
9+
n = len(citations)
10+
left, right = 0, len(citations) - 1
1011
res = 0
11-
while(lo <= hi):
12-
mid = lo + (hi - lo) // 2
13-
cnt = l - mid #包括mid自身右边还有的元素个数
14-
if citations[mid] >= cnt:
12+
while left <= right:
13+
14+
mid = (left + right) // 2
15+
16+
cnt = n - mid
17+
# print left, right, mid, citations[mid], cnt, citations
18+
if citations[mid] < cnt:
19+
left = mid + 1
20+
elif citations[mid] >= cnt:
1521
res = cnt
16-
hi = mid -1
17-
else:
18-
lo = mid + 1
22+
right = mid - 1
1923
return res
20-
24+
25+

0275.H指数II/0275-H指数II.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ def hIndex(self, citations):
44
:type citations: List[int]
55
:rtype: int
66
"""
7-
l = len(citations)
8-
lo, hi = 0, l - 1
7+
n = len(citations)
8+
left, right = 0, len(citations) - 1
99
res = 0
10-
while(lo <= hi):
11-
mid = lo + (hi - lo) // 2
12-
cnt = l - mid #包括mid自身右边还有的元素个数
13-
if citations[mid] >= cnt:
10+
while left <= right:
11+
12+
mid = (left + right) // 2
13+
14+
cnt = n - mid
15+
# print left, right, mid, citations[mid], cnt, citations
16+
if citations[mid] < cnt:
17+
left = mid + 1
18+
elif citations[mid] >= cnt:
1419
res = cnt
15-
hi = mid -1
16-
else:
17-
lo = mid + 1
18-
return res
19-
20+
right = mid - 1
21+
return res

0 commit comments

Comments
 (0)