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 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.所有字母都大写

2.所有字母都小写

3.首字母大写,其余都小写


4种情况:

1.如果字符串长度小于等于1,返回true;

2.如果第一个位置小写,以后出现大写,返回false

3.如果第一个位置大写,第二个位置大写,以后出现小写,返回false

4.如果第一个位置大写,第二个位置小写,以后出现大写,返回false


三:代码实现

思路一:

class Solution {
public:
    bool detectCapitalUse(string word) {
        
        if(word.length()<=1)
            return true;
            
        int i;
        bool first=false;  //true:第一个位置大写,false:第一个位置小写
        bool second=false; //true:第二个位置是大写, false:第二个位置小写
        if(word[0]>='A' && word[0]<='Z')
            first=true;
        if(word[1]>='A' && word[1]<='Z')
            second=true;
        //如果第一个位置小写,以后出现大写,返回false
        if(!first && second)
            return false;
        for(i=2;i<word.length();i++){
            //1.如果第一个位置小写,以后出现大写,返回false
            if(!first && !second && word[i]>='A' && word[i]<='Z')
                return false;
                
            //2.如果第一个位置大写,第二个位置大写,以后出现小写,返回false
             if(first && second && word[i]>='a' && word[i]<='z')
                return false;
                
            //3.如果第一个位置大写,第二个位置小写,以后出现大写,返回false
             if(first && !second && word[i]>='A' && word[i]<='Z')
                return false;
    
        }
        return true;
        
    }
};

思路二:

统计大写字母的个数

1.如果全为大写,最终大写字母的个数应该等于字符串的长度

2.如果首字母大写,其余为小写,则大写字母的个数为1

3.全为小写,大写字母个数为0

class Solution {
public:
    bool detectCapitalUse(string word) {
     
        int i;
        int numOfCapital=0;
        for(i=0;i<word.length();i++)
            if(word[i]>='A'  && word[i]<='Z')
                numOfCapital++;
        //全为小写
        if(numOfCapital==0)
            return true;
        //全为大写
        if(numOfCapital==word.length())
            return true;
        //首字母大写,其余小写
        if(word[0]>='A'  && word[0]<='Z' && numOfCapital==1)
            return true;
        return false;
        
    }
};

思路三(JAVA):

将单词转换为大写得到up,将单词转换为小写得到low,若word与up或与low相等,则返回true,
否则去掉word的首字母得到last,若last转换为小写后仍与last相等,则返回true,
否则返回false。

public boolean detectCapitalUse(String word) {
        int len=word.length();
        String up = word.toUpperCase();  
        String low = word.toLowerCase();  
        if (word.equals(up) || word.equals(low))  
            return true;  
         String last = word.substring(1, len);  
        if (last.toLowerCase().equals(last))  
           return true;  
         return false;  
    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值