Detect Capital
来源:互联网 发布:十大网络主播评选排名 编辑:程序博客网 时间:2024/06/03 06:00
题目地址:https://leetcode.com/problems/detect-capital/?tab=Description
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.
题目检查单词大写字母的书写是否符合规范:
- 假如首字母是大写字母
- 如果第二个字母是大写
- 如果后面的字符全部大写,返回true;
- 否则返回false;
- 否则
- 如果后面的字符全部小写,返回true;
- 否则返回false;
- 如果第二个字母是大写
- 假如首字母是小写字母
- 如果第二个字母后面(包含)的字母全部是小写,那么返回true;
- 否则返回false;
分支结构大致如此:
public class DetectCapital { public static boolean detectCapitalUse(String word) { if (word.length() <= 1) return true; // The first letter is lowercase if (word.charAt(0) >= 'a' && word.charAt(0) <= 'z') { for (int i = 1; i < word.length(); i++) { if (word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') return false; } return true; } else { // The first letter is uppercase if (word.charAt(1) >= 'A' && word.charAt(1) <= 'Z') { // The second letter is uppercase if (word.length() >= 3) { for (int i = 2; i < word.length(); i++) { if (word.charAt(i) >= 'a' && word.charAt(i) <= 'z') return false; } return true; } return true; } else { // The second letter is lowercase if (word.length() >= 3) { for (int i = 2; i < word.length(); i++) { if (word.charAt(i) >= 'A' && word.charAt(i) <= 'Z') return false; } return true; } return true; } } } public static void main(String[] args) { System.out.println(detectCapitalUse("gooGle")); }}
时间复杂度为:O(n)。
0 0
- Detect Capital
- Detect Capital
- Detect Capital
- Detect Capital
- Detect Capital
- Detect Capital
- Detect Capital
- 【Leetcode】520. Detect Capital
- 520. Detect Capital (E)
- LeetCode 520. Detect Capital
- leetcode-520-Detect Capital
- LeetCode 520. Detect Capital
- leetcode520: Detect Capital
- 520. Detect Capital
- LeetCode:Detect Capital
- LeetCode-520. Detect Capital
- 【520】Detect Capital
- Leetcode #520 Detect Capital
- 深入探讨JavaScript如何实现深度复制
- JVM
- 招行支付对接说明
- 乘法逆元(对于非质数的乘法逆元)
- 复习java基础------>java的静态绑定和动态绑
- Detect Capital
- 关于vim使用的笔记
- 基于DDD的.NET开发框架ABP实例,多租户 (Sass)应用程序,采用.NET MVC, Angularjs, EntityFramework-介绍
- 415. Add Strings
- android与服务器交互总结(json,post,xUtils,Volley,Gson)
- 让tabbar自动隐藏
- 重构之提炼方法
- HDU2094 产生冠军
- 优化myschool 第九章