Skip to content

Commit 4176b54

Browse files
authored
Merge pull request #677 from LetMeFly666/1367
添加问题“1367.二叉树中的链表”的代码和题解
2 parents f14c448 + d319c04 commit 4176b54

17 files changed

+564
-45
lines changed

Codes/1366-rank-teams-by-votes.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-30 09:20:50
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-30 13:20:36
6+
*/
7+
package main
8+
import "sort"
9+
10+
func rankTeams(votes []string) string {
11+
counts := make(map[byte][]int)
12+
for _, c := range votes[0] {
13+
counts[byte(c)] = make([]int, len(votes[0]))
14+
}
15+
for _, vote := range votes {
16+
for i, v := range vote {
17+
counts[byte(v)][i]++
18+
}
19+
}
20+
ans := []byte(votes[0])
21+
sort.Slice(ans, func(a, b int) bool {
22+
countA, countB := counts[ans[a]], counts[ans[b]]
23+
for i := range ans {
24+
if countA[i] != countB[i] {
25+
return countA[i] > countB[i]
26+
}
27+
}
28+
return ans[a] < ans[b]
29+
})
30+
return string(ans)
31+
}

Codes/1366-rank-teams-by-votes_Half.go

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-30 13:32:37
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-30 14:05:37
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* struct ListNode {
14+
* int val;
15+
* ListNode *next;
16+
* ListNode() : val(0), next(nullptr) {}
17+
* ListNode(int x) : val(x), next(nullptr) {}
18+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
19+
* };
20+
*/
21+
/**
22+
* Definition for a binary tree node.
23+
* struct TreeNode {
24+
* int val;
25+
* TreeNode *left;
26+
* TreeNode *right;
27+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
28+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
29+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
30+
* };
31+
*/
32+
// class Solution {
33+
// private:
34+
// bool dfs(ListNode* head, ListNode* now, TreeNode* root) {
35+
// if (!now) {
36+
// return true;
37+
// }
38+
// if (!root) {
39+
// return false;
40+
// }
41+
// if (now->val == root->val) {
42+
// if (dfs(head, now->next, root->left) || dfs(head, now->next, root->right)) {
43+
// return true;
44+
// }
45+
// }
46+
// if (head != now) {
47+
// if (dfs(head, head, root)) {
48+
// return true;
49+
// }
50+
// }
51+
// return dfs(head, now, root->left) || dfs(head, now, root->right);
52+
// }
53+
// public:
54+
// bool isSubPath(ListNode* head, TreeNode* root) {
55+
// return dfs(head, head, root);
56+
// }
57+
// };
58+
class Solution {
59+
private:
60+
bool dfs(ListNode* head, ListNode* l, TreeNode* t) {
61+
if (!l) {
62+
return true;
63+
}
64+
if (!t) {
65+
return false;
66+
}
67+
return l->val == t->val && (dfs(head, l->next, t->left) || dfs(head, l->next, t->right)) ||
68+
head == l && (dfs(head, l, t->left) || dfs(head, l, t->right));
69+
}
70+
public:
71+
bool isSubPath(ListNode* head, TreeNode* root) {
72+
return dfs(head, head, root);
73+
}
74+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-12-30 15:00:35
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-12-30 15:13:58
6+
'''
7+
from typing import Optional
8+
9+
# Definition for singly-linked list.
10+
class ListNode:
11+
def __init__(self, val=0, next=None):
12+
self.val = val
13+
self.next = next
14+
15+
# Definition for a binary tree node.
16+
class TreeNode:
17+
def __init__(self, val=0, left=None, right=None):
18+
self.val = val
19+
self.left = left
20+
self.right = right
21+
22+
class Solution:
23+
def dfs(self, l: Optional[ListNode], r: Optional[TreeNode]) -> bool:
24+
if not l:
25+
return True
26+
if not r:
27+
return False
28+
if l.val == r.val:
29+
if self.dfs(l.next, r.left) or self.dfs(l.next, r.right):
30+
return True
31+
if l == self.head:
32+
if self.dfs(l, r.left) or self.dfs(l, r.right):
33+
return True
34+
return False
35+
36+
def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool:
37+
self.head = head
38+
return self.dfs(head, root)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-30 14:37:32
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-30 14:37:36
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
ListNode* head;
14+
15+
bool dfs(ListNode* l, TreeNode* t) {
16+
if (!l) {
17+
return true;
18+
}
19+
if (!t) {
20+
return false;
21+
}
22+
if (l->val == t->val) {
23+
if (dfs(l->next, t->left) || dfs(l->next, t->right)) {
24+
return true;
25+
}
26+
}
27+
if (dfs(head, t->left) || dfs(head, t->right)) {
28+
return true;
29+
}
30+
return false;
31+
}
32+
public:
33+
bool isSubPath(ListNode* head, TreeNode* root) {
34+
this->head = head;
35+
return dfs(head, root);
36+
}
37+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-30 15:14:35
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-30 15:42:21
6+
*/
7+
/**
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode() {}
13+
* ListNode(int val) { this.val = val; }
14+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
15+
* }
16+
*/
17+
/**
18+
* Definition for a binary tree node.
19+
* public class TreeNode {
20+
* int val;
21+
* TreeNode left;
22+
* TreeNode right;
23+
* TreeNode() {}
24+
* TreeNode(int val) { this.val = val; }
25+
* TreeNode(int val, TreeNode left, TreeNode right) {
26+
* this.val = val;
27+
* this.left = left;
28+
* this.right = right;
29+
* }
30+
* }
31+
*/
32+
class Solution {
33+
private ListNode head;
34+
35+
36+
37+
public boolean isSubPath(ListNode head, TreeNode root) {
38+
this.head = head;
39+
return dfs(head, root);
40+
}
41+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-12-30 14:08:08
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-12-30 14:11:54
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
ListNode* head;
14+
15+
bool dfs(ListNode* l, TreeNode* t) {
16+
if (!l) {
17+
return true;
18+
}
19+
if (!t) {
20+
return false;
21+
}
22+
if (l->val == t->val) {
23+
if (dfs(l->next, t->left) || dfs(l->next, t->right)) {
24+
return true;
25+
}
26+
}
27+
if (l == head) {
28+
if (dfs(l, t->left) || dfs(l, t->right)) {
29+
return true;
30+
}
31+
}
32+
return false;
33+
}
34+
public:
35+
bool isSubPath(ListNode* head, TreeNode* root) {
36+
this->head = head;
37+
return dfs(head, root);
38+
}
39+
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2022-05-19 18:48:53
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2024-12-28 22:15:10
5+
* @LastEditTime: 2024-12-30 13:24:58
66
-->
77
# LetLeet Blog
88

@@ -378,6 +378,7 @@
378378
|1338.数组大小减半|中等|<a href="https://leetcode.cn/problems/reduce-array-size-to-the-half/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/15/LeetCode%201338.%E6%95%B0%E7%BB%84%E5%A4%A7%E5%B0%8F%E5%87%8F%E5%8D%8A/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144487651" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reduce-array-size-to-the-half/solutions/3020818/letmefly-1338shu-zu-da-xiao-jian-ban-tan-4985/" target="_blank">LeetCode题解</a>|
379379
|1349.参加考试的最大学生数|困难|<a href="https://leetcode.cn/problems/maximum-students-taking-exam/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/26/LeetCode%201349.%E5%8F%82%E5%8A%A0%E8%80%83%E8%AF%95%E7%9A%84%E6%9C%80%E5%A4%A7%E5%AD%A6%E7%94%9F%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135217045" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-students-taking-exam/solutions/2580213/letmefly-1349can-jia-kao-shi-de-zui-da-x-g7io/" target="_blank">LeetCode题解</a>|
380380
|1366.通过投票对团队排名|中等|<a href="https://leetcode.cn/problems/rank-teams-by-votes/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/30/LeetCode%201366.%E9%80%9A%E8%BF%87%E6%8A%95%E7%A5%A8%E5%AF%B9%E5%9B%A2%E9%98%9F%E6%8E%92%E5%90%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144814103" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/rank-teams-by-votes/solutions/3033962/letmefly-1366tong-guo-tou-piao-dui-tuan-ssklk/" target="_blank">LeetCode题解</a>|
381+
|1367.二叉树中的链表|中等|<a href="https://leetcode.cn/problems/linked-list-in-binary-tree/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/30/LeetCode%201367.%E4%BA%8C%E5%8F%89%E6%A0%91%E4%B8%AD%E7%9A%84%E9%93%BE%E8%A1%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144826418" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/linked-list-in-binary-tree/solutions/3034359/letmefly-1367er-cha-shu-zhong-de-lian-bi-7zmc/" target="_blank">LeetCode题解</a>|
381382
|1373.二叉搜索子树的最大键值和|困难|<a href="https://leetcode.cn/problems/maximum-sum-bst-in-binary-tree/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/05/20/LeetCode%201373.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E5%AD%90%E6%A0%91%E7%9A%84%E6%9C%80%E5%A4%A7%E9%94%AE%E5%80%BC%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/130779067" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-sum-bst-in-binary-tree/solutions/2276880/letmefly-1373er-cha-sou-suo-zi-shu-de-zu-kldz/" target="_blank">LeetCode题解</a>|
382383
|1374.生成每种字符都是奇数个的字符串|简单|<a href="https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/01/LeetCode%201374.%E7%94%9F%E6%88%90%E6%AF%8F%E7%A7%8D%E5%AD%97%E7%AC%A6%E9%83%BD%E6%98%AF%E5%A5%87%E6%95%B0%E4%B8%AA%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126093645" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts/solution/letmefly-1374sheng-cheng-mei-chong-zi-fu-352y/" target="_blank">LeetCode题解</a>|
383384
|1375.二进制字符串前缀一致的次数|中等|<a href="https://leetcode.cn/problems/number-of-times-binary-string-is-prefix-aligned/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/06/14/LeetCode%201375.%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%89%8D%E7%BC%80%E4%B8%80%E8%87%B4%E7%9A%84%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131213418" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-times-binary-string-is-prefix-aligned/solutions/2309139/letmefly-1375er-jin-zhi-zi-fu-chuan-qian-odu7/" target="_blank">LeetCode题解</a>|
@@ -927,6 +928,7 @@
927928
- [ ] Readme(尤其是文章列表部分)自动生成而非半自动或手动输修改
928929
- [ ] 有空玩下[这个](https://github.com/LetMeFly666/ViT-MGI/commit/df2255f07aa318d55f44da262315789a15f0f2fc)
929930
- [ ] arknights主题不支持mermaid的渲染
931+
- [ ] 研究DQT的[](https://github.com/LetMeFly666/LeetCode/tree/f14c448bc54f4efc3fa41b1d691d5e58a629353f/Codes/1366-rank-teams-by-votes_DQT-RE_version.cpp)[](1366-rank-teams-by-votes_DQT-RE_version.modifing.cpp)为何RE
930932
- hexo我是一刻也待不下去了
931933
- [x] hexo代码中默认Tab是8空格长!
932934
- [x] hexo不支持“- [ ] xxx”、“- [x] xxx”这种代办列表格式

Solutions/LeetCode 1366.通过投票对团队排名.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,42 @@ class Solution {
177177
}
178178
```
179179

180+
#### Go
181+
182+
```go
183+
/*
184+
* @Author: LetMeFly
185+
* @Date: 2024-12-30 09:20:50
186+
* @LastEditors: LetMeFly.xyz
187+
* @LastEditTime: 2024-12-30 13:20:36
188+
*/
189+
package main
190+
import "sort"
191+
192+
func rankTeams(votes []string) string {
193+
counts := make(map[byte][]int)
194+
for _, c := range votes[0] {
195+
counts[byte(c)] = make([]int, len(votes[0]))
196+
}
197+
for _, vote := range votes {
198+
for i, v := range vote {
199+
counts[byte(v)][i]++
200+
}
201+
}
202+
ans := []byte(votes[0])
203+
sort.Slice(ans, func(a, b int) bool {
204+
countA, countB := counts[ans[a]], counts[ans[b]]
205+
for i := range ans {
206+
if countA[i] != countB[i] {
207+
return countA[i] > countB[i]
208+
}
209+
}
210+
return ans[a] < ans[b]
211+
})
212+
return string(ans)
213+
}
214+
```
215+
180216
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/12/30/LeetCode%201366.%E9%80%9A%E8%BF%87%E6%8A%95%E7%A5%A8%E5%AF%B9%E5%9B%A2%E9%98%9F%E6%8E%92%E5%90%8D/)哦~
181217
>
182218
> Tisfy:[https://letmefly.blog.csdn.net/article/details/144814103](https://letmefly.blog.csdn.net/article/details/144814103)

0 commit comments

Comments
 (0)