简单的string问题解决:Detect Capital
来源:互联网 发布:怎么打理淘宝店铺 编辑:程序博客网 时间:2024/05/21 17:13
直接列出题目如下:
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".
只要单词是正确的格式即可,且题目也已确认不会输入其他字符,所以对输入的单词每个字母进行遍历。代码如下:
bool detectCapitalUse(string word) {
if (word[0] >= 'A' && word[0] <= 'Z') {
if (word.length() > 1) {
if (word[1] > 'Z') {
for (int k = 2; k < word.length(); k++) {
if (word[k] <= 'Z')
return false;
}
}
else if(word[1] <= 'Z') {
for (int k = 2; k < word.length(); k++) {
if (word[k] > 'Z')
return false;
}
}
}
}
else if (word[0] >= 'a' && word[0] <= 'z') {
for (int i = 0; i < word.length(); i++) {
if (word[i] < 'a' || word[i] > 'z')
return false;
}
}
return true;
}
if (word[0] >= 'A' && word[0] <= 'Z') {
if (word.length() > 1) {
if (word[1] > 'Z') {
for (int k = 2; k < word.length(); k++) {
if (word[k] <= 'Z')
return false;
}
}
else if(word[1] <= 'Z') {
for (int k = 2; k < word.length(); k++) {
if (word[k] > 'Z')
return false;
}
}
}
}
else if (word[0] >= 'a' && word[0] <= 'z') {
for (int i = 0; i < word.length(); i++) {
if (word[i] < 'a' || word[i] > 'z')
return false;
}
}
return true;
}
0 0
- 简单的string问题解决:Detect Capital
- String:520. Detect Capital
- 520.Detect Capital(String-Easy)
- Detect Capital
- Detect Capital
- Detect Capital
- Detect Capital
- Detect Capital
- Detect Capital
- Detect Capital
- 520. Detect Capital 的OJ代码笔记
- LeetCode 520. Detect Capital 自己的解法
- LeetCode-Easy部分中标签为String 520. Detect Capital
- 【Leetcode】520. Detect Capital
- 520. Detect Capital (E)
- LeetCode 520. Detect Capital
- leetcode-520-Detect Capital
- LeetCode 520. Detect Capital
- (大数相加)HDU 1002 A + B Problem II
- 历届试题 波动数列
- Glide、Picasso与与Fresco要点
- (C51学习五)单片机与PC通过串口通信
- 【C++】Keans聚类算法的C++实现
- 简单的string问题解决:Detect Capital
- java懒加载异常的解决
- Longest Palindromic Substring (第一周)
- 软件测试高级问题
- Python 爬虫教程 4
- 三种遍历:for,for-Each,Iterator
- Java正则表达式-2
- AIDL使用小结
- 修改Oracle重做日志文件大小