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. [BZOJ4772]显而易见的数论(数论)

    4772: 显而易见的数论 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 76  Solved: 32[Submit][Status][Discuss ...

  2. [BZOJ 1212] L语言

    Link: BZOJ 1212 传送门 Solution: 看到字符串的多模式匹配,正解一般就是Trie树/AC自动机 此题由于每个模式串长度都很小,于是直接在Trie树上暴力就行了 先把所有模式串建 ...

  3. [Contest20180426]校门外的树

    $\newcommand{\align}[1]{\begin{align*}#1\end{align*}}$题意:对于一个排列$p_{1\cdots n}$构造一个图,如果$i\lt j$且$p_i\ ...

  4. 【计算几何】bzoj2338 [HNOI2011]数矩形

    对于两条线段,若其中点重合,且长度相等,那么它们一定是某个矩形的对角线. N*N地处理出所有线段,排序,对每一部分中点重合.长度相等的线段进行暴力枚举,更新答案. 用 long double 注意EP ...

  5. 协同过滤中的Grey Sheep问题

    寒神解释:某些用户的倾向性和品味没有一致性,比较散.因此在协同过滤这种算法里,没办法和某个group有很高的相似/一致度,推荐会失效. 我理解是寻找邻居时候计算得到的相似度和其他用户相似度都非常小,或 ...

  6. delphi执行cmd命令和bat文件

    转载地址:http://blog.csdn.net/hutao1101175783/article/details/42807063 cmd:='echo d | Xcopy '+BasePath+' ...

  7. 自己做的javascript简易计算器

    html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF- ...

  8. CentOS6 安装golang

    CentOS6 安装golang 下载 wget http://golangtc.com/static/go/1.8/go1.8.linux-amd64.tar.gz 1 1 1 解压 tar -xz ...

  9. Fatal error: Maximum execution time of 30 seconds exceeded in

    Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Apache Software Found ...

  10. Shell实现多级菜单系统安装维护脚本实例分享

    Shell实现多级菜单系统安装维护脚本实例分享 这篇文章主要介绍了Shell实现多级菜单系统安装维护脚本实例分享,本文脚本用多级菜单实现管理WEB服务器.Mysql服务器.Nginx服器等,需要的朋友 ...