• 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:  

      1.All letters in this word are capitals, like "USA". All letters in this word are not capitals, like "leetcode".

           2.Only the first letter in this word is capital if it has more than one letter, like "Google".

           3.Otherwise, we define that this word doesn't use capitals in a right way.

        检测一个string中的大写字母的对错,以下三种情况中,大写字母的格式是正确的。

      1. 整个string全部是大写字母。

      2.整个string全是小写字母。

      3.单词中首字母大写。

    2、问题分析

       首先处理特殊情况,输入的string 长度是0 或者 1的时候,返回 true。

      然后根据 前两个字母的大小分析。

     1 bool detectCapitalUse(string word)
     2     {
     3         if( word.size() == 0 || word.size() == 1 )
     4             return true;
     5         
     6         if( islower( word[0] ) )
     7         {
     8             for(int i = 1 ; i < word.size(); i++)
     9             {
    10                 if( isupper(word[i]) )
    11                     return false;
    12             }
    13         }
    14         
    15         if( isupper( word[0] )  && isupper(word[1]) )
    16         {
    17             for( int i = 2; i < word.size(); i++)
    18                 if( islower( word[i]) )
    19                     return false;
    20         }
    21         
    22         if( isupper(word[0])  && islower(word[1]) )
    23         {
    24             for(int i = 2; i< word.size(); i++)
    25                 if(isupper(word[i]))
    26                     return false;
    27         }      
    28         
    29         return true;
    30     }

     1. 如果第一个字母是小写,那么后续字母中出现 大写字母,就返回false.

     2.如果前两个字母都是大写,那么后续的字母再出现小写字母就返回false。

      3.如果第一个字母是大写,第二个字母是小写,那么后续再出现大写字母,就返回false。

    3、代码

    pp
  • 相关阅读:
    js中关于undefined值的判断
    解决flexpaper搜索文字时不能高亮的问题
    计算出当月还剩下几天
    仿新浪微博返回顶部的js实现(jQuery/MooTools)
    使用Javascript计算时间差和计算日期加天数后的日期值
    小tip:iframe高度动态自适应
    JQuery之ContextMenu(右键菜单)
    JS的split函数用法
    jquery context menu用法
    jQuery实现搜索关键字自动匹配提示方法
  • 原文地址:https://www.cnblogs.com/wangxiaoyong/p/8652229.html
Copyright © 2020-2023  润新知