forked from itcharge/AlgoNote
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] main from itcharge:main #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
60d8274
Update 0771. 宝石与石头.md
itcharge 006157d
Update 2235. 两整数相加.md
itcharge ea71e33
Update 1929. 数组串联.md
itcharge 774639b
Update 1480. 一维数组的动态和.md
itcharge 07af991
Create 0709. 转换成小写字母.md
itcharge 3c66bb9
Create 1672. 最富有客户的资产总量.md
itcharge cf383a0
Update 0066. 加一.md
itcharge 9a7f2dc
Update 0189. 轮转数组.md
itcharge 26b605f
Update 0048. 旋转图像.md
itcharge efdb7d2
Update 0724. 寻找数组的中心下标.md
itcharge bb00113
Update 0054. 螺旋矩阵.md
itcharge 75279ea
Update 0498. 对角线遍历.md
itcharge 7aacbc3
更新「Leetcode 刷题课程第一期」内容
itcharge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create 0709. 转换成小写字母.md
- Loading branch information
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
Solutions/0709. 转换成小写字母.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# [0709. 转换成小写字母](https://leetcode.cn/problems/to-lower-case/) | ||
|
||
- 标签:字符串 | ||
- 难度:简单 | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定一个字符串 `s`。 | ||
|
||
**要求**:将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。 | ||
|
||
**说明**: | ||
|
||
- 1ドル \le s.length \le 100$。 | ||
- `s` 由 ASCII 字符集中的可打印字符组成。 | ||
|
||
**示例**: | ||
|
||
```Python | ||
输入:s = "Hello" | ||
输出:"hello" | ||
|
||
|
||
输入:s = "LOVELY" | ||
输出:"lovely" | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:直接模拟 | ||
|
||
- 大写字母 `A` ~ `Z` 的 ASCII 码范围为 `[65, 90]`。 | ||
- 小写字母 `a` ~ `z` 的 ASCII 码范围为 `[97, 122]`。 | ||
|
||
将大写字母的 ASCII 码加 `32`,就得到了对应的小写字母,则解决步骤如下: | ||
|
||
1. 使用一个字符串变量 `ans` 存储最终答案字符串。 | ||
2. 遍历字符串 `s`,对于当前字符 `ch`: | ||
1. 如果 `ch` 的 ASCII 码范围在 `[65, 90]`,则说明 `ch` 为大写字母。将 `ch` 的 ASCII 码增加 `32`,再转换为对应的字符,存入字符串 `ans` 的末尾。 | ||
2. 如果 `ch` 的 ASCII 码范围不在 `[65, 90]`,则说明 `ch` 为小写字母。直接将 `ch` 存入字符串 `ans` 的末尾。 | ||
3. 遍历完字符串 `s`,返回答案字符串 `ans`。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def toLowerCase(self, s: str) -> str: | ||
ans = "" | ||
for ch in s: | ||
if ord('A') <= ord(ch) <= ord('Z'): | ||
ans += chr(ord(ch) + 32) | ||
else: | ||
ans += ch | ||
return ans | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n)$。一重循环遍历的时间复杂度为 $O(n)$。 | ||
- **空间复杂度**:$O(n)$。如果算上答案数组的空间占用,则空间复杂度为 $O(n)$。不算上则空间复杂度为 $O(1)$。 | ||
|
||
### 思路 2:使用 API | ||
|
||
`Python` 语言中自带大写字母转小写字母的 API:`lower()`,用 API 转换完成之后,直接返回新的字符串。 | ||
|
||
### 思路 2:代码 | ||
|
||
```Python | ||
class Solution: | ||
def toLowerCase(self, s: str) -> str: | ||
return s.lower() | ||
``` | ||
|
||
### 思路 2:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n)$。一重循环遍历的时间复杂度为 $O(n)$。 | ||
- **空间复杂度**:$O(n)$。如果算上答案数组的空间占用,则空间复杂度为 $O(n)$。不算上则空间复杂度为 $O(1)$。 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.