Leetcode 520. Detect Capital (python+cpp)
程序员文章站
2022-05-15 14:05:40
...
题目
解法:
只需要注意一下python里面is_upper的用法即可。C++里面直接进行比较就好
python版本
class Solution:
def detectCapitalUse(self, word: str) -> bool:
capital_pos = []
for i,c in enumerate(word):
if c.isupper():
capital_pos.append(i)
if len(capital_pos) == 0:
return True
elif len(capital_pos) == 1:
return capital_pos[0] == 0
else:
return len(capital_pos)==len(word)
C++版本
class Solution {
public:
bool detectCapitalUse(string word) {
vector<int> capital_pos;
for (int i=0;i<word.size();i++){
if (word[i]>='A' and word[i]<='Z'){
capital_pos.push_back(i);
}
}
if (capital_pos.size()==0){
return true;
}
else if (capital_pos.size()==1){
return capital_pos[0]==0;
}
else{
return capital_pos.size() == word.size();
}
}
};
推荐阅读
-
LeetCode - 520 - 检测大写字母(detect-capital)
-
Java/520. Detect Capital 检测大写字母
-
[高级编程技术作业]LeetCode Problem 520. Detect Capital
-
Leetcode 520. Detect Capital (python+cpp)
-
Leetcode PHP题解--D81 520. Detect Capital
-
Leetcode PHP题解--D81 520. Detect Capital
-
520. Detect Capital
-
String:520. Detect Capital
-
LeetCode--Python解析【Detect Capital】(520)
-
【String-easy】520. Detect Capital 检查是否是Capital