Skip to content

Commit ce7ee46

Browse files
committed
ok
1 parent 9cc95a0 commit ce7ee46

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/com/leetcode/Main165.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.leetcode;
2+
3+
/**
4+
* 165. 比较版本号
5+
* 比较两个版本号 version1 和 version2。
6+
* 如果 version1 > version2 返回 1,如果 version1 < version2 返回 -1, 除此之外返回 0。
7+
*/
8+
public class Main165 {
9+
public int compareVersion(String version1, String version2) {
10+
String[] num1 = version1.split("\\.");
11+
String[] num2 = version2.split("\\.");
12+
int len1 = num1.length;
13+
int len2 = num2.length;
14+
for (int i = 0; i < Math.max(len1, len2); i++) {
15+
int v1 = i < len1 ? Integer.parseInt(num1[i]) : 0;
16+
int v2 = i < len2 ? Integer.parseInt(num2[i]) : 0;
17+
if (v1 != v2) {
18+
return v1 < v2 ? -1 : 1;
19+
}
20+
}
21+
return 0;
22+
}
23+
}

src/com/leetcode/Main543.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.leetcode;
2+
3+
/**
4+
* 543. 二叉树的直径
5+
* 给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。
6+
*
7+
* 示例 :
8+
* 给定二叉树
9+
*
10+
* 1
11+
* / \
12+
* 2 3
13+
* / \
14+
* 4 5
15+
* 返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。
16+
*/
17+
public class Main543 {
18+
public static class TreeNode {
19+
TreeNode left;
20+
TreeNode right;
21+
int val;
22+
TreeNode(int val) {
23+
this.val = val;
24+
}
25+
}
26+
int res = 0;
27+
public int diameterOfBinaryTree(TreeNode root) {
28+
if (root == null) {
29+
return 0;
30+
}
31+
subLen(root);
32+
return res - 1;
33+
}
34+
public int subLen(TreeNode root) {
35+
if (root == null) {
36+
return 0;
37+
}
38+
int L = subLen(root.left);
39+
int R = subLen(root.right);
40+
res = Math.max(res, L+R+1);
41+
return Math.max(L, R) + 1;
42+
}
43+
}

0 commit comments

Comments
 (0)