python--leetcode.Detect Capital
来源:互联网 发布:HIS系统数据库类型 编辑:程序博客网 时间:2024/06/07 10:32
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:
- 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 if it has more than one letter, like "Google".
Example 1:
Input: "USA"Output: True
Example 2:
Input: "FlaG"Output: False
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
题目意思就是给你一个字符串,判断它是不是全为大写或小写。或者开头为大写其他为小写。
解题思路:把该字符串全部转化为大写或小写,判断跟原字符串是否相等。
class Solution(object): def detectCapitalUse(self, word): """ :type word: str :rtype: bool """ if(word.lower()== word): return True elif (word.upper()==word) : return True elif(word[0].upper()==word[0] and word[1:].lower()==word[1:]):return True else:return Falses=Solution()print(s.detectCapitalUse('Fflk'))用python的库可以更方便地解决这一题....一行代码:
def detectCapitalUse(self, word): return word.isupper() or word.islower() or word.istitle()
阅读全文
1 0
- python--leetcode.Detect Capital
- [leetcode: Python]520. Detect Capital
- 【Leetcode】520. Detect Capital
- LeetCode 520. Detect Capital
- leetcode-520-Detect Capital
- LeetCode 520. Detect Capital
- LeetCode:Detect Capital
- LeetCode-520. Detect Capital
- Leetcode #520 Detect Capital
- leetcode 520. Detect Capital
- leetcode 520. Detect Capital
- [leetcode] 520. Detect Capital
- [LeetCode]520 Detect Capital
- [LeetCode]520. Detect Capital
- LeetCode | 520. Detect Capital
- 【LeetCode】520. Detect Capital
- LeetCode 520. Detect Capital
- leetcode 520. Detect Capital
- source insight 配置宏
- do while和while的区别
- Android Studio插件之快速findViewById butterknife和Android CodeGenerator的使用
- 扫描二维码
- 动态规划之字符串分词
- python--leetcode.Detect Capital
- Android使用自定义布局制作进度圆
- 在 CentOS7 上安装 Zookeeper-3.4.9 服务
- Pyinstaller使用教程
- 某道简单的crackme
- 射线获取坐标
- struts2项目启动Tomcat报严重: ContainerBase.addChild: start
- poj-2387 Til the Cows Come Home dijkstra
- Tunnel Warfare(hdu 1540)