mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
更新 0541.反转字符串 排版格式修复
This commit is contained in:
@ -23,9 +23,11 @@
|
|||||||
输入: s = "abcdefg", k = 2
|
输入: s = "abcdefg", k = 2
|
||||||
输出: "bacdfeg"
|
输出: "bacdfeg"
|
||||||
|
|
||||||
# 思路
|
## 算法公开课
|
||||||
|
|
||||||
针对本题,我录制了视频讲解:[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),结合本题解一起看,事半功倍!
|
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串操作进阶! | LeetCode:541. 反转字符串II](https://www.bilibili.com/video/BV1dT411j7NN),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||||
|
|
||||||
|
## 思路
|
||||||
|
|
||||||
这道题目其实也是模拟,实现题目中规定的反转规则就可以了。
|
这道题目其实也是模拟,实现题目中规定的反转规则就可以了。
|
||||||
|
|
||||||
@ -42,8 +44,6 @@
|
|||||||
|
|
||||||
那么这里具体反转的逻辑我们要不要使用库函数呢,其实用不用都可以,使用reverse来实现反转也没毛病,毕竟不是解题关键部分。
|
那么这里具体反转的逻辑我们要不要使用库函数呢,其实用不用都可以,使用reverse来实现反转也没毛病,毕竟不是解题关键部分。
|
||||||
|
|
||||||
# C++代码
|
|
||||||
|
|
||||||
使用C++库函数reverse的版本如下:
|
使用C++库函数reverse的版本如下:
|
||||||
|
|
||||||
```CPP
|
```CPP
|
||||||
@ -129,7 +129,7 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
C:
|
### C:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
char * reverseStr(char * s, int k){
|
char * reverseStr(char * s, int k){
|
||||||
@ -152,7 +152,7 @@ char * reverseStr(char * s, int k){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Java:
|
### Java:
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
//解法一
|
//解法一
|
||||||
@ -256,7 +256,8 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Python:
|
### Python:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def reverseStr(self, s: str, k: int) -> str:
|
def reverseStr(self, s: str, k: int) -> str:
|
||||||
@ -281,7 +282,7 @@ class Solution:
|
|||||||
return ''.join(res)
|
return ''.join(res)
|
||||||
```
|
```
|
||||||
|
|
||||||
Python3 (v2):
|
### Python3 (v2):
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
@ -296,7 +297,7 @@ class Solution:
|
|||||||
return s
|
return s
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func reverseStr(s string, k int) string {
|
func reverseStr(s string, k int) string {
|
||||||
@ -325,7 +326,7 @@ func reverse(b []byte) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
javaScript:
|
### JavaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
@ -346,7 +347,7 @@ var reverseStr = function(s, k) {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function reverseStr(s: string, k: number): string {
|
function reverseStr(s: string, k: number): string {
|
||||||
@ -368,7 +369,7 @@ function reverseStr(s: string, k: number): string {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Swift:
|
### Swift:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func reverseStr(_ s: String, _ k: Int) -> String {
|
func reverseStr(_ s: String, _ k: Int) -> String {
|
||||||
@ -388,7 +389,8 @@ func reverseStr(_ s: String, _ k: Int) -> String {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
C#:
|
### C#:
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class Solution
|
public class Solution
|
||||||
{
|
{
|
||||||
@ -403,7 +405,7 @@ public class Solution
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Scala:
|
### Scala:
|
||||||
|
|
||||||
版本一: (正常解法)
|
版本一: (正常解法)
|
||||||
```scala
|
```scala
|
||||||
@ -469,7 +471,7 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust:
|
### Rust:
|
||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
@ -503,4 +505,3 @@ impl Solution {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user