520. Detect Capital【easy】
520. Detect Capital【easy】
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.
解法一:
 class Solution {
 public:
     bool detectCapitalUse(string word) {
         if (word.length() <= ) {
             return true;
         }
         bool flag = false;
         for (int i = ; i < word.length(); ++i) {
             //以小写字母开头,则后面都必须为小写
             if (word[] >= 'a' && word[] <= 'z') {
                 if (word[i] < 'a' || word[i] > 'z') {
                     return false;
                 }
             }
             //以大写字母开头,下面分类讨论
             else if (word[] >= 'A' && word[] <= 'Z') {
                 if (i ==  && word[i] >= 'a' && word[i] <= 'z') {
                     flag = true;
                     continue;
                 }
                 //说明后面都必须为小写
                 if (flag) {
                     if (word[i] >= 'A' && word[i] <= 'Z') {
                         return false;
                     }
                 }
                 //说明后面都必须为大写
                 else {
                     if (word[i] >= 'a' && word[i] <= 'z') {
                         return false;
                     }
                 }
             }
         }
         return true;
     }
 };
解法二:
 class Solution {
 public:
     bool detectCapitalUse(string word) {
         int size=word.size(),count=;
         if(size<=)
             return true;
         for (int i = ; i < size; i++){
             if(word[i]>='a'&&word[i]<='z')
                 count+=;
             else
                 count+=;
         }
         if(count==size-)
             return true;
         else if(count==*(size-))
             return word[]>='A'&&word[]<='Z';
         else
             return false;
     }
 };
参考@zhengchaojie 的代码。
From 1~size-1,if we meet with a-z,we add 1,else we add 2.Then we can get the result that if the second to last letter is all lowercase or all upcase.
解法三:
 bool detectCapitalUse(string word) {
     const char *c = word.c_str();
     if (word.size() <= ) return true;
     if (*c <= 'z' && *c >= 'a') {
         c = c + ;
         while (*c) {
             if (*c <= 'Z' && *c >= 'A') return false;
             c = c + ;
         }
     } else {
         c = c + ;
         if (*c <= 'Z' && *c >= 'A') {
             c = c + ;
             while (*c) {
                 if (*c <= 'z' && *c >= 'a') return false;
                 c = c + ;
             }
         } else {
             c = c + ;
             while (*c) {
                 if (*c <= 'Z' && *c >= 'A') return false;
                 c = c + ;
             }
         }
     }
     return true;
 }
参考@ai977313677 的代码。
解法四:
 class Solution {
 public:
     bool detectCapitalUse(string word) {
         int cnt = ;
         for (char c : word) if (isupper(c)) ++cnt;
         return !cnt || cnt == word.size() || cnt ==  && isupper(word[]);
     }
 };
参考@lzl124631x 的代码。
520. Detect Capital【easy】的更多相关文章
- 【leetcode】520. Detect Capital
		problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ... 
- 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 ... 
- 170. Two Sum III - Data structure design【easy】
		170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ... 
- 160. Intersection of Two Linked Lists【easy】
		160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ... 
- 206. Reverse Linked List【easy】
		206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ... 
- 203. Remove Linked List Elements【easy】
		203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ... 
- 83. Remove Duplicates from Sorted List【easy】
		83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ... 
- 21. Merge Two Sorted Lists【easy】
		21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ... 
- 142. Linked List Cycle II【easy】
		142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ... 
随机推荐
- [CF983E]NN country
			题意:给一棵树,有许多条巴士线路$(a_i,b_i)$(巴士在路径上每个点都会停车),多次询问从一点到另一点最少要坐多少次巴士 首先dfs一遍预处理出一个点向上坐$2^k$次巴士能到的最浅点,于是我们 ... 
- 使用IDEA创建package
			1)使用IDEA创建java工程 什么也不选,直接点击Next 无脑继续下一步 点击“Finish”完成工程的创建. 2)在使用IDEA创建了工程之后,首先选中“src”文件夹,然后 紧接着输入包名 ... 
- SpingMVC实现操作的第一方式
			(一) 使用SpringMVC框架的步骤 (1):在web.xml中配置前端控制器 (2):在Spring-servlet.xml中配置 配置处理器映射器HandlerMapping(处理器Handl ... 
- Matlab设置形状大小
			x=0:10; y=2*x; plot(x,y,'-*','linewidth',0.5,'markersize',6)%%默认线宽为0.5,点大小为6 说明:调整线宽也可改变点的形状,这实际上是通过 ... 
- javascript  常用手势 分析
			javascript 常用手势, 个人觉得有3个 tap,swipe(swipeLeft,swipeRight,swipeTop,swipeRight),hold tap 是轻击 判断的原则是,在to ... 
- 【java】File的使用:将字符串写出到本地文件,大小0kb的原因
			实现方法: 暂时写一种方法,将字符串写出到本地文件,以后可以补充更多种方法: public static void main(String[] args) { /** * ============== ... 
- 一些常用JS 函数总结
			搜索url参数 /** * 搜索url参数 * @param {String} name 参数键名 * @return {String} 对应值 */ function getQueryVariabl ... 
- 全文检索引擎[asp版]
			search.asp: <% set DM=server.CreateObject("DeepMap.HLL")pnn=0: wdd="": pnn=Re ... 
- 转:facebook 开源工具集合
			http://codekk.com/blogs/detail/Trinea/Facebook%20%E7%9A%84%E9%82%A3%E4%BA%9B%E5%BC%80%E6%BA%90%E9%A1 ... 
- Android——DEBUG 堆栈
			当android系统执行出现死机等致命错误的时候.通常会有堆栈的DEBUG信息打印,一般直接看根本看不出问题是出在哪里!记录下我android4.2 的DEBUG 堆栈log的方法. 撰写不易,转载请 ... 
