【LeetCode】520 Detect Capital
来源:互联网 发布:java主要用来做什么 编辑:程序博客网 时间:2024/05/19 21:16
原题地址:520. Detect Capital 。
题目大意
判断一个单词的大小写运用是否正确。全部大写、全部小写和首字母大写是正确的写法。
解法一
分三种情况:
- 首字母小写,如果之后有一个大写字母,则返回false。
- 首字母大写,如果第二个字母小写,则将
type
复制为1,其后如果有大写字母,则返回false。 - 首字母大写,如果第二个字母大写,则将
type
复制为2,其后如果有小写字母,则返回false。
bool detectCapitalUse(string word) { bool isFirstUpper = isupper(word[0]); int type = 0; for (int i = 1; i < word.length(); i++) { if (!isFirstUpper && isupper(word[i])) { return false; } else if (isFirstUpper && islower(word[i])) { // "Google" if (type == 0 || type == 1) type = 1; else return false; } else if (isFirstUpper && isupper(word[i])) { // "USA" if (type == 0 || type == 2) type = 2; else return false; } } return true;}
解法二
分两种情况:
- 全部小写与首字母大写分为一种,其后如果有大写字母则返回false。
- 全部大写为一种情况,其后如果有小写字母则返回false。
bool detectCapitalUse2(string word) { if (word.length() <= 1) return true; if (islower(word[0]) || isupper(word[0]) && islower(word[1])) { for (int i = 1; i < word.length(); i++) { if (isupper(word[i])) return false; } } else { for (int i = 1; i < word.length(); i++) { if (islower(word[i])) return false; } } return true;}
参考:
- LeetCode 520. Detect Capital
阅读全文
0 0
- 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
- LeetCode 520:Detect Capital (c++)
- LeetCode 520 Detect Capital 检测大写
- 【Leetcode】520. Detect Capital
- LeetCode 520. Detect Capital
- LeetCode 520. Detect Capital
- LeetCode:Detect Capital
- bzoj 1027: [JSOI2007]合金(floyd最小环)
- Java线程死锁
- springmvc、jpa(hibernate)、mybatis组合框架搭建(三)——加入JPA、单元测试
- SqlDataReader和SqlDataAdapter
- C++ 判断一个数是否在int的取值范围内
- 【LeetCode】520 Detect Capital
- Java实现冒泡排序
- JSON:JS对象表示法
- html javascript原型(二)
- 排序算法之冒泡排序
- 使用HTML+CSS实现如图布局,border-width:5px,格子大小是50px*50px,hover时 边框变成红色,需要考虑语义化。
- IO流——LineNumberReader
- 计算累加和、累积和、日期的定义及其使用
- numpy ndarray 与 array