当前位置:网站首页>520. Detect capital letters

520. Detect capital letters

2022-04-23 04:32:00 Zhang Joshua

520. Detect capital letters

  • Question no : Power button 520
  • Knowledge point : character string
  • Goal completion :20/150
  • summary
    stem :
     Insert picture description here

Ideas :

  • 1. First judge the case of the first letter , If the first letter is lowercase , Then the following must be lowercase , If it appears directly after False
  • 2. If the first letter is capitalized , Look at the case of the second letter , If the second letter is lowercase , Then the following must be lowercase , If it appears directly after False
  • 3. If the second letter is capitalized , Then the following must be all capitalized , If lowercase appears after it, it directly returns False
#
# @lc app=leetcode.cn id=520 lang=python3
#
# [520]  Detect capital letters 
#

# @lc code=start
class Solution:
    def detectCapitalUse(self, word: str) -> bool:
        a = ord('a')
        z = ord('z')
        A = ord('A')
        Z = ord('Z')
        if len(word) <= 1:
            return True
        if a<=ord(word[0])<=z:
            for i in range(len(word)):
                if A<=ord(word[i])<=Z:
                    return False
            return True
        elif A<=ord(word[0])<=Z:
            if a<=ord(word[1])<=z:
                for j in range(1, len(word)):
                    if A<=ord(word[j])<=Z:
                        return False
                return True
            for l in range(1, len(word)):
                    if a<=ord(word[l])<=z:
                        return False
            return True
# @lc code=end


版权声明
本文为[Zhang Joshua]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230415046514.html