LeetCode之Detect Capital

简介: LeetCode之Detect Capital

1、题目


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.


Subscribe to see which companies asked this question.

2、代码实现

public class Solution {
    public boolean detectCapitalUse(String word) {
      if (word == null || word.length() == 0)
        return false;
      if (word.length() == 1) 
        return true;
      int length = word.length();
      if (word.charAt(0) >= 65 && word.charAt(0) <= 90) {
        //AA**
        if (word.charAt(1) >= 65 && word.charAt(1) <= 90) {
          if (length > 2) {
            //AA*a*
            for (int i = 2 ; i < length; ++i) {
              if (word.charAt(i) >= 97 && word.charAt(i) <= 122)
                return false;
            }
            //AAA
            return true;
          } else {
            //AA
            return true;
          }
        }
        //Aa**
        if (word.charAt(1) >= 97 && word.charAt(1) <= 122) {
          if (length > 2) {
            for (int i = 2 ; i < length; ++i) {
              //Aa*A*
              if (word.charAt(i) >= 65 && word.charAt(i) <= 90)
                return false;
            }
            //Aaa
            return true;
          } else {
            //Aa
            return true;
          }
        } 
      } else {
        //aAa
        for (int i = 1; i < length; i++) {
          if (word.charAt(i) >= 65 && word.charAt(i) <= 90)
            return false;
        }
          //aaa
        return true;
      }
        return false;
    }
}
相关文章
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
41 6
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
35 4
|
1月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
73 2
|
1月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
35 7
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
17 4
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
40 5
|
1月前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
36 3
|
1月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
15 3
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - II. 从上到下打印二叉树 II
本文提供了一种Python实现方法,用于层次遍历二叉树并按层打印结果,每层节点按从左到右的顺序排列,每层打印到一行。
28 3