520. Detect Capital判断单词有效性
[抄题]:
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
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么表示大写字母:c <= 'Z'
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
c <= 'Z'
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
charAt 可以把字符串中的字母单独抽出来
[关键模板化代码]:
统计大写字母:
for (char c : word.toCharArray()) {
if (c <= 'Z') count++;
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public boolean detectCapitalUse(String word) {
//ini
int count = 0;
for (char c : word.toCharArray()) {
if (c <= 'Z') count++;
}
if (count == 0 || count == word.length() || (count == 1 && word.charAt(0) <= 'Z')) {
return true;
}
return false;
}
}
520. Detect Capital判断单词有效性的更多相关文章
- Leetcode 520. Detect Capital 发现大写词 (字符串)
Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...
- 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 ...
- 520. Detect Capital【easy】
520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...
- 【leetcode】520. Detect Capital
problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...
- 520 Detect Capital 检测大写字母
给定一个单词,你需要判断单词的大写使用是否正确.我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如&q ...
- 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the ...
- LeetCode 520 Detect Capital 解题报告
题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...
- LeetCode: 520 Detect Capital(easy)
题目: Given a word, you need to judge whether the usage of capitals in it is right or not. We define t ...
- 520. Detect Capital(检测数组的大小写)
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
随机推荐
- Django之mysql表单操作
在Django之ORM模型中总结过django下mysql表的创建操作,接下来总结mysql表记录操作,包括表记录的增.删.改.查. 1. 添加表记录 class UserInfo(models.Mo ...
- 遇到eclipse安装插件一直报错问题(版本问题)
刚好用了一个插件,然后在线安装报错了,一直报错,之前用的这个插件是直接解压贴的,现在在线安装出错,就很尬,我又有点懒,不想再去下载,贴,所以查了下,有网友说是eclipse的插件版本问题,更新了下,成 ...
- 【msdn】RESTful 服务(配备 WCF)介绍
原文地址:http://msdn.microsoft.com/zh-cn/magazine/dd315413.aspx RESTful 服务(配备 WCF)介绍 Jon Flanders 代码下载位置 ...
- Maven无法上传到到私服
在pom.xml中插入如下代码: <distributionManagement> <repository> <id>nexus</id> <ur ...
- IE 9 下的 css 陷阱
IE 9 下的 css 陷阱 今天 Karson 老大的分享. 根据说明 当 css 文件超过一定大小时会被自动截断. http://ju.outofmemory.cn/entry/168599
- iframe添加点击事件
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 数据库模型类转换实体类的dbToPojoUtil工具类开发
idea颜色说明http://blog.csdn.net/saindy5828/article/details/53319693 1,中途运用了properties,properties.getPro ...
- 有限状态机(Finite-state machine, FSM)的C语言实现
有限状态机,也称为FSM(Finite State Machine),其在任意时刻都处于有限状态集合中的某一状态.当其获得一个输入字符时,将从当前状态转换到另一个状态,或者仍然保持在当前状态.任何一个 ...
- Memcached的限制和使用建议
1. 在Memcached中可以保存item数据量没有限制的,只要内存足够 2. Memcached单进程最大使用内存2G,要使用更多内存,可以分多个端口开启多个Memcached进程 3. Memc ...
- UDP协议相关解释
UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无连接的传输层 ...