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:

  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.

解法一:

 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】的更多相关文章

  1. 【leetcode】520. Detect Capital

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

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

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

  5. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  6. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  7. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

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

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

随机推荐

  1. 【贪心】Gym - 100507H - Pair: normal and paranormal

    每次取相邻的两个可以射击的从序列中删除,重复n次. 可以看作括号序列的匹配. #include<cstdio> #include<vector> using namespace ...

  2. 【动态规划】【记忆化搜索】【搜索】CODEVS 1262 不要把球传我 2012年CCC加拿大高中生信息学奥赛

    可以暴力递归求解,应该不会TLE,但是我们考虑记忆化优化. 设f(i,j)表示第i个数为j时的方案数. f(i,j)=f(1,j-1)+f(2,j-1)+……+f(i-1,j-1) (4>=j& ...

  3. 【数论】【快速幂】bzoj1008 [HNOI2008]越狱

    根据 高中的数学知识 即可推出 ans=m^n-m*(m-1)^(n-1) .快速幂取模搞一下即可. #include<cstdio> using namespace std; typed ...

  4. [CF864F]Cities Excursions

    题目大意: 一个$n(n\le3000)$个点的有向图,$q(q\le4\times10^5)$组询问,每次询问$s_i,t_i$之间是否存在一条字典序最小的路径(可以重复经过不为$t_i$的结点). ...

  5. 1.8(java学习笔记)继承与方法的重写

    继承 在java中可以通过继承提高代码的复用率. 例如A继承了B,就可以是 例如,首先有一个类似Person,这个类中有有一些属性和方法,我们再新建一个Student类,其中有一部分属性和方法与Per ...

  6. Problem H: 阶乘和

    #include<stdio.h> int main() { ; ; ; int n; scanf("%d",&n); ;i<=n;i++) { ret= ...

  7. 自定义PHP页面跳转函数redirect($url, $time = 0, $msg = '')

    利用PHP的header()函数,可以实现页面跳转,如 header("Location: " . $url); 但它有个缺点,一旦HTTP报头块已经发送,就不能使用 header ...

  8. Objective-C:NSSet和NSMutbaleSet的用法

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...

  9. min-height IE6的解决方案

    selector { min-height:500px; height:auto !important; height:500px; } 因为IE6中当内容大于容器高度或宽度时,就会撑破容器.并且在同 ...

  10. Solr6 Suggest(智能提示)

    1.介绍 Solr从1.4开始便提供了检查建议,检索建议目前是各大搜索的标配应用,主要作用是避免用户输入错误的搜索词,同时将用户引导到相应的关键词搜索上.通常,我们将其称为搜索联想. 其效果如图所示. ...