3636检测大写字母

Posted 小雷FansUnion

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3636检测大写字母相关的知识,希望对你有一定的参考价值。

我们定义,在以下情况时,单词的大写用法是正确的:

全部字母都是大写,比如 "USA" 。
单词中所有字母都不是大写,比如 "leetcode" 。
如果单词不只含有一个字母,只有首字母大写, 比如 "Google" 。
给你一个字符串 word 。如果大写用法正确,返回 true ;否则,返回 false 。

示例 1:

输入:word = "USA"
输出:true
示例 2:

输入:word = "FlaG"
输出:false
 

提示:

1 <= word.length <= 100
word 由小写和大写英文字母组成

来源:力扣(LeetCode)
链接:力扣
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

package cn.fansunion.leecode.string;

/**

 * 检测大写字母

 * 我们定义,在以下情况时,单词的大写用法是正确的:

全部字母都是大写,比如 "USA" 。

单词中所有字母都不是大写,比如 "leetcode" 。

如果单词不只含有一个字母,只有首字母大写, 比如 "Google" 。

给你一个字符串 word 。如果大写用法正确,返回 true ;否则,返回 false 。

来源:力扣(LeetCode)

链接:力扣

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 * @author wen.lei@brgroup.com

 *

 * 2022-2-16

 */

public class DetectCapital

    /* 输入:word = "USA"

        输出:true

     

        输入:word = "FlaG"

        输出:false*/

    /**

     * 遍历字符串,用1个数组[]维护每个位置是大写还是小写,以及大写的总个数;根据上述2个字段+数组长度即可判定

     * @param word

     * @return

     */

    public boolean detectCapitalUse(String word)

        //遍历字符串,用1个数组[]维护每个位置是大写还是小写,以及大写的总个数

        final int length = word.length();

        int[] upperArray = new int[length];

        int upperCount=0;

        for(int index=0;index<length;index++)

            char c=word.charAt(index);

            boolean isUpper=Character.isUpperCase(c);

            if(isUpper)

                upperArray[index]=1;

                upperCount++;

            else 

                upperArray[index]=0;

            

        

        //"USA"

        if(upperCount==length)

            return true;

        

        //Word

        if(upperArray[0]==1 && upperCount==1)

            return true;

        

        //word

        if(upperCount==0)

            return true;

        

        //其它情况,比如“WorD”、“WOrd”

        return false;

    

     

    //官方解法,优先判定 “非法情况”;不符合我的思维习惯,弃坑

    public boolean detectCapitalUse2(String word)

        // 若第 1 个字母为小写,则需额外判断第 2 个字母是否为小写

        if (word.length() >= 2 && Character.isLowerCase(word.charAt(0)) && Character.isUpperCase(word.charAt(1)))

            return false;

        

         

        // 无论第 1 个字母是否大写,其他字母必须与第 2 个字母的大小写相同

        for (int i = 2; i < word.length(); ++i)

            if (Character.isLowerCase(word.charAt(i)) ^ Character.isLowerCase(word.charAt(1)))

                return false;

            

        

        return true;

    

package test.leecode.string;

import org.junit.Assert;

import org.junit.Test;

import cn.fansunion.leecode.string.DetectCapital;

/**

 * @author wen.lei@brgroup.com

 *

 * 2022-2-16

 */

public class DetectCapitalTest

    @Test

    public void test()

        DetectCapital cap = new DetectCapital();

        boolean trueAllUpper=cap.detectCapitalUse("USA");

        Assert.assertEquals(true,trueAllUpper);

        boolean trueAllLower=cap.detectCapitalUse("word");

        Assert.assertEquals(true,trueAllLower);

        boolean trueCaptial=cap.detectCapitalUse("Lei");

        Assert.assertEquals(true,trueCaptial);

        //other error

        boolean falseError1=cap.detectCapitalUse("LeiWen");

        Assert.assertEquals(false,falseError1);

    

以上是关于3636检测大写字母的主要内容,如果未能解决你的问题,请参考以下文章

520-检测大写字母

《LeetCode之每日一题》:206.检测大写字母

520. 检测大写字母

520-检测大写字母

520. 检测大写字母

520. 检测大写字母