zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    HDOJ 4276 The Ghost Blows Light
    Spring MVC程序中得到静态资源文件css,js,图片文件的路径问题总结
    $.ajax()方法详解
    jQuery Ajax 实例 ($.ajax、$.post、$.get)
    gitHub优秀android项目
    Android JSON
    SQL 设置自增,和default
    POST JSON fails with 415 Unsupported media type, SpringMVC
    23种设计模式
    转 Android_开源框架_AndroidUniversalImageLoader网络图片加载
  • 原文地址:https://www.cnblogs.com/wangxiaoyong/p/8652229.html
Copyright © 2011-2022 走看看