Skip to content

Commit 8f7dda9

Browse files
committed
solve problem Detect Capital
1 parent 268a830 commit 8f7dda9

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ All solutions will be accepted!
3131
|20|[Valid Parentheses](https://leetcode-cn.com/problems/valid-parentheses/description/)|[java/py/js](./algorithms/ValidParentheses)|Easy|
3232
|485|[Max Consecutive Ones](https://leetcode-cn.com/problems/max-consecutive-ones/description/)|[java/py/js](./algorithms/MaxConsecutiveOnes)|Easy|
3333
|191|[Number Of 1 Bits](https://leetcode-cn.com/problems/number-of-1-bits/description/)|[java/py/js](./algorithms/NumberOf1Bits)|Easy|
34-
|258|[Add Digits](https://leetcode-cn.com/problems/add-digits/description/)|[java/py/js](./algorithms/AddDigits)|Easy|
34+
|258|[Add Digits](https://leetcode-cn.com/problems/add-digits/description/)|[java/py/js](./algorithms/AddDigits)|Easy|
35+
|520|[Detect Capital](https://leetcode-cn.com/problems/detect-capital/description/)|[java/py/js](./algorithms/DetectCaptial)|Easy|

algorithms/DetectCapital/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Detect Capital
2+
This problem is easy to solve
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public boolean detectCapitalUse(String word) {
3+
int upperCount = 0,
4+
lowerCount = 0;
5+
for (char c : word.toCharArray()) {
6+
int v = (int) c;
7+
if (65 <= v && v <= 90) {
8+
if (lowerCount > 0) return false;
9+
upperCount += 1;
10+
} else {
11+
if (upperCount > 1) return false;
12+
lowerCount += 1;
13+
}
14+
}
15+
return true;
16+
}
17+
}

algorithms/DetectCapital/solution.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} word
3+
* @return {boolean}
4+
*/
5+
var detectCapitalUse = function(word) {
6+
let upperCount = 0,
7+
lowerCount = 0,
8+
list = word.split('')
9+
10+
for (let i = 0; i < list.length; i++) {
11+
if (65 <= list[i].charCodeAt(0) && list[i].charCodeAt(0) <= 90) {
12+
if (lowerCount > 0) return false
13+
upperCount += 1
14+
} else {
15+
if (upperCount > 1) return false
16+
lowerCount += 1
17+
}
18+
}
19+
20+
return true
21+
};

algorithms/DetectCapital/solution.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def detectCapitalUse(self, word):
3+
"""
4+
:type word: str
5+
:rtype: bool
6+
"""
7+
upper_count = 0
8+
lower_count = 0
9+
for c in word:
10+
if 65 <= ord(c) <= 90:
11+
if lower_count > 0:
12+
return False
13+
upper_count += 1
14+
else:
15+
if upper_count > 1:
16+
return False
17+
lower_count += 1
18+
return True

0 commit comments

Comments
 (0)