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. Dynamics 365创建电子邮箱字段包含值的联系人同时更改负责人的方法。

    摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复267或者20171129可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...

  2. redis在spring-boot中的应用

    Redis(REmote DIctionary Server) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis是一个开源的使用ANSI C语言编写.遵守BS ...

  3. HDU 2795 Billboard 线段树,区间最大值,单点更新

    Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 在学java继承中

    看到一片个人认为的好博客,http://www.cnblogs.com/dolphin0520/p/3803432.html   ;这一节看的比较慢,加油,就是干: 分类不好意思分到Java中,嘿嘿还 ...

  5. Solr中Field常用属性

    FieldType 实例:<fieldType name="text_ik" class="solr.TextField"></fieldTy ...

  6. python 爬取国家粮食局东北地区玉米收购价格监测信息

    #!/usr/bin/python# -*- coding: UTF-8 -*-import reimport sysimport timeimport urllibimport urllib.req ...

  7. Android最佳性能实践(四)——布局优化技巧

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43376527 在前面几篇文章其中.我们学习了怎样通过合理管理内存,以及高性能编码技 ...

  8. iOS_20_微博OAuth授权_取得用户授权的accessToken

    终于效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill ...

  9. [Phonegap+Sencha Touch] 移动开发26 Android下的sencha touch程序,转屏时,Ext.Viewport不能触发orientationchange事件的解决的方法

    Sencha touch 2.4.2 已经解决问题了. 比方你为Ext.Viewport的orientationchange事件加入了一个监听方法: Ext.Viewport.on('orientat ...

  10. Ajax 请求后打开新窗口被拦截的解决方案

    公司业务上有个场景,需请求后台获取支付链接地址,再打开地址引导用户购买.这样的场景在其他企业应该也很场景.但是遇到个很常见的问题,Ajax后直接用window.open(url),会被浏览器作为广告拦 ...