520. Detect Capital
来源:互联网 发布:欧陆风云4 steam Mac 编辑:程序博客网 时间:2024/06/05 21:57
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”.
Python字符串有两个方法:isupper()和islower() 可以判断字符串是否为小写/大写。如果用这个方法,则程序为:
class Solution(object): def detectCapitalUse(self, word): """ :type word: str :rtype: bool """ return word is not None and ( \ word.isupper() or \ word.islower() or \ (word[0].isupper() and word[1:].islower()))# word = "USA"# word = 'leetcode'# word = 'Google'word = 'FlaG'r = Solution().detectCapitalUse(None)print(r)
如果不偷懒,不用Python自带的方法,那么需要自己写函数判断是否是大小写。程序如下:
class Solution(object): def ifAllUpper(self, word): for i in word: if i >= 'a' and i <= 'z': return False return True def ifAllLower(self, word): for i in word: if i >= 'A' and i <= 'Z': return False return True def ifOnlyFirstUpper(self, word): return self.ifAllUpper(word[0]) and self.ifAllLower(word[1:]) def detectCapitalUse(self, word): """ :type word: str :rtype: bool """ return word is not None and ( \ self.ifAllUpper(word) or \ self.ifAllLower(word) or \ self.ifOnlyFirstUpper(word))
0 0
- 【Leetcode】520. Detect Capital
- 520. Detect Capital (E)
- LeetCode 520. Detect Capital
- LeetCode 520. Detect Capital
- 520. Detect Capital
- LeetCode-520. Detect Capital
- 520. Detect Capital
- leetcode 520. Detect Capital
- 520. Detect Capital
- 520. Detect Capital
- 520. Detect Capital
- 520. Detect Capital
- leetcode 520. Detect Capital
- [leetcode] 520. Detect Capital
- 520. Detect Capital Easy
- 520. Detect Capital
- [LeetCode]520. Detect Capital
- 520. Detect Capital
- 实验:C++实验4-项目6
- jqgrid格式化字段
- Nodejs的HTTP
- iOS调用拨打电话
- 快速调试你的Python程序
- 520. Detect Capital
- 中国游客到哪里,移动支付去哪里
- Ubuntu上卸载 anaconda
- iOS AES128加解密
- 第四次作业
- iOS AFNETWorkIng的封装类
- 常见的异步实现方法以及ES7中如何通过async实现异步操作
- C++第4次作业
- java并发包:几个多线程控制工具类