Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2ecd954

Browse files
refactor 520
1 parent4310acb commit2ecd954

File tree

1 file changed

+42
-37
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+42
-37
lines changed
Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,70 @@
11
packagecom.fishercoder.solutions;
22

33
/**
4+
* 520. Detect Capital
5+
*
46
* Given a word, you need to judge whether the usage of capitals in it is right or not.
5-
6-
We define the usage of capitals in a word to be right when one of the following cases holds:
7+
* We define the usage of capitals in a word to be right when one of the following cases holds:
78
89
All letters in this word are capitals, like "USA".
910
All letters in this word are not capitals, like "leetcode".
1011
Only the first letter in this word is capital if it has more than one letter, like "Google".
1112
Otherwise, we define that this word doesn't use capitals in a right way.
13+
1214
Example 1:
1315
Input: "USA"
1416
Output: True
17+
1518
Example 2:
1619
Input: "FlaG"
1720
Output: False
18-
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
1921
22+
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
2023
*/
2124
publicclass_520 {
22-
publicbooleandetectCapitalUse(Stringword) {
23-
char[]words =word.toCharArray();
24-
booleanfirstLetterCap =false;
25-
if (Character.isUpperCase(words[0])) {
26-
firstLetterCap =true;
27-
}
25+
publicstaticclassSolution1 {
26+
publicbooleandetectCapitalUse(Stringword) {
27+
char[]words =word.toCharArray();
28+
booleanfirstLetterCap =false;
29+
if (Character.isUpperCase(words[0])) {
30+
firstLetterCap =true;
31+
}
2832

29-
if (firstLetterCap) {
30-
if (words.length >=2) {
31-
inti =2;
32-
if (Character.isUpperCase(words[1])) {
33-
//then all following must be all uppercase
34-
while (i <words.length) {
35-
if (!Character.isUpperCase(words[i])) {
36-
returnfalse;
33+
if (firstLetterCap) {
34+
if (words.length >=2) {
35+
inti =2;
36+
if (Character.isUpperCase(words[1])) {
37+
//then all following must be all uppercase
38+
while (i <words.length) {
39+
if (!Character.isUpperCase(words[i])) {
40+
returnfalse;
41+
}
42+
i++;
3743
}
38-
i++;
39-
}
40-
returntrue;
41-
}else {
42-
//then all following must be all lowercase
43-
while (i <words.length) {
44-
if (!Character.isLowerCase(words[i])) {
45-
returnfalse;
44+
returntrue;
45+
}else {
46+
//then all following must be all lowercase
47+
while (i <words.length) {
48+
if (!Character.isLowerCase(words[i])) {
49+
returnfalse;
50+
}
51+
i++;
4652
}
47-
i++;
53+
returntrue;
4854
}
49-
returntrue;
5055
}
51-
}
52-
returntrue;
53-
}else {
54-
//then all following must be all lowercase
55-
inti =1;
56-
while (i <words.length) {
57-
if (!Character.isLowerCase(words[i])) {
58-
returnfalse;
56+
returntrue;
57+
}else {
58+
//then all following must be all lowercase
59+
inti =1;
60+
while (i <words.length) {
61+
if (!Character.isLowerCase(words[i])) {
62+
returnfalse;
63+
}
64+
i++;
5965
}
60-
i++;
66+
returntrue;
6167
}
62-
returntrue;
6368
}
6469
}
6570
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp