zoukankan      html  css  js  c++  java
  • 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:

    1. All letters in this word are capitals, like "USA".
    2. All letters in this word are not capitals, like "leetcode".
    3. 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.

    解法一:

     1 class Solution {
     2 public:
     3     bool detectCapitalUse(string word) {
     4         if (word.length() <= 1) {
     5             return true;
     6         }
     7         
     8         bool flag = false;
     9         for (int i = 1; i < word.length(); ++i) {
    10             //以小写字母开头,则后面都必须为小写
    11             if (word[0] >= 'a' && word[0] <= 'z') {
    12                 if (word[i] < 'a' || word[i] > 'z') {
    13                     return false;
    14                 }
    15             }
    16             //以大写字母开头,下面分类讨论
    17             else if (word[0] >= 'A' && word[0] <= 'Z') {
    18                 if (i == 1 && word[i] >= 'a' && word[i] <= 'z') {
    19                     flag = true;
    20                     continue;
    21                 }
    22                 //说明后面都必须为小写
    23                 if (flag) {
    24                     if (word[i] >= 'A' && word[i] <= 'Z') {
    25                         return false;
    26                     }
    27                 }
    28                 //说明后面都必须为大写
    29                 else {
    30                     if (word[i] >= 'a' && word[i] <= 'z') {
    31                         return false;
    32                     }
    33                 }
    34             }
    35         }
    36         
    37         return true;
    38     }
    39 };

    解法二:

     1 class Solution {
     2 public:
     3     bool detectCapitalUse(string word) {
     4         int size=word.size(),count=0;
     5         if(size<=1)
     6             return true;
     7         for (int i = 1; i < size; i++){
     8             if(word[i]>='a'&&word[i]<='z')
     9                 count+=1;
    10             else
    11                 count+=2;
    12         }
    13         if(count==size-1)
    14             return true;
    15         else if(count==2*(size-1))
    16             return word[0]>='A'&&word[0]<='Z';
    17         else 
    18             return false;
    19     }
    20 };

    参考@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.

    解法三:

     1 bool detectCapitalUse(string word) {
     2     const char *c = word.c_str();
     3     if (word.size() <= 1) return true;
     4     if (*c <= 'z' && *c >= 'a') {
     5         c = c + 1;
     6         while (*c) {
     7             if (*c <= 'Z' && *c >= 'A') return false;
     8             c = c + 1;
     9         }
    10     } else {
    11         c = c + 1;
    12         if (*c <= 'Z' && *c >= 'A') {
    13             c = c + 1;
    14             while (*c) {
    15                 if (*c <= 'z' && *c >= 'a') return false;
    16                 c = c + 1;
    17             }
    18         } else {
    19             c = c + 1;
    20             while (*c) {
    21                 if (*c <= 'Z' && *c >= 'A') return false;
    22                 c = c + 1;
    23             }
    24         }
    25     }
    26         
    27     return true;
    28 }

    参考@ai977313677 的代码。

    解法四:

    1 class Solution {
    2 public:
    3     bool detectCapitalUse(string word) {
    4         int cnt = 0;
    5         for (char c : word) if (isupper(c)) ++cnt;
    6         return !cnt || cnt == word.size() || cnt == 1 && isupper(word[0]);
    7     }
    8 };

    参考@lzl124631x 的代码。

  • 相关阅读:
    Unity3D GUI图形用户界面系统
    Unity3D 自动寻路入门指南
    Unity3D 导航网格自动寻路(Navigation Mesh)
    拓展通用的冒泡排序方法
    DoTween 应用设置
    DoTween 教程
    Unity3D 脚本手册
    unity3d中获得物体的size
    Unity自动寻路Navmesh之高级
    C# 代码页获取input的值
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7582018.html
Copyright © 2011-2022 走看看