Skip to content

Commit 9d42a78

Browse files
authoredJun 13, 2020
Merge pull request doocs#266 from HendSame/master
add 0520,0521,0522 solution for java
2 parents 64e0170 + 883ae70 commit 9d42a78

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public boolean detectCapitalUse(String word) {
3+
char[] cs = word.toCharArray();
4+
int upper = 0;
5+
int lower = 0;
6+
for (int i = 0; i < cs.length; i++) {
7+
if (cs[i] >= 'a') {
8+
lower++;
9+
} else {
10+
upper++;
11+
}
12+
}
13+
if (upper == cs.length) {
14+
return true;
15+
}
16+
if (lower == cs.length) {
17+
return true;
18+
}
19+
if (upper == 1 && cs[0] < 'a') {
20+
return true;
21+
}
22+
return false;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public int findLUSlength(String a, String b) {
3+
if (a.equals(b))
4+
return -1;
5+
return Math.max(a.length(), b.length());
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public int findLUSlength(String[] strs) {
3+
int res = -1;
4+
if (strs == null || strs.length == 0) {
5+
return res;
6+
}
7+
if (strs.length == 1) {
8+
return strs[0].length();
9+
}
10+
// 两两比较
11+
// 1、存在子串,直接不比较后面的字符串
12+
// 2、不存在子串,判断当前字符串是否是最长的字符串
13+
for (int i = 0, j; i < strs.length; i++) {
14+
for (j = 0; j < strs.length; j++) {
15+
if (i == j) {
16+
continue;
17+
}
18+
// 根据题意,子串 可以 不是 原字符串中 连续的子字符串
19+
if (isSubsequence(strs[i], strs[j])) {
20+
break;
21+
}
22+
}
23+
if (j == strs.length) {
24+
res = Math.max(res, strs[i].length());
25+
}
26+
}
27+
return res;
28+
}
29+
30+
public boolean isSubsequence(String x, String y) {
31+
int j = 0;
32+
for (int i = 0; i < y.length() && j < x.length(); i++) {
33+
if (x.charAt(j) == y.charAt(i))
34+
j++;
35+
}
36+
return j == x.length();
37+
}
38+
}

0 commit comments

Comments
 (0)
Please sign in to comment.