-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_0344ReverseString.java
43 lines (42 loc) · 1.12 KB
/
_0344ReverseString.java
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
package com.heatwave.leetcode.problems;
/**
* Write a function that reverses a string. The input string is given as an array of characters s.
* <p>
* You must do this by modifying the input array in-place with O(1) extra memory.
* <p>
*
* <p>
* Example 1:
* <p>
* Input: s = ["h","e","l","l","o"]
* Output: ["o","l","l","e","h"]
* Example 2:
* <p>
* Input: s = ["H","a","n","n","a","h"]
* Output: ["h","a","n","n","a","H"]
*
* <p>
* Constraints:
* <p>
* 1 <= s.length <= 105
* s[i] is a printable ascii character.
* <p>
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/reverse-string
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class _0344ReverseString {
static class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length - 1;
char temp;
while (left < right) {
temp = s[right];
s[right] = s[left];
s[left] = temp;
left++;
right--;
}
}
}
}