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的更多相关文章

  1. [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 ...

  2. leetCode Detect Capital

    1.问题描述 Given a word, you need to judge whether the usage of capitals in it is right or not. We defin ...

  3. 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 ...

  4. Leetcode 520. Detect Capital 发现大写词 (字符串)

    Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...

  5. 【leetcode】520. Detect Capital

    problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...

  6. 520. Detect Capital【easy】

    520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...

  7. 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 ...

  8. LeetCode 520 Detect Capital 解题报告

    题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...

  9. [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 ...

随机推荐

  1. Eclipse修改背景颜色

    windows->peferences->General->Editors->Text EditorAppearance color options:选择Background ...

  2. docker 从容器中拷文件到宿主机器中

    sudo docker cp 1d051604e0ea:/root/data /home/developer/zhanghui/data

  3. javascript 上传文件相关 (5) Blob 对象

    这一系列文章都讲述的是关于使用 JavaScript 操作文件相关的知识,其中最重要的是 File 对象,而实际上 file 对象只是 blob 对象的一个更具体的版本,blob 存储着大量的二进制数 ...

  4. MySQL5.7安装及遇到的问题

     Mysql安装教程: mysql历史版本下载 将在官网下载的安装包解压(如:如D:\mysql-5.7.19-x64) 1.(修复问题Q1时必做,全新安装时不要删除)在mysql的安装路径(如D:\ ...

  5. Android 界面滑动卡顿分析与解决方案(入门)

    Android 界面滑动卡顿分析与解决方案(入门) 导致Android界面滑动卡顿主要有两个原因: 1.UI线程(main)有耗时操作 2.视图渲染时间过长,导致卡顿 目前只讲第1点,第二点相对比较复 ...

  6. ZOJ 3946 Highway Project(Dijkstra)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  7. Exchange 2016 系统要求

    Exchange 2016 和早期版本的 Exchange Server 共存方案 Exchange 2016支持混合部署方案 Exchange 2016 的网络和目录服务器要求 目录服务体系结构: ...

  8. linux必学

    memcache zookeeper activemq

  9. Linux入门之运维(1) 系统监控 vmstat top

    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...

  10. mysql按时间查询(年/月/日)

    0.创建表sql语句查询 mysql> show create table byzp_personinfo; CREATE TABLE `byzp_personinfo` ( `id` int( ...