Skip to content

Commit 662e343

Browse files
committedMar 2, 2020
add 1367
1 parent 1bdac9e commit 662e343

File tree

7 files changed

+151
-4
lines changed

7 files changed

+151
-4
lines changed
 

Diff for: ‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -657,4 +657,5 @@ LeetCode
657657
|1362|[Closest Divisors](https://leetcode.com/problems/closest-divisors/)|c|[c++](./src/1362-Closest-Divisors/1362.cpp)|[python](./src/1362-Closest-Divisors/1362.py)|[go](./src/1362-Closest-Divisors/1362.go)|[js](./src/1362-Closest-Divisors/1362.js)|[java](./src/1362-Closest-Divisors/1362.java)|Medium|
658658
|1363|[Largest Multiple of Three](https://leetcode.com/problems/largest-multiple-of-three/)|c|[c++](./src/1363-Largest-Multiple-of-Three/1363.cpp)|[python](./src/1363-Largest-Multiple-of-Three/1363.py)|[go](./src/1363-Largest-Multiple-of-Three/1363.go)|[js](./src/1363-Largest-Multiple-of-Three/1363.js)|[java](./src/1363-Largest-Multiple-of-Three/1363.java)|Hard|
659659
|1365|[How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)|c|[c++](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.cpp)|[python](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.py)|[go](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.go)|[js](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.js)|[java](./src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.java)|Easy|
660-
|1366|[Rank Teams by Votes](https://leetcode.com/problems/rank-teams-by-votes/)|c|[c++](./src/1366-Rank-Teams-by-Votes/1366.cpp)|[python](./src/1366-Rank-Teams-by-Votes/1366.py)|[go](./src/1366-Rank-Teams-by-Votes/1366.go)|[js](./src/1366-Rank-Teams-by-Votes/1366.js)|[java](./src/1366-Rank-Teams-by-Votes/1366.java)|Medium|
660+
|1366|[Rank Teams by Votes](https://leetcode.com/problems/rank-teams-by-votes/)|c|[c++](./src/1366-Rank-Teams-by-Votes/1366.cpp)|[python](./src/1366-Rank-Teams-by-Votes/1366.py)|[go](./src/1366-Rank-Teams-by-Votes/1366.go)|[js](./src/1366-Rank-Teams-by-Votes/1366.js)|[java](./src/1366-Rank-Teams-by-Votes/1366.java)|Medium|
661+
|1367|[Linked List in Binary Tree](https://leetcode.com/problems/linked-list-in-binary-tree/)|c|[c++](./src/1367-Linked-List-in-Binary-Tree/1367.cpp)|[python](./src/1367-Linked-List-in-Binary-Tree/1367.py)|[go](./src/1367-Linked-List-in-Binary-Tree/1367.go)|[js](./src/1367-Linked-List-in-Binary-Tree/1367.js)|[java](./src/1367-Linked-List-in-Binary-Tree/1367.java)|Medium|

Diff for: ‎src/1367-Linked-List-in-Binary-Tree/1367.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution
2+
{
3+
public:
4+
bool isSubPath(ListNode* head, TreeNode* root)
5+
{
6+
f.emplace_back(-1);
7+
int i = -1;
8+
ListNode* node = head;
9+
10+
while (node != nullptr)
11+
{
12+
while (i != -1 && node != nullptr && node->val != arr[i]) i = f[i];
13+
i++;
14+
f.emplace_back(i);
15+
arr.emplace_back(node->val);
16+
node = node->next;
17+
}
18+
19+
return dfs(root, 0);
20+
}
21+
private:
22+
vector<int> arr, f;
23+
24+
bool dfs(TreeNode* node, int u)
25+
{
26+
if (node == nullptr) return false;
27+
28+
while (u != -1 && node->val != arr[u]) u = f[u];
29+
u++;
30+
31+
if (u == arr.size()) return true;
32+
return dfs(node->left, u) || dfs(node->right, u);
33+
}
34+
};

Diff for: ‎src/1367-Linked-List-in-Binary-Tree/1367.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var arr, f []int
2+
3+
func isSubPath(head *ListNode, root *TreeNode) bool {
4+
arr, f = []int{}, []int{-1}
5+
i, node := -1, head
6+
7+
for node != nil {
8+
for i != -1 && node != nil && node.Val != arr[i] {
9+
i = f[i]
10+
}
11+
i++
12+
13+
arr = append(arr, node.Val)
14+
f = append(f, i)
15+
node = node.Next
16+
}
17+
18+
return dfs(root, 0)
19+
}
20+
21+
func dfs(root *TreeNode, u int) bool {
22+
if root == nil {
23+
return false
24+
}
25+
26+
for u != -1 && arr[u] != root.Val {
27+
u = f[u]
28+
}
29+
u++
30+
31+
if u == len(arr) {
32+
return true
33+
}
34+
return dfs(root.Left, u) || dfs(root.Right, u)
35+
}

Diff for: ‎src/1367-Linked-List-in-Binary-Tree/1367.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public boolean isSubPath(ListNode head, TreeNode root) {
3+
arr = new ArrayList();
4+
f = new ArrayList();
5+
f.add(-1);
6+
int i = -1;
7+
ListNode node = head;
8+
9+
while (node != null) {
10+
while (i != -1 && node != null && node.val != arr.get(i)) i = f.get(i);
11+
i++;
12+
f.add(i);
13+
arr.add(node.val);
14+
node = node.next;
15+
}
16+
return dfs(root, 0);
17+
}
18+
19+
private List<Integer> arr, f;
20+
21+
private boolean dfs(TreeNode node, int u) {
22+
if (node == null) return false;
23+
24+
while (u != -1 && node.val != arr.get(u)) {
25+
u = f.get(u);
26+
}
27+
u++;
28+
29+
if (u == arr.size()) return true;
30+
return dfs(node.left, u) || dfs(node.right, u);
31+
}
32+
}

Diff for: ‎src/1367-Linked-List-in-Binary-Tree/1367.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var isSubPath = function(head, root) {
2+
let arr = [], f = [-1];
3+
let node = head, i = -1;
4+
5+
while (node != null) {
6+
while (i != -1 && node != null && node.val != arr[i]) i = f[i];
7+
i++;
8+
9+
arr.push(node.val);
10+
f.push(i);
11+
node = node.next;
12+
}
13+
14+
let dfs = function(cur, u) {
15+
if (cur == null) return false;
16+
17+
while (u != -1 && cur.val != arr[u]) u = f[u];
18+
u++;
19+
20+
if (u == arr.length) return true;
21+
return dfs(cur.left, u) || dfs(cur.right, u);
22+
}
23+
24+
return dfs(root, 0);
25+
};

Diff for: ‎src/1367-Linked-List-in-Binary-Tree/1367.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
3+
arr, f = [], [-1]
4+
i, node = -1, head
5+
while node: # 计算next数组
6+
while i != -1 and node and node.val != arr[i]:
7+
i = f[i]
8+
i += 1
9+
f.append(i)
10+
arr.append(node.val)
11+
node = node.next
12+
13+
def dfs(root, u):
14+
if not root: return False
15+
while u != -1 and root.val != arr[u]:
16+
u = f[u]
17+
u += 1
18+
if u == len(arr): return True
19+
return dfs(root.left, u) or dfs(root.right, u)
20+
return dfs(root, 0)

Diff for: ‎src/addProb.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import os, bisect
33

44
# 题目名称
5-
name = "Rank Teams by Votes"
6-
ID = 1366
7-
url = "https://leetcode.com/problems/rank-teams-by-votes/"
5+
name = "Linked List in Binary Tree"
6+
ID = 1367
7+
url = "https://leetcode.com/problems/linked-list-in-binary-tree/"
88
difficult = "Medium"
99
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']
1010

0 commit comments

Comments
 (0)