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:

  1. Input: "USA"
  2. Output: True

Example 2:

  1. Input: "FlaG"
  2. Output: False

Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.

  1. class Solution(object):
  2. def detectCapitalUse(self, word):
  3. """
  4. :type word: str
  5. :rtype: bool
  6. """
  7. n=len(word)
  8. if word[0].isupper():
  9. if n>1:
  10. if word[1].isupper():
  11. for i in word[2:]:
  12. if i.islower():
  13. return False
  14. return True
  15. else:
  16. for i in word[2:]:
  17. if i.isupper():
  18. return False
  19. return True
  20. else:
  21. return True
  22. else:
  23. if n>1:
  24. for i in word[1:]:
  25. if i.isupper():
  26. return False
  27. return True
  28. else:
  29. return True

  

[LeetCode&Python] Problem 520. Detect Capital的更多相关文章

  1. 【leetcode】520. Detect Capital

    problem 520. Detect Capital 题意: 题目中给出的三种情况,分别是全是大写.全是小写.首字母大写,这三种情况返回True;否则返回False; solution: class ...

  2. 50. leetcode 520. Detect Capital

    520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right or ...

  3. Leetcode 520. Detect Capital 发现大写词 (字符串)

    Leetcode 520. Detect Capital 发现大写词 (字符串) 题目描述 已知一个单词,你需要给出它是否为"大写词" 我们定义的"大写词"有下 ...

  4. 520. Detect Capital【easy】

    520. Detect Capital[easy] Given a word, you need to judge whether the usage of capitals in it is rig ...

  5. 【LeetCode】520. Detect Capital 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 循环判断三个条件 大写字母个数和位置判断 根据首字符 ...

  6. LeetCode 520 Detect Capital 解题报告

    题目要求 Given a word, you need to judge whether the usage of capitals in it is right or not. We define ...

  7. LeetCode - 520. Detect Capital

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

  8. LeetCode: 520 Detect Capital(easy)

    题目: Given a word, you need to judge whether the usage of capitals in it is right or not. We define t ...

  9. 520. Detect Capital

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

随机推荐

  1. Sublime Text 3 + phpfmt 插件

    Sublime Text 3 + phpfmt 插件 phpfmt 插件参数 ```json{ "autocomplete": true, "enable_auto_al ...

  2. ActiveMQ queue和topic,持久订阅和非持久订阅

    消息的 destination 分为 queue 和 topic,而消费者称为 subscriber(订阅者).queue 中的消息只会发送给一个订阅者,而 topic 的消息,会发送给每一个订阅者. ...

  3. python格式化日期

    #!/usr/bin/python # -*- coding: UTF-8 -*- import time import calendar """ 时间元组(年.月.日. ...

  4. Python isspace() 方法检测字符串是否只由空格组成。

  5. idea中deBug方法

    1 2设置controller层断点鼠标左键点击,断点在哪里,就会deBug到哪里 3刷新页面 4查看 5service层设置断点 6 7查看返回信息是否错误

  6. valgrind 工具介绍和简单的使用

    最近老是遇上各种奇奇怪怪的core dump,不太会分析的情况下看到了这款工具.在这记录分享下. Valgrind 是个开源的工具,功能很多.例如检查内存泄漏工具---memcheck. Valgri ...

  7. laravel中的plicy授权方法:

    1.用命令新建policy: php artisan make:policy PostPolicy 2.在app/Policies/PostPolicy.php中添加处理文件的权限的方法: //修改: ...

  8. xStream解析生成xml文件学习资料

    参考链接: http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html

  9. linux 系统监控、诊断工具之 lsof 用法简介

    1.lsof 简介 lsof 是 Linux 下的一个非常实用的系统级的监控.诊断工具. 它的意思是 List Open Files,很容易你就记住了它是 "ls + of"的组合 ...

  10. FPGA的GTP(aurora 协议)高速串行接口数据收发(转)

    reference:https://blog.csdn.net/qq_40261818/article/details/83039829 PG046-Aurora 8B/10B  Logicore I ...