描述

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:

All letters in this word are capitals, like "USA".

All letters in this word are not capitals, like "leetcode".

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.

分析

每遇到一个大写字母,就将变量upCount加1,遍历结束后,如果要返回真,那么会满足下列条件之一:

  • upCount == word.size()。即所有字母均为大写
  • upCount == 0.即所有字母均为小写
  • upCount == 1.有且仅有第一个字母为大写。

否则,返回假。

代码如下:

class Solution {
public:
bool detectCapitalUse(string word) {
int upCount = 0; if(word.size() == 0)return false;
for(int i = 0; i != word.size(); ++i)
if(isupper(word[i]))++upCount;
if(upCount == word.size() || upCount == 0 || (upCount == 1 && isupper(word[0])))return true;
return false;
}
};

日常水题。。。

最新文章

  1. word20161218
  2. Oracle数据库全球化
  3. chrome控制台调试学习笔记 暂未整理
  4. ArcEngine拓扑
  5. CSS打造经典鼠标触发显示选项
  6. 添加事件(jquery)
  7. Java设计模式12:常用设计模式之外观模式(结构型模式)
  8. 阐述Lambada表达式
  9. WebBrowser控件使用技巧分享
  10. robin 今日南
  11. handler的使用
  12. Selenium的简单安装和使用
  13. python链接mysql以及常用语法
  14. window.location.hash 使用说明
  15. IScroll5要防止重复加载
  16. 洛谷P4705 玩游戏 [生成函数,NTT]
  17. Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程(转)
  18. PAT 1085 PAT单位排行(25)(映射、集合训练)
  19. 谈谈 Python 程序的运行原理
  20. python中的运算符的分类以及使用方法

热门文章

  1. Elasticsearch-6.7.0系列(八)开启kibana监控
  2. Linux守护进程编写指南
  3. Hadoop常用命令及范例
  4. SIM7600CE http post
  5. Flask统计代码行数
  6. sql的匹配和正则表达式
  7. RNN、LSTM介绍以及梯度消失问题讲解
  8. Java中处理接口返回base64编码的图片数据
  9. python算法与数据结构-快速排序算法(36)
  10. Django之路——4 Django的视图层