题目:

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:

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. 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.

代码:

自己的:

 class Solution {
public:
bool detectCapitalUse(string word) {
bool result = ;
if (word[] < 'a'){
if (word[] < 'a'){
for(int i = ; i < word.size(); i++){
if(word[i] > 'Z'){
result = ;
break;
}
}
}
else{
for(int i = ; i < word.size(); i++){
if(word[i] < 'a'){
result = ;
break;
}
}
}
}
else{
for(auto c : word){
if (c < 'a'){
result = ;
break;
}
}
}
return result;
}
};

别人的:

 class Solution {
public:
bool detectCapitalUse(string word) {
int cnt = ;
for(int a = ; a < word.length(); a++ ) {
if('Z' - word[a] >= )
cnt++;
}
if(cnt == word.length() || cnt == || (cnt == && ('Z' - word[] >= )))
return true;
return false; }
};

对大写字母计数,然后再判断(全是大写、全是小写、首字母大写)

LeetCode: 520 Detect Capital(easy)的更多相关文章

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

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

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

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

  4. LeetCode 520 Detect Capital 解题报告

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

  5. 520. Detect Capital【easy】

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

  6. 【leetcode】520. Detect Capital

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

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

  8. 【LeetCode】520. Detect Capital 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环判断三个条件 大写字母个数和位置判断 根据首字符 ...

  9. 520. Detect Capital

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

随机推荐

  1. caffe搭建--opensuse13.2上搭建caffe开发环境

    第一部分:参考一下内容.将sudo 替换成zypper即可. --------------------------------------------这部分参照以下官网内容-------------- ...

  2. Java的泛型约束和限制

    不能用基本类型实例化类型参数 不能用类型参数代替基本类型:例如,没有Pair<double>,只有Pair<Double>,其原因是类型擦除.擦除之后,Pair类含有Objec ...

  3. HSSFWorkbook

    public ActionResult excelPrint() { HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件 HSSFShe ...

  4. React机制浅析

    在写React代码时,避免不了提前想想页面的结构,当然这也属于HTML布局了,比如哪些组件里需要包含哪些组件.遂突发奇想,如果试着把子组件的render内容替换原组件,会是个啥? 比如拿 https: ...

  5. 第 2 章 第 2 题 找" 重数/漏数 "问题 位向量实现

    问题分析 输入:一个包含了4 300 000 000个32位整数的文件( 其中可能有重复出现的数字 ) 输出:一个在这个文件中重复出现过了的数字 约束:无 解答思路 第一章中,我们学习了如何用位向量进 ...

  6. css定位的元素内层不自动扩高解决

        给最外层的CSS加上一行: 1 position: relative;             /* 给最外层加上这行 */

  7. Go语言string,int,int64 ,float之间类型转换方法

    (1)int转string ? 1 2 s := strconv.Itoa(i) 等价于s := strconv.FormatInt(int64(i), 10) (2)int64转string ? 1 ...

  8. LightOJ1220 —— 质因数分解

    题目链接:https://vjudge.net/problem/LightOJ-1220 1220 - Mysterious Bacteria    PDF (English) Statistics ...

  9. 牛逼的This使用

    今天看到一个很不错的this使用demo: package com.toov5.Reordering; class Message1{ private Channel channel; private ...

  10. 写给精明Java开发者的测试技巧

    我们都会为我们的代码编写测试,不是吗?毫无疑问,我知道这个问题的答案可能会从 “当然,但你知道怎样才能避免写测试吗?” 到 “必须的!我爱测试”都有.接下来我会给你几个小建议,它们可以让你编写测试变得 ...