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. 3.3常用类(java学习笔记)Runtime与Process

    一.Runtime 我们来看下文档中对Runtime的说明: 每一个java程序都有一个属于Runtime类的实例,它允许程序连接到程序运行环境. 当前runtime可以用getRuntime()方法 ...

  2. 误删除Ubuntu 14桌面文件夹“~/桌面”,如何恢复?

    一不小心把当前用户的桌面文件夹“/home/wenjianbao/桌面”删了,导致系统把“/home/wenjianbao”当成桌面文件夹.结果,桌面上全是乱七八糟的文件/文件夹. 查看了下网络资料, ...

  3. 【spring boot】spring cloud下spring boot微服务启动没有报错,但是访问访问不到

    spring cloud下spring boot微服务启动没有报错,但是访问访问不到 解决方法: 可能是端口被占用了,但是依旧启用成功了. 更改一下项目启用的端口号,再重新启动查看是否可以正常访问.

  4. java基础之:java注解

    一:元注解 元注解的作用就是负责注解其他注解.Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明.Java5.0定义的元注解: 1. ...

  5. golang构造单链表

    原文地址:http://www.niu12.com/article/47package main import "fmt" type ListNode struct { Value ...

  6. iOS:自定义代码块{ }

    1.Xcode本身带有编码常用的代码块可供使用,如下图 调用方法: (1)直接拖到代码区域中: (2)使用快捷键,键入 “while”, Xcode就会出现自动完成的提示 也可以自定义自己常用的代码块 ...

  7. MyBatis学习-偏实践(单独MyBatis项目)

    准备先把MyBatis搞熟悉了,然后把SpringMVC搞熟悉了. MyBatis的材料,除了我之前自己实验的 http://www.cnblogs.com/charlesblc/p/5906431. ...

  8. idea 配置Spring MVC

    一.idea 生成的Spring MVC 项目将<url-pattern>.form<url-pattern>改成<url-pattern>.do<url-p ...

  9. Joomla详细安装图文教程

    Joomla 详细安装图文教程 第一步,配置网站信息 配置数据库:这里我选择MySQLi,可以根据自己的选择         安装-- 安装完成!

  10. Vue工程模板文件 webpack打包

    1.github github地址:https://github.com/MengFangui/VueProjectTemplate 2.webpack配置 (1)基础配置webpack.base.c ...