问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 的时间复杂度也应当为:  ,因为使用了部分运行库,不能简单的认为它的时间复杂度为  。

最新文章

  1. int与CString互相转化
  2. HDU 3966 Aragorn&#39;s Story 树链剖分
  3. python之OS模块详解
  4. ads 错误
  5. Proactor设计模式:单线程高并发
  6. java 实现视频转换通用工具类:视频相互转换-总方法及Mencoder(二)
  7. Object-C内存管理
  8. 谈谈单元測试之(二):測试工具 JUnit 3
  9. /etc/nginx/nginx.conf配置文件详解
  10. python/*args和**kwargs
  11. Dockerfile详解及优化
  12. 用理论告诉你 三极管和MOS管的区别在哪
  13. thinkphp自动创建数据对象分析
  14. June.19 2018, Week 25th Tuesday
  15. Egg入门学习(二)---理解service作用
  16. [C++]Linux之C编程异常[true未定义解决方案]
  17. 总结:Java 集合进阶精讲1
  18. Solr之.net操作
  19. EntityFramework异常The specified cast from a materialized &#39;System.Double&#39; type to the &#39;System.Single&#39; type is not valid.
  20. 关于Android中50M+的文本入库处理细节

热门文章

  1. row_number() over()排序功能说明
  2. scratch编程体感游戏
  3. 设计模式:Iterator模式
  4. ”initialization failure:0x0000000C“错误,何解?
  5. 【JVM之内存与垃圾回收篇】执行引擎
  6. 切换npm源的几种方法
  7. Win7安装Python失败 提示Setup failed
  8. 全卷积神经网络FCN详解(附带Tensorflow详解代码实现)
  9. python关于字符编码的基本操作
  10. 面试官:如何在Integer类型的ArrayList中同时添加String、Character、Boolean等类型的数据? | Java反射高级应用