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 ...
随机推荐
- 在asp.net页面上得到Castle容器的实例
在项目中使用Castle IOC容器,Asp.net程序中如何得到Castle容器内. 可以如下实现: 1.Gloabal实现接口IContainerAccessor public class Glo ...
- SPOJ OPTM - Optimal Marks
OPTM - Optimal Marks no tags You are given an undirected graph G(V, E). Each vertex has a mark whic ...
- xmpp muc 群聊协议 3
6. Entity Use Cases A MUC implementation MUST support Service Discovery [7]. 服务端必须实现 service discove ...
- delphi xe----操作mongoDB驱动,TMongoWire(Delphi MongoDB Driver)
所有例子来自:https://github.com/stijnsanders/TMongoWire Delphi MongoDB的驱动 一个Delphi的驱动程序来访问mongoDB的服务器.用jso ...
- table width 决定 td width
w td width 有无在chrome edge ff 均未影响td实际宽度,td接近等比分配table width. <!doctype html> <html lang=&qu ...
- css3的clip-path属性
css3的clip-path属性 网上看到的都是因为2年前一个出名的网站引发了对该属性的研究.所以大概是2年前火了一阵子的属性.2016-09-10 23:54:00 直接开始总结它的用法: 2个基 ...
- buffer/interger overflow /return-to-libc攻击实验
buffer/interger overflow /return-to-libc攻击实验 http://blog.sina.com.cn/s/blog_70dd16910100rdgn.html ht ...
- Hibernate 中一对多和多对多映射
1. 一对多映射 1.1 JavaWeb 一对多建表原则 多方表的外键指向一方表的主键; 1.2 编写一对多的 JavaBean // 客户(一方)和联系人(多方) // 客户(一方) JavaBea ...
- requests 中response如何改变编码格式
查看初始编码 首先查看拿到的response编码格式: (就不放代码了,因为此例需要用到cookie,可自行找个网站具体测试) 可见初始编码为:ISO-8859-1 修改编码 初始编码: 修改后编码: ...
- threading模块、ThreadLocal
一.threading模块 1.线程对象的创建 1.1 Thread类直接创建 import threading import time def countNum(n): # 定义某个线程要运行的函数 ...