LeetCode——Detect Capital
LeetCode——Detect Capital
Question
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".
Otherwise, we define that this word doesn't use capitals in a right way.
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.
Answer
class Solution {
public:
bool detectCapitalUse(string word) {
if (word.length() == 1)
return true;
char c = word[0];
if (c >= 'a' && c <= 'z') {
for (int i = 1; i < word.length(); i++) {
if (word[i] < 'a' || word[i] > 'z')
return false;
}
return true;
} else {
c = word[1];
if (c >= 'A' && c <= 'Z') {
for (int i = 2; i < word.length(); i++) {
if (word[i] < 'A' || word[i] > 'Z')
return false;
}
return true;
} else {
for (int i = 2; i < word.length(); i++) {
if (word[i] < 'a' || word[i] > 'z')
return false;
}
return true;
}
}
}
};
LeetCode——Detect Capital的更多相关文章
- [LeetCode] Detect Capital 检测大写格式
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- leetCode Detect Capital
1.问题描述 Given a word, you need to judge whether the usage of capitals in it is right or not. We defin ...
- 50. leetcode 520. Detect Capital
520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right or ...
- Leetcode 520. Detect Capital 发现大写词 (字符串)
Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...
- 【leetcode】520. Detect Capital
problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...
- 520. Detect Capital【easy】
520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...
- LeetCode - 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- LeetCode 520 Detect Capital 解题报告
题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...
- [LeetCode&Python] Problem 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
随机推荐
- Android获取 应用程序大小,数据大小,缓存大小
在项目中创建,android.content.pm 包名.里面创建两个aidl文件.PackageStats.aidl 和 IPackageStatsObserver.aidl. PackageSt ...
- python 之 多进程
阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiprocessingpython中的多线程 ...
- JS中:数组和二维数组、MAP、Set和枚举的使用
1.数组和二维数组: 方法一: var names = ['Michael', 'Bob', 'Tracy']; names[0];// 'Michael' 方法二: var mycars=new ...
- Let's encrypt申请泛域名证书以及报错处理
申请泛域名证书的步骤请参考该链接地址: https://www.jianshu.com/p/df6d13187578 报错信息: No matching distribution found for ...
- cocopods
一.什么是CocoaPods 1.为什么需要CocoaPods 在进行iOS开发的时候,总免不了使用第三方的开源库,比如SBJson.AFNetworking.Reachability等等.使用这些库 ...
- Storm-源码分析-Topology Submit-Task
mk-task, 比较简单, 因为task只是概念上的结构, 不象其他worker, executor都需要创建进程或线程 所以其核心其实就是mk-task-data, 1. 创建TopologyCo ...
- [cocos2dx笔记006]流格式日志
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zdhsoft/article/details/36001945 在cocos2dx 2.2.2版本号 ...
- cmd命令行和bat批处理操作windows服务(转载)
一.cmd命令行---进行Windows服务操作 1.安装服务 sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32srv ...
- 浅谈HTML文档模式
不知道爱多想的你有没有在编写HTML代码时思考过 <!DOCTYPE html> 或是这一长串看都看不懂的 <!DOCTYPE HTML PUBLIC "-//W3C//D ...
- tornado项目下路由系统的使用?
路由系统 在web框架中,路由表中的任意一项是一个元组,每个元组包含pattern(模式)和handler(处理器).当httpserver接收到一个http请求,server从接收到的请求中解析出u ...