Skip to content

Commit 444abd1

Browse files
committed
19-20
1 parent a400b44 commit 444abd1

File tree

7 files changed

+199
-0
lines changed

7 files changed

+199
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ $=link=$: 题目原题链接
8585

8686
17.[N 叉树的层序遍历](https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/)[Java解法](https://github.com/DengBoCong/Algorithm/blob/master/core/NAryTreeLevelOrderTraversal/NAryTreeLevelOrderTraversal.java) | [Python解法](https://github.com/DengBoCong/Algorithm/blob/master/core/NAryTreeLevelOrderTraversal/NAryTreeLevelOrderTraversal.py)):题目难度(中等),题目类型(树_广度优先搜索)
8787

88+
18.[统计最高分的节点数目](https://leetcode-cn.com/problems/count-nodes-with-the-highest-score/)[Java解法](https://github.com/DengBoCong/Algorithm/blob/master/core/CountNodesWithTheHighestScore/CountNodesWithTheHighestScore.java) | [Python解法](https://github.com/DengBoCong/Algorithm/blob/master/core/CountNodesWithTheHighestScore/CountNodesWithTheHighestScore.py)):题目难度(中等),题目类型(树_深度优先搜索_数组_二叉树)
89+
90+
19.[寻找最近的回文数](https://leetcode-cn.com/problems/find-the-closest-palindrome/)[Java解法](https://github.com/DengBoCong/Algorithm/blob/master/core/FindTheClosestPalindrome/FindTheClosestPalindrome.java) | [Python解法](https://github.com/DengBoCong/Algorithm/blob/master/core/FindTheClosestPalindrome/FindTheClosestPalindrome.py)):题目难度(困难),题目类型(数学_字符串)
91+
92+
20.[Z 字形变换](https://leetcode-cn.com/problems/zigzag-conversion/)[Java解法](https://github.com/DengBoCong/Algorithm/blob/master/core/ZigzagConversion/ZigzagConversion.java) | [Python解法](https://github.com/DengBoCong/Algorithm/blob/master/core/ZigzagConversion/ZigzagConversion.py)):题目难度(中等),题目类型(字符串)
93+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.dbc;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class CountNodesWithTheHighestScore {
7+
private long maxScores = 0;
8+
private int count = 0;
9+
private List<List<Integer>> children = null;
10+
private int length = 0;
11+
12+
private int dfs(int node) {
13+
long scores = 1;
14+
int size = this.length - 1;
15+
for (int i = 0; i < this.children.get(node).size(); i++) {
16+
int sz = dfs(this.children.get(node).get(i));
17+
scores *= sz;
18+
size -= sz;
19+
}
20+
if (node != 0) {
21+
scores *= size;
22+
}
23+
if (scores > this.maxScores) {
24+
this.maxScores = scores;
25+
this.count = 1;
26+
} else if (scores == this.maxScores) {
27+
this.count++;
28+
}
29+
return this.length - size;
30+
}
31+
32+
public int countHighestScoreNodes(int[] parents) {
33+
this.length = parents.length;
34+
this.children = new ArrayList<>();
35+
for (int i = 0; i < parents.length; i++) {
36+
this.children.add(new ArrayList<>());
37+
}
38+
for (int i = 1; i < parents.length; i++) {
39+
this.children.get(parents[i]).add(i);
40+
}
41+
42+
dfs(0);
43+
return this.count;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import List
2+
3+
4+
def countHighestScoreNodes(parents: List[int]) -> int:
5+
n = len(parents)
6+
children = [[] for _ in range(n)]
7+
for index in range(1, n):
8+
children[parents[index]].append(index)
9+
10+
max_scores, count = 0, 0
11+
12+
def dfs(root):
13+
size, scores = n - 1, 1
14+
for child in children[root]:
15+
sz = dfs(child)
16+
scores *= sz
17+
size -= sz
18+
if root != 0:
19+
scores *= size
20+
nonlocal max_scores, count
21+
if scores > max_scores:
22+
count = 1
23+
max_scores = scores
24+
elif scores == max_scores:
25+
count += 1
26+
27+
return n - size
28+
29+
dfs(0)
30+
return count
31+
32+
33+
if __name__ == '__main__':
34+
print(countHighestScoreNodes([-1, 2, 0, 2, 0]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.dbc;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class FindTheClosestPalindrome {
7+
public String nearestPalindromic(String n) {
8+
int length = n.length();
9+
long int_n = Long.parseLong(n);
10+
long prefix = Long.parseLong(n.substring(0, (length + 1) / 2));
11+
List<Long> candidate = new ArrayList<>();
12+
candidate.add((long) (Math.pow(10, (length - 1)) - 1));
13+
candidate.add((long) (Math.pow(10, length) + 1));
14+
for (long i = prefix - 1; i < prefix + 2; i++) {
15+
long y = length % 2 == 0 ? i : i / 10;
16+
long x = i;
17+
while (y != 0) {
18+
x = x * 10 + y % 10;
19+
y /= 10;
20+
}
21+
candidate.add(x);
22+
}
23+
24+
long res = -1;
25+
for (long candi : candidate) {
26+
if (candi - int_n != 0) {
27+
if (res == -1 || Math.abs(int_n - candi) < Math.abs(int_n - res)) {
28+
res = candi;
29+
}
30+
}
31+
}
32+
return Long.toString(res);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def nearestPalindromic(n: str) -> str:
2+
left, right, temp, length = 0, len(n) - 1, list(n), len(n)
3+
int_n = int(n)
4+
candidate = [10 ** (length - 1) - 1, 10 ** length + 1]
5+
self_prefix = int(n[:(length + 1) // 2])
6+
for prefix in range(self_prefix - 1, self_prefix + 2):
7+
y = prefix if length % 2 == 0 else prefix // 10
8+
while y != 0:
9+
prefix = prefix * 10 + y % 10
10+
y //= 10
11+
candidate.append(prefix)
12+
13+
res = -1
14+
for candi in candidate:
15+
if str(candi) != n:
16+
if res == -1 or abs(int_n - candi) < abs(int_n - res):
17+
res = candi
18+
19+
return str(res)
20+
21+
22+
if __name__ == '__main__':
23+
print(nearestPalindromic("1283"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.dbc;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ZigzagConversion {
7+
public static String convert(String s, int numRows) {
8+
List<Character>[] resMatrix = new List[numRows];
9+
for (int i = 0; i < numRows; i++) {
10+
resMatrix[i] = new ArrayList<>();
11+
}
12+
int curPoint = 0, flag = 0;
13+
for (int i = 0; i < s.length(); i++) {
14+
resMatrix[curPoint].add(s.charAt(i));
15+
if (flag == 0 && curPoint + 1 < numRows) {
16+
curPoint++;
17+
} else if (flag == 0 && curPoint - 1 >= 0) {
18+
curPoint--;
19+
flag = 1;
20+
} else if (curPoint - 1 >= 0) {
21+
curPoint--;
22+
} else if (curPoint + 1 < numRows) {
23+
curPoint++;
24+
flag = 0;
25+
}
26+
}
27+
28+
StringBuffer res = new StringBuffer();
29+
for (List<Character> chList : resMatrix) {
30+
for (Character ch : chList) {
31+
res.append(ch);
32+
}
33+
}
34+
return res.toString();
35+
}
36+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def convert(s: str, numRows: int) -> str:
2+
res_matrix = [[] for _ in range(numRows)]
3+
cur_point, flag = 0, "down"
4+
for ch in s:
5+
res_matrix[cur_point].append(ch)
6+
if flag == "down" and cur_point + 1 < numRows:
7+
cur_point += 1
8+
elif flag == "down":
9+
cur_point -= 1
10+
flag = "up"
11+
elif flag == "up" and cur_point - 1 >= 0:
12+
cur_point -= 1
13+
else:
14+
cur_point += 1
15+
flag = "down"
16+
17+
return "".join(["".join(item) for item in res_matrix])
18+
19+
20+
if __name__ == '__main__':
21+
print(convert(s="AB", numRows=1))

0 commit comments

Comments
 (0)