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. java集合 stream 相关用法(1)

    java8新增一种流式数据,让操作集合数据更简单方便. 定义基本对象: public class Peo { private String name; private String id; publi ...

  2. JStorm开发经验+运维经验总结

    1.开发经验总结  ——12 Sep 2014 · 8 revisions 在jstorm中, spout中nextTuple和ack/fail运行在不同的线程中, 从而鼓励用户在nextTuple里 ...

  3. 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并

    [BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...

  4. cocos2d-X学习之主要类介绍:动作:CCAction

    引用自:http://www.cnblogs.com/lhming/archive/2012/07/01/2572238.html 类继承图: 主要函数: virtual CCObject *  co ...

  5. 在java中public void与public static void有什么区别 ?

    public void 修饰是非静态方法,该类方法属于对象,在对象初始化(new Object())后才能被调用:public static void 修饰是静态方法,属于类,使用类名.方法名直接调用 ...

  6. go项目找不到包问题

    最近学习go语言,但是在主函数中引入其他包(自定义包)中方法的时候,编译代码,显示找不到包,如: $GOPATH>go build stacker.gostacker.go:18:2: cann ...

  7. CoreThink开发(十一)首页控制器判断移动设备还是PC并做相应处理

    在home模块Index控制器添加判断代码 application\Home\Controller\IndexController.class.php <?php // +----------- ...

  8. pandas(二)函数应用和映射

    NumPy的ufuncs也可以操作pandas对象 >>> frame one two three four a 0 1 2 3 b 4 5 6 7 c 8 9 10 11 d 12 ...

  9. [StringUtil ] isEmpty VS isBlank

    昨天才意识到这两个的存在. Blank(空字符串 blank) StringUtils.isNoneBlank(null) = false StringUtils.isNoneBlank(null, ...

  10. 10046 trace详解(1)

    10046 trace帮助我们解析一条/多条SQL.PL/SQL语句的运行状态,这些状态包括:Parse/Fetch/Execute三个阶段中遇到的等待事件.消耗的物理和逻辑读.CPU时间.执行计划等 ...