[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么表示大写字母:c <= 'Z'

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

c <= 'Z'

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

charAt 可以把字符串中的字母单独抽出来

[关键模板化代码]:

统计大写字母:

for (char c : word.toCharArray()) {
if (c <= 'Z') count++;
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public boolean detectCapitalUse(String word) {
//ini
int count = 0;
for (char c : word.toCharArray()) {
if (c <= 'Z') count++;
}
if (count == 0 || count == word.length() || (count == 1 && word.charAt(0) <= 'Z')) {
return true;
}
return false;
}
}

520. Detect Capital判断单词有效性的更多相关文章

  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. 520. Detect Capital【easy】

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

  4. 【leetcode】520. Detect Capital

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

  5. 520 Detect Capital 检测大写字母

    给定一个单词,你需要判断单词的大写使用是否正确.我们定义,在以下情况时,单词的大写用法是正确的:    全部字母都是大写,比如"USA".    单词中所有字母都不是大写,比如&q ...

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

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

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

随机推荐

  1. bzoj 1264 基因匹配

    Written with StackEdit. Description 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的\(DNA\)序列由无数种碱基排列而成(地球上只有\(4\)种) ...

  2. 【策略】一致性Hash算法

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

  3. rest_framework使用完之后的简单总结

    首先先大致概括一下使用流程,因为还不是对这个框架很熟悉(其实有很多知识可以对比formModel的) 其实还是遵循django的MTV的模式,还是得从url开始 1.rest_framework有一个 ...

  4. Android中内容观察者的使用---- ContentObserver类详解 (转)

    前言: 工作中,需要开启一个线程大量的查询某个数据库值发送了变化,导致的开销很大,后来在老大的指点下,利用了 ContentObserver完美的解决了该问题,感到很兴奋,做完之后自己也对Conten ...

  5. <trim>: prefix+prefixOverrides+suffix+suffixOverrides

    <trim prefix="where" prefixOverrides="where" suffixOverrides="and"& ...

  6. 1020. Tree Traversals (25) ——树的遍历

    //题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印 PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难 先通过后续中序建一棵树,然后通过BFS遍历这棵树 ...

  7. OpenCL™ 2.0 – Pipes

    copy from http://developer.amd.com/community/blog/2014/10/31/opencl-2-0-pipes/ OpenCL™ 2.0 – Pipes I ...

  8. jinja2的一些用法

    1.split用法以及乘法运算 {% set user_list=l.users.split(',') %} <tr> <td>{{ l.name }}</td> ...

  9. mysql的账号管理

    mysql的账号管理 最先匹配 user 表(包含:用户列 权限列 安全列 资源控制列)连接判断:host  user password字段(user的授权是全局的): 然后匹配db表:如果只是给指定 ...

  10. TI技术官方论坛

    https://e2echina.ti.com/question_answer/dsp_arm/c6000_dsp/f/32/t/172279