-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathReverseString.java
More file actions
49 lines (45 loc) · 1.6 KB
/
Copy pathReverseString.java
File metadata and controls
49 lines (45 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* <p>编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 <code>s</code> 的形式给出。</p>
*
* <p>不要给另外的数组分配额外的空间,你必须<strong><a href="https://baike.baidu.com/item/原地算法" target="_blank">原地</a>修改输入数组</strong>、使用 O(1) 的额外空间解决这一问题。</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
*
* <pre>
* <strong>输入:</strong>s = ["h","e","l","l","o"]
* <strong>输出:</strong>["o","l","l","e","h"]
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>s = ["H","a","n","n","a","h"]
* <strong>输出:</strong>["h","a","n","n","a","H"]</pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>1 <= s.length <= 10<sup>5</sup></code></li>
* <li><code>s[i]</code> 都是 <a href="https://baike.baidu.com/item/ASCII" target="_blank">ASCII</a> 码表中的可打印字符</li>
* </ul>
* <div><div>Related Topics</div><div><li>递归</li><li>双指针</li><li>字符串</li></div></div><br><div><li>👍 518</li><li>👎 0</li></div>
*/
package leetcode9;
public class ReverseString {
public static void main(String[] args) {
Solution solution = new ReverseString().new Solution();
}
class Solution {
public void reverseString(char[] s) {
for (int left = 0, right = s.length - 1; left < right; left++, right--) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
}
}
}
}