Leetcode#520. Detect Capital

0人阅读 评论(0) 收藏 举报
分类:

题目描述:判断字符串的大小写。

  • 全部大写,返回true;
  • 全部小写,返回true;
  • 首字母大写,其余字母小写,返回true;
  • 否则,返回false;

解题思路:分几种情况考虑:

  • 首字母小写,或者首字母大写,第二个字母小写,若后面字符出现大写,返回 false;
  • 否则,若后面字母出现小写,返回false;
  • 否则,返回 true;
class Solution {
public:
    bool detectCapitalUse(string word) 
    {
        if((isupper(word[0]) && islower(word[1])) || islower(word[0]))
        {
            for(int i = 1; i < word.size(); ++i)
                if(isupper(word[i]))
                   return false;
        }
        else
        {
            for(int i = 1; i < word.size(); ++i)
                if(islower(word[i]))
                    return false;
        }
        return true;               
    }
};
查看评论

LeetCode之路:520. Detect Capital

LeetCode之路:520. Detect Capital 一、引言这道题有关于处理字符的大小写问题,对于熟悉字符的大小写处理函数非常有帮助。这里粘出题目信息: Given a word, yo...
  • u012814856
  • u012814856
  • 2017-04-18 17:52:31
  • 427

LeetCode-520. Detect Capital (JAVA)大写字母的合法性

LeetCode-520. Detect Capital (JAVA)大写字母的合法性
  • mine_song
  • mine_song
  • 2017-04-30 19:00:30
  • 730

LeetCode:Detect Capital

Given a word, you need to judge whether the usage of capitals in it is right or not.We define the us...
  • lwgechen
  • lwgechen
  • 2017-02-22 16:53:39
  • 629

LeetCode 520:Detect Capital (c++)

一:题目 Given a word, you need to judge whether the usage of capitals in it is right or not. We define...
  • u010059070
  • u010059070
  • 2017-06-21 16:34:07
  • 142

【LeetCode】520. Detect Capital【E】【83】

Given a word, you need to judge whether the usage of capitals in it is right or not. We define th...
  • sscssz
  • sscssz
  • 2017-02-22 19:03:54
  • 438

LeetCode 520. Detect Capital

520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right or...
  • liuchuo
  • liuchuo
  • 2017-02-21 13:48:58
  • 1349

简单的string问题解决:Detect Capital

直接列出题目如下: Given a word, you need to judge whether the usage of capitals in it is right or not. ...
  • WaverDn
  • WaverDn
  • 2017-02-25 20:54:04
  • 114

520.Detect Capital(String-Easy)

Given a word, you need to judge whether the usage of capitals in it is right or not.
  • c406495762
  • c406495762
  • 2017-06-20 12:36:59
  • 1325

leetcode 520 Detect Capital C++

这道题我用了从后往前的思路。 如果当前是小写的,上一个是大写的,直接false,否则continue 如果当前是大写的,上一个是大写的,continue,否则看当前是不是首尾,不是返回false。 6...
  • a2331046
  • a2331046
  • 2017-08-19 20:33:12
  • 194

【LeetCode】520. Detect Capital

问题描述问题链接:https://leetcode.com/problems/single-number/#/descriptionGiven a word, you need to judge wh...
  • mrbcy
  • mrbcy
  • 2017-03-17 11:08:33
  • 1073
    个人资料
    持之以恒
    等级:
    访问量: 3万+
    积分: 2118
    排名: 2万+