[LeetCode]520 Detect Capital
来源:互联网 发布:单片机交通灯实验报告 编辑:程序博客网 时间:2024/05/19 20:44
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".
Simple Java Solution O(n) time O(1) space
public boolean detectCapitalUse(String word) { return word.equals(word.toUpperCase()) || word.equals(word.toLowerCase()) || Character.isUpperCase(word.charAt(0)) && word.substring(1).equals(word.substring(1).toLowerCase()); }
A slightly more verbose, but more efficient solution:
public boolean detectCapitalUse(String word) { int numUpper = 0; for (int i=0;i<word.length();i++) { if (Character.isUpperCase(word.charAt(i))) numUpper++; } if (numUpper == 1) return Character.isUpperCase(word.charAt(0)); return numUpper == 0 || numUpper == word.length(); }
0 0
- leetcode-520-Detect Capital
- Leetcode #520 Detect Capital
- [LeetCode]520 Detect Capital
- LeetCode 520:Detect Capital
- LeetCode 520 Detect Capital
- Leetcode 520 Detect Capital
- LeetCode 520 Detect Capital
- 【LeetCode】520 Detect Capital
- Leetcode 520 Detect Capital
- LeetCode.520 Detect Capital
- leetcode 520 Detect Capital C++
- leetcode 520[easy]---Detect Capital
- LeetCode 520:Detect Capital (c++)
- LeetCode 520 Detect Capital 检测大写
- 【Leetcode】520. Detect Capital
- LeetCode 520. Detect Capital
- LeetCode 520. Detect Capital
- LeetCode:Detect Capital
- 通过js就可以判断当前页面是在什么浏览器打开的
- KMP算法
- 495. Teemo Attacking
- 使用FreeMarker替换JSP的10个理由
- Android广播
- [LeetCode]520 Detect Capital
- 1.4.PHP7.1 狐教程-女神的成长环境(Mac下 PHP开发环境 配置及安装 php7.1.x nginx mysql)
- 您正在通过不兼容的虚拟化管理程序运行 VMware Workstation。禁用此虚拟化管理程序后才能开启虚拟机
- Linux学习--磁盘分区与挂载
- 大数据江湖之即席查询与分析(上篇)--即席查询与分析的前世今生
- 线程池的原理及实现
- poj 2752Seek the Name,Seek the Fame(KMP)
- Aircrack的简单使用
- valarray类