Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a250e20

Browse files
✨feat: Add 520
1 parent 1baad2b commit a250e20

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
| [492. 构造矩形](https://leetcode-cn.com/problems/construct-the-rectangle/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/construct-the-rectangle/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-7ser/) | 简单 | 🤩🤩🤩🤩 |
4646
| [495. 提莫攻击](https://leetcode-cn.com/problems/teemo-attacking/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/teemo-attacking/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-gteh/) | 简单 | 🤩🤩🤩🤩🤩 |
4747
| [500. 键盘行](https://leetcode-cn.com/problems/keyboard-row/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/keyboard-row/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-zx6b/) | 简单 | 🤩🤩🤩🤩 |
48+
| [520. 检测大写字母](https://leetcode-cn.com/problems/detect-capital/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/detect-capital/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-rpor/) | 简单 | 🤩🤩🤩🤩 |
4849
| [528. 按权重随机选择](https://leetcode-cn.com/problems/random-pick-with-weight/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-pick-with-weight/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-8bx50/) | 中等 | 🤩🤩🤩🤩 |
4950
| [541. 反转字符串 II](https://leetcode-cn.com/problems/reverse-string-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-string-ii/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-p88f/) | 简单 | 🤩🤩🤩🤩🤩 |
5051
| [551. 学生出勤记录 I](https://leetcode-cn.com/problems/student-attendance-record-i/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/student-attendance-record-i/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-hui7/) | 简单 | 🤩🤩🤩 |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[520. 检测大写字母](https://leetcode-cn.com/problems/detect-capital/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-rpor/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
我们定义,在以下情况时,单词的大写用法是正确的:
10+
11+
* 全部字母都是大写,比如 "USA" 。
12+
* 单词中所有字母都不是大写,比如 "leetcode" 。
13+
* 如果单词不只含有一个字母,只有首字母大写, 比如 "Google" 。
14+
15+
给你一个字符串 word 。如果大写用法正确,返回 true ;否则,返回 false 。
16+
17+
示例 1:
18+
```
19+
输入:word = "USA"
20+
21+
输出:true
22+
```
23+
示例 2:
24+
```
25+
输入:word = "FlaG"
26+
27+
输出:false
28+
```
29+
提示:
30+
* 1 <= word.length <= 100
31+
* word 由小写和大写英文字母组成
32+
33+
---
34+
35+
### 模拟
36+
37+
根据题意,分别进行三种规则的判断即可。
38+
39+
代码:
40+
```Java
41+
class Solution {
42+
public boolean detectCapitalUse(String word) {
43+
if (word.toUpperCase().equals(word)) return true;
44+
if (word.toLowerCase().equals(word)) return true;
45+
int n = word.length(), idx = 1;
46+
if (Character.isUpperCase(word.charAt(0))) {
47+
while (idx < n && Character.isLowerCase(word.charAt(idx))) idx++;
48+
}
49+
return idx == n;
50+
}
51+
}
52+
```
53+
* 时间复杂度:$O(n),ドル常数为 5ドル$。
54+
* 空间复杂度:算法执行过程会产生新的字符串,复杂度为 $O(n)$
55+
56+
---
57+
58+
### 最后
59+
60+
这是我们「刷穿 LeetCode」系列文章的第 `No.520` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
61+
62+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
63+
64+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
65+
66+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
67+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /