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 ...
随机推荐
- 1.5(学习笔记)Cookie
一.Cookie简介 Cookie是网站发送的一小段数据,在用户访问浏览网站时通过浏览器存储在用户的计算机上. 主要用于记录一些用户状态信息,例如记录用户的账号,当前所在地等,根据这些信息网站 可以提 ...
- 回文数 Exercise06_03
import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:回文数 * */ public class Exercise06_03 { ...
- 泳池迷宫(p24)
/*2018年8月26日15:55:29作者:冰樱梦page-24泳池迷宫*/public class swiming{public static void main(String[] args){i ...
- Spring IOC 中三种注入方式
项目错误知识点记录 正文 最近在项目的时候,用到Spring框架,Spring框架提供了一种IOC的自动注入功能,可以很轻松的帮助我们创建一个Bean,这样就省的我们四处写new Object()这样 ...
- winform treeView 数据绑定
转载:http://www.jetwu.cn/archives/737 winform treeView 数据绑定 private void Form1_Load(object sender, Eve ...
- VMware虚拟机中为Linux 添加虚拟硬盘(VirtualBox方法类似)
修改1:2014-06-24 11:38:21 Linux添加硬盘是在原来安装的硬盘空间不够或者需要使用其他硬盘上的东西时候的解决办法,因为大多数初学者习惯使用虚拟机,这里以在Vmware虚拟机中实现 ...
- ZooKeeper本身是一个分布式应用程序,为写入分布式应用程序提供服务。
ZooKeeper本身是一个分布式应用程序,为写入分布式应用程序提供服务. 作为ZooKeeper架构的一部分的每个组件在下表中进行了说明. 部分 描述 Client(客户端) 客户端,我们的分布式应 ...
- Android 友盟社会化组件-分享实现
本文章链接地址:http://dev.umeng.com/social/android/share/quick-integration 分享快速集成 1 产品概述 友盟社会化组件,可以让移动应用快速具 ...
- 【json】前台ajax序列化的多个属性拼接在一起的字符串,转化为JSONObject对象
1.首先看一下前台序列化了哪些东西: 部分js代码 //查询按钮 $(".questButton").click(function(){ $.ajax({url:"/qu ...
- js各种验证总结
1.邮箱验证 function isEmail(str){ var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1 ...