博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
520. Detect Capital判断单词有效性
阅读量:4354 次
发布时间:2019-06-07

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

[抄题]:

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

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么表示大写字母:c <= 'Z'

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

c <= 'Z'

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

charAt 可以把字符串中的字母单独抽出来

[关键模板化代码]:

统计大写字母:

for (char c : word.toCharArray()) {            if (c <= 'Z') count++;        }

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

class Solution {    public boolean detectCapitalUse(String word) {        //ini        int count = 0;        for (char c : word.toCharArray()) {            if (c <= 'Z') count++;        }        if (count == 0 || count == word.length() || (count == 1 && word.charAt(0) <= 'Z')) {            return true;        }        return false;    }}
View Code

 

转载于:https://www.cnblogs.com/immiao0319/p/8641842.html

你可能感兴趣的文章
Navicat for MySQL10.1.7注册码
查看>>
Proxy模式
查看>>
读书多些会怎样
查看>>
浏览器好用的技术
查看>>
HDU 2188------巴什博弈
查看>>
tp5任务队列使用supervisor常驻进程
查看>>
Xmind?
查看>>
spring+quartz 实现定时任务三
查看>>
day2-三级菜单
查看>>
关于软件工程
查看>>
完成注册功能
查看>>
解决:父类中的@NotNull无效以及@Notnull 验证list对象无效
查看>>
ORA-01652: 无法通过 128 (在表空间 HIS_TABLESPACE_TEMP 中) 扩展 temp 段
查看>>
BIO,NIO与AIO的区别
查看>>
python将不同数据库多张表内容整合到一张表中
查看>>
软件工程第一次作业 - 导航
查看>>
对照实验(1)-批量清理系统临时文件
查看>>
GOF设计模式(02)抽象工厂模式
查看>>
Java NIO 详解(一)
查看>>
人类的奇怪之处
查看>>