Skip to content

Commit 4f22633

Browse files
committed
feat: add solutions to lc problem: No.0520
No.0520.Detect Capital
1 parent b28600f commit 4f22633

File tree

6 files changed

+121
-44
lines changed

6 files changed

+121
-44
lines changed

solution/0500-0599/0520.Detect Capital/README.md

+45-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
<p><strong>注意:</strong> 输入是由大写和小写拉丁字母组成的非空单词。</p>
3636

37-
3837
## 解法
3938

4039
<!-- 这里可写通用的实现逻辑 -->
@@ -46,15 +45,59 @@
4645
<!-- 这里可写当前语言的特殊实现逻辑 -->
4746

4847
```python
49-
48+
class Solution:
49+
def detectCapitalUse(self, word: str) -> bool:
50+
cnt = 0
51+
for c in word:
52+
if c.isupper():
53+
cnt += 1
54+
return cnt == 0 or cnt == len(word) or (cnt == 1 and word[0].isupper())
5055
```
5156

5257
### **Java**
5358

5459
<!-- 这里可写当前语言的特殊实现逻辑 -->
5560

5661
```java
62+
class Solution {
63+
public boolean detectCapitalUse(String word) {
64+
int cnt = 0;
65+
for (char c : word.toCharArray()) {
66+
if (Character.isUpperCase(c)) {
67+
++cnt;
68+
}
69+
}
70+
return cnt == 0 || cnt == word.length() || (cnt == 1 && Character.isUpperCase(word.charAt(0)));
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
bool detectCapitalUse(string word) {
81+
int cnt = 0;
82+
for (char c : word)
83+
if (isupper(c)) ++cnt;
84+
return cnt == 0 || cnt == word.size() || (cnt == 1 && isupper(word[0]));
85+
}
86+
};
87+
```
5788
89+
### **Go**
90+
91+
```go
92+
func detectCapitalUse(word string) bool {
93+
cnt := 0
94+
for _, c := range word {
95+
if unicode.IsUpper(c) {
96+
cnt++
97+
}
98+
}
99+
return cnt == 0 || cnt == len(word) || (cnt == 1 && unicode.IsUpper(rune(word[0])))
100+
}
58101
```
59102

60103
### **...**

solution/0500-0599/0520.Detect Capital/README_EN.md

+45-23
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66

77
<p>Given a word, you need to judge whether the usage of capitals in it is right or not.</p>
88

9-
10-
119
<p>We define the usage of capitals in a word to be right when one of the following cases holds:</p>
1210

13-
14-
1511
<ol>
1612
<li>All letters in this word are capitals, like &quot;USA&quot;.</li>
1713
<li>All letters in this word are not capitals, like &quot;leetcode&quot;.</li>
@@ -20,16 +16,10 @@
2016

2117
Otherwise, we define that this word doesn&#39;t use capitals in a right way.
2218

23-
24-
2519
<p>&nbsp;</p>
2620

27-
28-
2921
<p><b>Example 1:</b></p>
3022

31-
32-
3323
<pre>
3424

3525
<b>Input:</b> &quot;USA&quot;
@@ -38,16 +28,10 @@ Otherwise, we define that this word doesn&#39;t use capitals in a right way.
3828

3929
</pre>
4030

41-
42-
4331
<p>&nbsp;</p>
4432

45-
46-
4733
<p><b>Example 2:</b></p>
4834

49-
50-
5135
<pre>
5236

5337
<b>Input:</b> &quot;FlaG&quot;
@@ -56,30 +40,68 @@ Otherwise, we define that this word doesn&#39;t use capitals in a right way.
5640

5741
</pre>
5842

59-
60-
6143
<p>&nbsp;</p>
6244

63-
64-
6545
<p><b>Note:</b> The input will be a non-empty word consisting of uppercase and lowercase latin letters.</p>
6646

67-
68-
6947
## Solutions
7048

7149
<!-- tabs:start -->
7250

7351
### **Python3**
7452

7553
```python
76-
54+
class Solution:
55+
def detectCapitalUse(self, word: str) -> bool:
56+
cnt = 0
57+
for c in word:
58+
if c.isupper():
59+
cnt += 1
60+
return cnt == 0 or cnt == len(word) or (cnt == 1 and word[0].isupper())
7761
```
7862

7963
### **Java**
8064

8165
```java
66+
class Solution {
67+
public boolean detectCapitalUse(String word) {
68+
int cnt = 0;
69+
for (char c : word.toCharArray()) {
70+
if (Character.isUpperCase(c)) {
71+
++cnt;
72+
}
73+
}
74+
return cnt == 0 || cnt == word.length() || (cnt == 1 && Character.isUpperCase(word.charAt(0)));
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
bool detectCapitalUse(string word) {
85+
int cnt = 0;
86+
for (char c : word)
87+
if (isupper(c)) ++cnt;
88+
return cnt == 0 || cnt == word.size() || (cnt == 1 && isupper(word[0]));
89+
}
90+
};
91+
```
8292
93+
### **Go**
94+
95+
```go
96+
func detectCapitalUse(word string) bool {
97+
cnt := 0
98+
for _, c := range word {
99+
if unicode.IsUpper(c) {
100+
cnt++
101+
}
102+
}
103+
return cnt == 0 || cnt == len(word) || (cnt == 1 && unicode.IsUpper(rune(word[0])))
104+
}
83105
```
84106

85107
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
bool detectCapitalUse(string word) {
4+
int cnt = 0;
5+
for (char c : word)
6+
if (isupper(c)) ++cnt;
7+
return cnt == 0 || cnt == word.size() || (cnt == 1 && isupper(word[0]));
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func detectCapitalUse(word string) bool {
2+
cnt := 0
3+
for _, c := range word {
4+
if unicode.IsUpper(c) {
5+
cnt++
6+
}
7+
}
8+
return cnt == 0 || cnt == len(word) || (cnt == 1 && unicode.IsUpper(rune(word[0])))
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
class Solution {
22
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++;
3+
int cnt = 0;
4+
for (char c : word.toCharArray()) {
5+
if (Character.isUpperCase(c)) {
6+
++cnt;
117
}
128
}
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;
9+
return cnt == 0 || cnt == word.length() || (cnt == 1 && Character.isUpperCase(word.charAt(0)));
2310
}
24-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def detectCapitalUse(self, word: str) -> bool:
3+
cnt = 0
4+
for c in word:
5+
if c.isupper():
6+
cnt += 1
7+
return cnt == 0 or cnt == len(word) or (cnt == 1 and word[0].isupper())

0 commit comments

Comments
 (0)