|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 简单 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3794.Reverse%20String%20Prefix/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3794. 反转字符串前缀](https://leetcode.cn/problems/reverse-string-prefix) |
| 10 | + |
| 11 | +[English Version](/solution/3700-3799/3794.Reverse%20String%20Prefix/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>给你一个字符串 <code>s</code> 和一个整数 <code>k</code>。</p> |
| 18 | + |
| 19 | +<p>反转 <code>s</code> 的前 <code>k</code> 个字符,并返回结果字符串。</p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | + |
| 23 | +<p><strong class="example">示例 1:</strong></p> |
| 24 | + |
| 25 | +<div class="example-block"> |
| 26 | +<p><strong>输入:</strong> <span class="example-io">s = "abcd", k = 2</span></p> |
| 27 | + |
| 28 | +<p><strong>输出:</strong> <span class="example-io">"bacd"</span></p> |
| 29 | + |
| 30 | +<p><strong>解释:</strong></p> |
| 31 | + |
| 32 | +<p>前 <code>k = 2</code> 个字符 <code>"ab"</code> 反转为 <code>"ba"</code>。最终得到的结果字符串为 <code>"bacd"</code>。</p> |
| 33 | +</div> |
| 34 | + |
| 35 | +<p><strong class="example">示例 2:</strong></p> |
| 36 | + |
| 37 | +<div class="example-block"> |
| 38 | +<p><strong>输入:</strong> <span class="example-io">s = "xyz", k = 3</span></p> |
| 39 | + |
| 40 | +<p><strong>输出:</strong> <span class="example-io">"zyx"</span></p> |
| 41 | + |
| 42 | +<p><strong>解释:</strong></p> |
| 43 | + |
| 44 | +<p>前 <code>k = 3</code> 个字符 <code>"xyz"</code> 反转为 <code>"zyx"</code>。最终得到的结果字符串为 <code>"zyx"</code>。</p> |
| 45 | +</div> |
| 46 | + |
| 47 | +<p><strong class="example">示例 3:</strong></p> |
| 48 | + |
| 49 | +<div class="example-block"> |
| 50 | +<p><strong>输入:</strong> <span class="example-io">s = "hey", k = 1</span></p> |
| 51 | + |
| 52 | +<p><strong>输出:</strong> <span class="example-io">"hey"</span></p> |
| 53 | + |
| 54 | +<p><strong>解释:</strong></p> |
| 55 | + |
| 56 | +<p>前 <code>k = 1</code> 个字符 <code>"h"</code> 在反转后保持不变。最终得到的结果字符串为 <code>"hey"</code>。</p> |
| 57 | +</div> |
| 58 | + |
| 59 | +<p> </p> |
| 60 | + |
| 61 | +<p><strong>提示:</strong></p> |
| 62 | + |
| 63 | +<ul> |
| 64 | + <li><code>1 <= s.length <= 100</code></li> |
| 65 | + <li><code>s</code> 仅由小写英文字母组成。</li> |
| 66 | + <li><code>1 <= k <= s.length</code></li> |
| 67 | +</ul> |
| 68 | + |
| 69 | +<!-- description:end --> |
| 70 | + |
| 71 | +## 解法 |
| 72 | + |
| 73 | +<!-- solution:start --> |
| 74 | + |
| 75 | +### 方法一:模拟 |
| 76 | + |
| 77 | +我们按照题目描述,将字符串的前 $k$ 个字符反转,然后与剩余的字符串拼接即可。 |
| 78 | + |
| 79 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是字符串的长度。 |
| 80 | + |
| 81 | +<!-- tabs:start --> |
| 82 | + |
| 83 | +#### Python3 |
| 84 | + |
| 85 | +```python |
| 86 | +class Solution: |
| 87 | + def reversePrefix(self, s: str, k: int) -> str: |
| 88 | + return s[:k][::-1] + s[k:] |
| 89 | +``` |
| 90 | + |
| 91 | +#### Java |
| 92 | + |
| 93 | +```java |
| 94 | +class Solution { |
| 95 | + public String reversePrefix(String s, int k) { |
| 96 | + StringBuilder sb = new StringBuilder(s.substring(0, k)); |
| 97 | + return sb.reverse().toString() + s.substring(k); |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +#### C++ |
| 103 | + |
| 104 | +```cpp |
| 105 | +class Solution { |
| 106 | +public: |
| 107 | + string reversePrefix(string s, int k) { |
| 108 | + string t = s.substr(0, k); |
| 109 | + reverse(t.begin(), t.end()); |
| 110 | + return t + s.substr(k); |
| 111 | + } |
| 112 | +}; |
| 113 | +``` |
| 114 | +
|
| 115 | +#### Go |
| 116 | +
|
| 117 | +```go |
| 118 | +func reversePrefix(s string, k int) string { |
| 119 | + t := []byte(s[:k]) |
| 120 | + slices.Reverse(t) |
| 121 | + return string(t) + s[k:] |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +#### TypeScript |
| 126 | + |
| 127 | +```ts |
| 128 | +function reversePrefix(s: string, k: number): string { |
| 129 | + return s.slice(0, k).split('').reverse().join('') + s.slice(k); |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +<!-- tabs:end --> |
| 134 | + |
| 135 | +<!-- solution:end --> |
| 136 | + |
| 137 | +<!-- problem:end --> |
0 commit comments