问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3947 访问。

给定一个单词,你需要判断单词的大写使用是否正确。

我们定义,在以下情况时,单词的大写用法是正确的:

全部字母都是大写,比如"USA"。

单词中所有字母都不是大写,比如"leetcode"。

如果单词不只含有一个字母,只有首字母大写, 比如 "Google"。

否则,我们定义这个单词没有正确使用大写字母。

输入: "USA"

输出: True

输入: "FlaG"

输出: False

注意: 输入是由大写和小写拉丁字母组成的非空单词。


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".

Otherwise, we define that this word doesn't use capitals in a right way.

Input: "USA"

Output: True

Input: "FlaG"

Output: False

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


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3947 访问。

public class Program {

    public static void Main(string[] args) {
var word = "isA"; var res = DetectCapitalUse(word);
Console.WriteLine(res); word = "Google"; res = DetectCapitalUse2(word);
Console.WriteLine(res); Console.ReadKey();
} private static bool DetectCapitalUse(string word) {
//定义结果
var res = string.Empty;
//大写字母用1表示,小写字母用0表示
foreach(var c in word) {
if(c >= 'a' && c <= 'z') res += '0';
else res += '1';
}
//去除前导0为空,表示全小写
//去除前导1为空,表示全大写
//去除后导0为1,表示首字母大写
return res.TrimStart('0') == "" ||
res.TrimStart('1') == "" ||
res.TrimEnd('0') == "1";
} private static bool DetectCapitalUse2(string word) {
//直接判定
return word == word.ToLower() ||
word == word.ToUpper() ||
(word[0].ToString() == word[0].ToString().ToUpper() &&
word.Substring(1) == word.Substring(1).ToLower());
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3947 访问。

False
True

分析:

DetectCapitalUse 的时间复杂度为  ,DetectCapitalUse2 的时间复杂度也应当为:  ,因为使用了部分运行库,不能简单的认为它的时间复杂度为  。

C#LeetCode刷题之#520-检测大写字母(Detect Capital)的更多相关文章

  1. [Swift]LeetCode520. 检测大写字母 | Detect Capital

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

  2. Java实现 LeetCode 520 检测大写字母

    520. 检测大写字母 给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是 ...

  3. 力扣(LeetCode)520. 检测大写字母

    给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"l ...

  4. C#LeetCode刷题之#242-有效的字母异位词(Valid Anagram)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4040 访问. 给定两个字符串 s 和 t ,编写一个函数来判断 ...

  5. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  6. C#LeetCode刷题-排序

    排序篇 # 题名 刷题 通过率 难度 56 合并区间   31.2% 中等 57 插入区间   30.4% 困难 75 颜色分类   48.6% 中等 147 对链表进行插入排序   50.7% 中等 ...

  7. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  8. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  9. LeetCode刷题总结-树篇(中)

    本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...

随机推荐

  1. 记录一次升级ant-design-vue的遇见的bug

    记录一次升级ant-design-vue的遇见的bug 使用版本: "version": "2.5.2" "ant-design-vue": ...

  2. 痞子衡嵌入式:SNVS Master Key仅在i.MXRT10xx Hab关闭时才能用于DCP加解密

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT系列中数据协处理器DCP使用SNVS Master Key加解密的注意事项. i.MXRT不仅仅是处理性能超强的MCU,也是 ...

  3. NIO实践-HTTP交互实现暨简版Tomcat交互内核

    今天就NIO实现简单的HTTP交互做一下笔记,进而来加深Tomcat源码印象. 一.关于HTTP 1.HTTP的两个显著特点,HTTP是一种可靠的超文本传输协议 第一.实际中,浏览器作为客户端,每次访 ...

  4. 【Python】Async异步等待简单例子理解

    import time def run(coroutine): try: print("") coroutine.send(None) except StopIteration a ...

  5. Spark 3.0 新特性 之 自适应查询与分区动态裁剪

    Spark憋了一年半的大招后,发布了3.0版本,新特性主要与Spark SQL和Python相关.这也恰恰说明了大数据方向的两大核心:BI与AI.下面是本次发布的主要特性,包括性能.API.生态升级. ...

  6. Python实现数据结构 图

    邻接矩阵 class Vertex: def __init__(self, node): self.id = node # Mark all nodes unvisited self.visited ...

  7. shell动态向sql传参

    一直在想有什么好方法可以实现,用shell动态给sql传参,自己写了一个简单,有什么好方法,欢迎留言补充,下面代码纯手打,可能有疏忽之处,请大佬批评指正指正. 实现方法如下: 1.新建一个文件02.t ...

  8. PHP get_defined_vars() 函数

    get_defined_vars() 函数返回由所有已定义变量所组成的数组. 版本要求:PHP 4 >= 4.0.4, PHP 5, PHP 7高佣联盟 www.cgewang.com 语法 a ...

  9. PHP getNamespaces() 函数

    实例 返回 XML 文档中使用的命名空间: <?php$xml=<<<XML高佣联盟 www.cgewang.com<?xml version="1.0&quo ...

  10. Skill 扫描list中是否含有某元素

    https://www.cnblogs.com/yeungchie/ code procedure(ycInListp(scan keylist) prog((times) times = 0 for ...