鱼C论坛

 找回密码
 立即注册
查看: 1777|回复: 0

[学习笔记] Leetcode 520. Detect Capital

[复制链接]
发表于 2020-8-2 03:39:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Given a word, you need to judge whether the usage of capitals in it is right or not.

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

  3. All letters in this word are capitals, like "USA".
  4. All letters in this word are not capitals, like "leetcode".
  5. Only the first letter in this word is capital, like "Google".
  6. Otherwise, we define that this word doesn't use capitals in a right way.


  7. Example 1:

  8. Input: "USA"
  9. Output: True


  10. Example 2:

  11. Input: "FlaG"
  12. Output: False


  13. Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters
复制代码

  1. class Solution:
  2.     def detectCapitalUse(self, word: str) -> bool:
  3.         small = False
  4.         count_B = 0
  5.         count_S = 0
  6.         if 97 <= ord(word[0]) <= 122:
  7.             small = True
  8.         else:
  9.             small = False
  10.         for i in range(1, len(word)):
  11.             if small == True:
  12.                 if not 97 <= ord(word[i]) <= 122:
  13.                     return False
  14.                 else:
  15.                     count_S += 1
  16.             else:
  17.                 if 97 <= ord(word[i]) <= 122:
  18.                     count_S += 1
  19.                 elif 65 <= ord(word[i]) <= 90:
  20.                     count_B += 1
  21.         if small == True:
  22.             return count_S == len(word) - 1
  23.         else:
  24.             return count_S == len(word) - 1 or count_B == len(word) - 1
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 22:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表