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
  • 相关阅读:
    SQL判断如果一列值为null则取另一列值代替 isnull()
    关于js的function.来自百度知道的回答,学习了.
    OSI七层与TCP/IP五层网络架构
    504 Gateway Time-out
    nginx中关于并发数的问题worker_connections,worker_processes
    php实现二叉树的遍历
    nginx负载均衡的简单实现
    linux shell数据重定向
    数据库范式的思考以及数据库的设计
    msyql中myism和innodb的区别
  • 原文地址:https://www.cnblogs.com/wangxiaoyong/p/8652229.html
Copyright © 2011-2022 走看看