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.

public class Solution {
public boolean detectCapitalUse(String word) {
if (word == null)
return true;
int upCnt = 0; for (int i=0; i<word.length(); i++) {
char ch = word.charAt(i);
if (ch>='A' && ch<='Z')
upCnt ++;
}
if (upCnt==word.length() || upCnt==0)
return true;
else {
if (upCnt == 1 && word.charAt(0)>='A'&&word.charAt(0)<='Z')
return true;
}
return false;
}
}

LeetCode - 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. LeetCode 520 Detect Capital 解题报告

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

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

  5. 【leetcode】520. Detect Capital

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

  6. 520. Detect Capital【easy】

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

  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. line-height相关总结

    line-height http://cindylu520.iteye.com/blog/670512 四个boxes containing boxes inline boxes 匿名inline b ...

  2. Nginx实战之反向代理WebSocket的配置实例

    http://www.jb51.net/article/112183.htm 最近在工作中遇到一个需求,需要使用 nginx 反向代理websocket,经过查找一番资料,目前已经测试通过,所以这篇文 ...

  3. javaScript中关于字符串的操作函数和方法

    1.字符串转换 toString():可以将任何类型的数据都转换为字符串 var num= 19; //19 var myStr = num.toString(); //"19" ...

  4. h5开发安卓软键盘遮挡解决方案

    //处理input focus时被键盘遮挡问题 inputFocus:function(){ if(/Android [4-6]/.test(navigator.appVersion)) { wind ...

  5. RPC架构简单理解

    RPC(Remote Promote Call) 一种进程间通信方式.允许像调用本地服务一样调用远程服务. RPC框架的主要目标就是让远程服务调用更简单.透明.RPC框架负责屏蔽底层的传输方式(TCP ...

  6. mybatis_SQL映射(1)

    文章摘录自:http://blog.csdn.net/y172158950/article/details/17258377 1. select的映射 <select id="sele ...

  7. Unix/Linux命令:SED

    在Unix/Linux系统中,sed命令采用逐行处理的方式对文件进行查找.删除.替换.添加.插入等操作. 语法:sed [OPTION]... {script-only-if-no-other-scr ...

  8. php curl实现get和post请求

    1.cURL介绍 cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性 ...

  9. 写一个简单的配置文件和日志管理(shell)

    最近在做一个Linux系统方案的设计,写了一个之前升级服务程序的配置和日志管理. 共4个文件,服务端一个UpdateServer.conf配置文件和一个UpdateServer脚本,客户端一个Upda ...

  10. mybatis用spring的动态数据源实现读写分离

    一.环境: 三个mysql数据库.一个master,两个slaver.master写数据,slaver读数据. 二.原理: 借助Spring的 AbstractRoutingDataSource 这个 ...