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, 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 boolean detectCapitalUse(String word) {
int numCap = 0;
int i = 0;
while (i < word.length()) {
if (Character.isUpperCase(word.charAt(i))) {
numCap += 1;
}
i += 1;
}
if (Character.isUpperCase(word.charAt(0))) {
return numCap == 1 || numCap == word.length();
}
return numCap == 0;
}
}

[LC] 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. 520. Detect Capital判断单词有效性

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

  6. 520. Detect Capital

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

  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. HDU 3484 Matrix Game 枚举暴力

    上次周赛碰到这个题目,居然都没思路,真是不应该啊,起码也应该想到枚举法. 因为题目只允许每一row进行reverse操作,而每两列可以进行交换操作,所以首先把row的变化固定下来,即枚举第一列与第1- ...

  2. 十五、Numpy-科学计算基础库

    Numpy:          NumPy(Numerical Python) 是科学计算基础库,提供大量科学计算相关功能,比如数据统计,随机数生成等.其提供最核心类型为多维数组类型(ndarray) ...

  3. 题解 P1317 【低洼地】

    题目 这题挺简单的,没必要用数组 [分析] 需要判断的是低洼地的数量 通过对题目中图进行分析,显然可以发现低洼地的定义: 若数组中存在一个数值相同的连续区间,这个区间端点外相邻两点的数值都大于该区间的 ...

  4. vim python支持

    yum remove vim -y yum install ncurses-devel python-devel -y git clone https://github.com/vim/vim.git ...

  5. ansible删除目录下所有内容

    使用ansible的同学都知道,ansible只支持新增删除具体的某个文件夹或者文件,如下所示: 1. 创建目录,删除整个目录 - name: Create a directory if it doe ...

  6. idea新建文件模板 (以xml文件为例)

    https://blog.csdn.net/li1325169021/article/details/93158207 偷个懒

  7. canvas实现粒子星空连线

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>离 ...

  8. CertUtil: -hashfile 失败: 0xd00000bb (-805306181)

    使用CertUtil验证Python安装文件的时候出现了这个错误. CertUtil: -hashfile 失败: 0xd00000bb (-805306181) 代码是这样 certutil -ha ...

  9. Maven--可选依赖

    假设有这样换一个依赖关系,项目 A 依赖于项目 B,项目 B 依赖于项目 X 和 Y,B 对于 X 和 Y的依赖都是可选依赖: A -> B B -> X(可选) B -> Y(可选 ...

  10. mysql 不停机 短时间锁表 备份 主备同步 新增备份机器

    刷新数据   [root@localhost ~]# mysql -e 'flush tables with read lock;' 锁表刷新表数据   [root@localhost ~]# mys ...