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

[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
pull merged 13 commits into AlgorithmAndLeetCode:main from itcharge:main
Aug 9, 2022
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 Aug 8, 2022
006157d
Update 2235. 两整数相加.md
itcharge Aug 8, 2022
ea71e33
Update 1929. 数组串联.md
itcharge Aug 8, 2022
774639b
Update 1480. 一维数组的动态和.md
itcharge Aug 9, 2022
07af991
Create 0709. 转换成小写字母.md
itcharge Aug 9, 2022
3c66bb9
Create 1672. 最富有客户的资产总量.md
itcharge Aug 9, 2022
cf383a0
Update 0066. 加一.md
itcharge Aug 9, 2022
9a7f2dc
Update 0189. 轮转数组.md
itcharge Aug 9, 2022
26b605f
Update 0048. 旋转图像.md
itcharge Aug 9, 2022
efdb7d2
Update 0724. 寻找数组的中心下标.md
itcharge Aug 9, 2022
bb00113
Update 0054. 螺旋矩阵.md
itcharge Aug 9, 2022
75279ea
Update 0498. 对角线遍历.md
itcharge Aug 9, 2022
7aacbc3
更新「Leetcode 刷题课程第一期」内容
itcharge Aug 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create 0709. 转换成小写字母.md
  • Loading branch information
itcharge committed Aug 9, 2022
commit 07af991e6d5ff0e3b52b977b403c6ac8be50929e
77 changes: 77 additions & 0 deletions Solutions/0709. 转换成小写字母.md
View file Open in desktop
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)$。

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