问题描述:
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".
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
示例:
Input: "USA" Output: True
Input: "FlaG" Output: False
问题来源:Detect Capital(详细地址:https://leetcode.com/problems/detect-capital/description/)
思路分析:在这我们就不调用库函数了,直接和'Z'比较就行了。我们在这将字符串先转化成数组好了,要是觉得浪费空间的话,其实也是可以不用的,直接调用charAt()方法进行比较,接着记录下每个字符串中出现大写字母的次数。情况一:所有的字符都是大写;情景二:没有一个是大写的;情景三:只有第一个是大写的,其他的都是小写的。以上三种情况下,返回的都是true,其他都是false。
代码: