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 #34

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 22 commits into AlgorithmAndLeetCode:main from itcharge:main
Aug 26, 2022
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
096cb11
Update 0167. 两数之和 II - 输入有序数组.md
itcharge Aug 24, 2022
dee78d6
Update 0473. 火柴拼正方形.md
itcharge Aug 25, 2022
70eb9e0
Update 0003. 无重复字符的最长子串.md
itcharge Aug 25, 2022
4073c18
Update 0011. 盛最多水的容器.md
itcharge Aug 25, 2022
f95ab04
Update 0015. 三数之和.md
itcharge Aug 25, 2022
4e75720
Update 0026. 删除有序数组中的重复项.md
itcharge Aug 25, 2022
3b372d4
Update 0080. 删除有序数组中的重复项 II.md
itcharge Aug 25, 2022
4fa1bb6
Update 0125. 验证回文串.md
itcharge Aug 25, 2022
b5c9af4
Update 0167. 两数之和 II - 输入有序数组.md
itcharge Aug 25, 2022
14c770a
Update 0344. 反转字符串.md
itcharge Aug 25, 2022
1084fe7
Update 0349. 两个数组的交集.md
itcharge Aug 25, 2022
7a28f76
Update 1343. 大小为 K 且平均值大于等于阈值的子数组数目.md
itcharge Aug 25, 2022
b3e6cee
Update 01.Array-Two-Pointers.md
itcharge Aug 25, 2022
910d51d
Update 0027. 移除元素.md
itcharge Aug 26, 2022
2d26e41
Update 0209. 长度最小的子数组.md
itcharge Aug 26, 2022
6d0fabe
Update 0220. 存在重复元素 III.md
itcharge Aug 26, 2022
21d9781
Update 0283. 移动零.md
itcharge Aug 26, 2022
cbe4524
Update 0674. 最长连续递增序列.md
itcharge Aug 26, 2022
31eae85
Update 0713. 乘积小于K的子数组.md
itcharge Aug 26, 2022
5662489
Update 1004. 最大连续1的个数 III.md
itcharge Aug 26, 2022
dd52597
Update 01.Array-Sliding-Window.md
itcharge Aug 26, 2022
b36df61
更新题解列表
itcharge Aug 26, 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
Update 0344. 反转字符串.md
  • Loading branch information
itcharge committed Aug 25, 2022
commit 14c770a50995b1458ca77ecde38d53f90f9a98a2
45 changes: 38 additions & 7 deletions Solutions/0344. 反转字符串.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,49 @@

## 题目大意

给定一个字符串数组,将其反转。要求不能使用额外的数组空间。
**描述**:给定一个字符数组 `s`。

**要求**:将其反转。

**说明**:

- 不能使用额外的数组空间,必须原地修改输入数组、使用 $O(1)$ 的额外空间解决问题。
- 1ドル \le s.length \le 10^5$。
- `s[i]` 都是 ASCII 码表中的可打印字符。

**示例**:

```Python
输入:s = ["h","e","l","l","o"]
输出:["o","l","l","e","h"]


输入:s = ["H","a","n","n","a","h"]
输出:["h","a","n","n","a","H"]
```

## 解题思路

简单题,遍历字符串,将 s[i] 与 s[len-1-i] 交换即可。
### 思路 1:对撞指针

## 代码
1. 使用两个指针 `left`,`right`。`left` 指向字符数组开始位置,`right` 指向字符数组结束位置。
2. 交换 `s[left]` 和 `s[right]`,将 `left` 右移、`right` 左移。
3. 如果遇到 `left == right`,跳出循环。

### 思路 1:代码

```Python
def reverseString(self, s: List[str]) -> None:
n = len(s)
for i in range(n//2):
s[i], s[n-1-i] = s[n-1-i], s[i]
class Solution:
def reverseString(self, s: List[str]) -> None:
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
```

### 思路 1:复杂度分析

- **时间复杂度**:$O(n)$。
- **空间复杂度**:$O(1)$。

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