LeetCode 520:Detect Capital (c++)
来源:互联网 发布:js当前时间减一小时 编辑:程序博客网 时间:2024/05/29 09:12
一:题目
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".
Example 1:
Input: "USA"Output: True
Example 2:
Input: "FlaG"Output: False
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
二:解题分析
判断一个单词的字母大写使用是否正确
1.所有字母都大写
2.所有字母都小写
3.首字母大写,其余都小写
4种情况:
1.如果字符串长度小于等于1,返回true;
2.如果第一个位置小写,以后出现大写,返回false
3.如果第一个位置大写,第二个位置大写,以后出现小写,返回false
4.如果第一个位置大写,第二个位置小写,以后出现大写,返回false
三:代码实现
思路一:
class Solution {public: bool detectCapitalUse(string word) { if(word.length()<=1) return true; int i; bool first=false; //true:第一个位置大写,false:第一个位置小写 bool second=false; //true:第二个位置是大写, false:第二个位置小写 if(word[0]>='A' && word[0]<='Z') first=true; if(word[1]>='A' && word[1]<='Z') second=true; //如果第一个位置小写,以后出现大写,返回false if(!first && second) return false; for(i=2;i<word.length();i++){ //1.如果第一个位置小写,以后出现大写,返回false if(!first && !second && word[i]>='A' && word[i]<='Z') return false; //2.如果第一个位置大写,第二个位置大写,以后出现小写,返回false if(first && second && word[i]>='a' && word[i]<='z') return false; //3.如果第一个位置大写,第二个位置小写,以后出现大写,返回false if(first && !second && word[i]>='A' && word[i]<='Z') return false; } return true; }};
思路二:
统计大写字母的个数
1.如果全为大写,最终大写字母的个数应该等于字符串的长度
2.如果首字母大写,其余为小写,则大写字母的个数为1
3.全为小写,大写字母个数为0
class Solution {public: bool detectCapitalUse(string word) { int i; int numOfCapital=0; for(i=0;i<word.length();i++) if(word[i]>='A' && word[i]<='Z') numOfCapital++; //全为小写 if(numOfCapital==0) return true; //全为大写 if(numOfCapital==word.length()) return true; //首字母大写,其余小写 if(word[0]>='A' && word[0]<='Z' && numOfCapital==1) return true; return false; }};
思路三(JAVA):
将单词转换为大写得到up,将单词转换为小写得到low,若word与up或与low相等,则返回true,
否则去掉word的首字母得到last,若last转换为小写后仍与last相等,则返回true,
否则返回false。
public boolean detectCapitalUse(String word) { int len=word.length(); String up = word.toUpperCase(); String low = word.toLowerCase(); if (word.equals(up) || word.equals(low)) return true; String last = word.substring(1, len); if (last.toLowerCase().equals(last)) return true; return false; }
阅读全文
0 0
- LeetCode 520:Detect Capital (c++)
- leetcode 520. Detect Capital(C语言)
- leetcode-520-Detect Capital
- Leetcode #520 Detect Capital
- [LeetCode]520 Detect Capital
- LeetCode 520:Detect Capital
- LeetCode 520 Detect Capital
- Leetcode 520 Detect Capital
- LeetCode 520 Detect Capital
- 【LeetCode】520 Detect Capital
- Leetcode 520 Detect Capital
- LeetCode.520 Detect Capital
- leetcode 520 Detect Capital C++
- leetcode 520[easy]---Detect Capital
- 520. Detect Capital(C++)
- LeetCode 520 Detect Capital 检测大写
- LeetCode 520. Detect Capital (字符串)
- 【Leetcode】520. Detect Capital
- Spring 配置Thymeleaf时出现ServletContextTemplateResolver no Matching constructor.....
- 初步理解组策略,Active Directory系列之二十一
- win7和win10下安装caffe和tensorflow的过程
- Oracle存储过程
- 跳跃表
- LeetCode 520:Detect Capital (c++)
- 如何通过源码修改手机型号等信息
- thinkphp单独连接一个数据库
- 设计模式之模板方法模式
- instanceof方法
- cocos compile 时提示ant 不是内部命令也不是外部命令
- SQL-创建表
- 组策略轻松实现软件发布,Active Directory系列之二十二
- MATLAB学习笔记 彩色图像处理