Skip to content

add 0520,0521,0522 solution for java #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions solution/0500-0599/0520.Detect Capital/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public boolean detectCapitalUse(String word) {
char[] cs = word.toCharArray();
int upper = 0;
int lower = 0;
for (int i = 0; i < cs.length; i++) {
if (cs[i] >= 'a') {
lower++;
} else {
upper++;
}
}
if (upper == cs.length) {
return true;
}
if (lower == cs.length) {
return true;
}
if (upper == 1 && cs[0] < 'a') {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution {
public int findLUSlength(String a, String b) {
if (a.equals(b))
return -1;
return Math.max(a.length(), b.length());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public int findLUSlength(String[] strs) {
int res = -1;
if (strs == null || strs.length == 0) {
return res;
}
if (strs.length == 1) {
return strs[0].length();
}
// 两两比较
// 1、存在子串,直接不比较后面的字符串
// 2、不存在子串,判断当前字符串是否是最长的字符串
for (int i = 0, j; i < strs.length; i++) {
for (j = 0; j < strs.length; j++) {
if (i == j) {
continue;
}
// 根据题意,子串 可以 不是 原字符串中 连续的子字符串
if (isSubsequence(strs[i], strs[j])) {
break;
}
}
if (j == strs.length) {
res = Math.max(res, strs[i].length());
}
}
return res;
}

public boolean isSubsequence(String x, String y) {
int j = 0;
for (int i = 0; i < y.length() && j < x.length(); i++) {
if (x.charAt(j) == y.charAt(i))
j++;
}
return j == x.length();
}
}