-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetect_Capital.java
More file actions
82 lines (71 loc) · 2.21 KB
/
Detect_Capital.java
File metadata and controls
82 lines (71 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.leetcode;
/*
Given a word, you need to judge whether the usage of capitals in it is right or not.
We define the usage of capitals in a word to be right when one of the following cases holds:
All letters in this word are capitals, like "USA".
All letters in this word are not capitals, like "leetcode".
Only the first letter in this word is capital if it has more than one letter, like "Google".
Otherwise, we define that this word doesn't use capitals in a right way.
Example 1:
Input: "USA"
Output: True
Example 2:
Input: "FlaG"
Output: False*/
public class Detect_Capital {
/*
* word 1.第一个字母是小写时,后边全是小写,则true.
* 2.@return 大写时,后边大小写一一致(全为大写或者全为小写),则true.
*
*/
public boolean detectCapitalUse(String word) {
// word len的长度 = 1
int len = word.length();
if(len == 1) return true;
int lower = ('a'<=word.charAt(0))?1:0;
boolean b = false;
// if(lower == 1){
// b = allUpperOrLower(0,len-1,word);
// }
//
// if(lower == 0){
// b = allUpperOrLower(1,len-1,word);
// }
b = allUpperOrLower(1-lower,len-1,word);
return b;
}
/**
*
* @param start 开始索引
* @param end 结束索引
* @param s 判断的字符串
* @return true:s 在[start,end]所有字符都是大写,或者都是小写
* @return false:字符串区间的大小写不一致
*
*/
public boolean allUpperOrLower(int start ,int end , String s){
boolean res = true;
int upper = (s.charAt(start)>='A'&&s.charAt(start)<='Z')? 1:0;
if(upper == 1){
for (int k = start;k<=end ;k++ ) {
if(s.charAt(k) >= 'a') {
res = false;
break;
}
}
}
if(upper == 0){
for (int k = start;k<=end ;k++ ) {
if(s.charAt(k) <= 'Z') {
res = false;
break;
}
}
}
return res;
}
public static void main(String[] args) {
System.out.println(new Detect_Capital().detectCapitalUse("Gkljhdlkjfkl"));
//System.out.println();
}
}