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.

 

题目中定义了三种规则,1.全部为大写字母,2.全部为小写字母,3.第一个字母为大写字母。判断给定word是否满足其中一项规则

该问题可以进行转化为:1.word大写字母个数为字符串长度,2.大写字母个数为0,3.大写字母个数为1且为第一位
class Solution {
public boolean detectCapitalUse(String word) {
int cnt = 0;
for(char c:word.toCharArray())
if(c <= 'Z') //大写
cnt ++;
if((cnt == 0|| cnt==word.length()) || (cnt==1 && word.charAt(0) <= 'Z'))
return true;
return false;
}
}
 
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

 
 
 
 

520. Detect Capital的更多相关文章

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

  2. 520. Detect Capital【easy】

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

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

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

  4. 【leetcode】520. Detect Capital

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

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

  6. LeetCode 520 Detect Capital 解题报告

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

  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. 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: 520 Detect Capital(easy)

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

随机推荐

  1. Numpy入门 - 数组基本运算

    本节主要讲解numpy数组的基本运算,包括两数组相加.相减.相乘和相除. 一.两数组相加add import numpy as np arr1 = np.array([[1, 2, 3], [4, 5 ...

  2. absolute和relative元素 设置百分比宽高的差异

    一般元素在页面所占的空间包括:magin border padding content.以前一直以为子元素设置百分比宽高都是以父元素的content值为基准计算的.但是当子元素的position不同时 ...

  3. php 写程序求三个数的最大值

    最简单的调用PHP自带的max函数即可:echo max(1,2,3,4,5);如果要自定义函数的话:function test($a,$b,$c){ return $a > $b ?($a & ...

  4. 【Codeforces 788C】The Great Mixing

    http://codeforces.com/contest/788/problem/C 显然如果有两杯一样的酒,把它们当作同一杯就好了.所以k<=1e6毫无意义. 若选的x杯酒的浓度分别为a,b ...

  5. linux apt-cache使用方法

    apt-cache是linux下的一个apt软件包管理工具,它可查询apt的二进制软件包缓存文件.APT包管理的大多数信息查询功能都可以由apt-cache命令实现,通过apt-cache命令配合不同 ...

  6. 使用hiredis实现pipeline方式访问

    1.介绍 hiredis: 一个c/c++的访问redis的api库 地址:https://github.com/redis/hiredis pipeline方式: redis中的pipeline方式 ...

  7. iScroll的简单使用

    今天是2017-1-18,每天进步一点点 今天主要来总结一下我在项目中遇到的关于iScroll的使用问题. 第一个是iscroll的初始化问题. --在页面资源(包括图片)加载完毕后100ms之后初始 ...

  8. LeetCode(24) Swap Nodes in Pairs

    题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  9. Android View视图系统分析和Scroller和OverScroller分析

    Android  View视图系统分析和Scroller和OverScroller分析 View  视图分析         首先,我们知道.在Android中全部的视图资源(无论是Layout还是V ...

  10. Leetcode 高精度 Plus One

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Plus One Total Accepted: 17614 Total Submissio ...