博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 520. Detect Capital
阅读量:5925 次
发布时间:2019-06-19

本文共 1654 字,大约阅读时间需要 5 分钟。

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:

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.

解法1:

class Solution(object):    def detectCapitalUse(self, word):        """        :type word: str        :rtype: bool        """        """        USA=True        CN=True        C=False?        U=False?        ""=?        usa=True        Usa=True        uSa=False        """        # get first letter:        # if first letter is Uppaer case: check all the following letter is Lower case or upper case        # if first letter is lower case: check all the following letter is lower case        # otherwise, False        if word[0].islower(): return all(c.islower() for c in word[1:])        else: return all(c.isupper() for c in word[1:]) or all(c.islower() for c in word[1:])

换一种思维:大写字母个数==0或者==1个(仅为首字母)或者字符串长度!

class Solution(object):    def detectCapitalUse(self, word):        """        :type word: str        :rtype: bool                """        upper_cnt = 0        for c in word:            if c.isupper(): upper_cnt += 1        return upper_cnt == 0 or (upper_cnt == 1 and word[0].isupper()) or upper_cnt == len(word)

 

 

转载地址:http://qyavx.baihongyu.com/

你可能感兴趣的文章
C# 删除Collections中的重复数据
查看>>
NFS 网络系统配置及自动挂载
查看>>
corosync(openais)+drbd+pacemaker实现mysql服务器的高可用性群集
查看>>
centos 6.2 硬盘安装(双系统)
查看>>
RHEL6.2 64位系统Virtualbox虚拟机下安装过程
查看>>
Linux中文件查找——find命令
查看>>
How to Install Apache Kafka on CentOS 7
查看>>
Exchange 2016 将邮箱数据库排除
查看>>
正式学习React(四) 前序篇
查看>>
yum安装mysql
查看>>
如何在fedora 16下配置×××连接
查看>>
linux下cache和buffer的使用情况
查看>>
多余的拼音导致Python的数据类型错误
查看>>
前端开发知识之前端移动端适配总结
查看>>
Matrix
查看>>
Apache Spark源码走读之18 -- 使用Intellij idea调试Spark源码
查看>>
VMware 中如何打开U盘弹出U盘或者移动硬盘的(两种方法)
查看>>
南阳38--布线问题
查看>>
通过jsp请求Servlet来操作HBASE
查看>>
Learn Python 012: for loop
查看>>