首页 > 其他分享 >leetcode-520-easy

leetcode-520-easy

时间:2022-11-08 20:00:27浏览次数:37  
标签:return charAt Character isUpperCase 520 easy word c1 leetcode

Detect Capital

We define the usage of capitals in a word to be right when one of the following cases holds:

All letters in this word are capitals, like "USA".
All letters in this word are not capitals, like "leetcode".
Only the first letter in this word is capital, like "Google".
Given a string word, return true if the usage of capitals in it is right.

Example 1:

Input: word = "USA"
Output: true
Example 2:

Input: word = "FlaG"
Output: false
Constraints:

1 <= word.length <= 100
word consists of lowercase and uppercase English letters.

思路一:按照题目意思,对长度大于 1 的字符串进行完全分类,只可能有四种情况,对这四种情况依次判断即可

public boolean detectCapitalUse(String word) {
    if (word.length() == 1) return true;

    char c1 = word.charAt(0);
    char c2 = word.charAt(1);

    if (Character.isUpperCase(c1) && Character.isUpperCase(c2)) {
        for (int i = 2; i < word.length(); i++) {
            if (Character.isLowerCase(word.charAt(i))) {
                return false;
            }
        }
    } else if ((Character.isLowerCase(c1) && Character.isLowerCase(c2)) ||
            Character.isUpperCase(c1) && Character.isLowerCase(c2)) {
        for (int i = 2; i < word.length(); i++) {
            if (Character.isUpperCase(word.charAt(i))) {
                return false;
            }
        }
    } else {
        return false;
    }

    return true;
}

标签:return,charAt,Character,isUpperCase,520,easy,word,c1,leetcode
From: https://www.cnblogs.com/iyiluo/p/16870940.html

相关文章

  • leetcode-1784-easy
    CheckifBinaryStringHasatMostOneSegmentofOnesGivenabinarystringswithoutleadingzeros,returntrueifscontainsatmostonecontiguoussegment......
  • leetcode-819-easy
    MostCommonWordGivenastringparagraphandastringarrayofthebannedwordsbanned,returnthemostfrequentwordthatisnotbanned.Itisguaranteedthe......
  • LeetCode 435. Non-overlapping Intervals
    贪心按照有边界排序,只有先选择了右边边界小的,才可以放下更多的区间classSolution{public:interaseOverlapIntervals(vector<vector<int>>&intervals){......
  • leetcode-1260-easy
    Shift2DGridGivena2Dgridofsizemxnandanintegerk.Youneedtoshiftthegridktimes.Inoneshiftoperation:Elementatgrid[i][j]movestogrid......
  • leetcode-561-easy
    ArrayPartitionGivenanintegerarraynumsof2nintegers,grouptheseintegersintonpairs(a1,b1),(a2,b2),...,(an,bn)suchthatthesumofmin(ai,bi......
  • leetcode-1854-easy
    MaximumPopulationYearYouaregivena2Dintegerarraylogswhereeachlogs[i]=[birthi,deathi]indicatesthebirthanddeathyearsoftheithperson.The......
  • leetcode-1684-easy
    CounttheNumberofConsistentStringsYouaregivenastringallowedconsistingofdistinctcharactersandanarrayofstringswords.Astringisconsistenti......
  • LeetCode 135. Candy
    贪心算法贪心策略:在每次遍历中,仅考虑并更新相邻一侧的大小关系classSolution{public:intcandy(vector<int>&ratings){intsize=ratings.size();......
  • LeetCode 455. Assign Cookies
    贪心classSolution{public:intfindContentChildren(vector<int>&g,vector<int>&s){sort(g.begin(),g.end());sort(s.begin(),s.end());......
  • Leetcode练题系列(六): 字符串相关的算法
    LeetCode  ​​英文官网(推荐)​​  ​​中文官网​​  从2016年大二左右开始就接触算法,起初也简单练习过,但现在工作一段时间后,随着代码水平的提高(​​自我感觉​​)......