Skip to content

Commit 35e2734

Browse files
committed
issue #20 344-Done💯
1 parent 343ef52 commit 35e2734

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcodeEasyLevel;
2+
3+
import java.util.Arrays;
4+
5+
public class _0344ReverseString {
6+
7+
public static void reverseString(char[] s) {
8+
//새로운 배열을 만들지않고 char array 역순으로 출력하는 문제
9+
10+
//sol1. Runtime 1 ms Memory 52.5 MB
11+
/*
12+
for (int i = 0; i < s.length / 2; i++) {
13+
char temp = s[i];
14+
s[i] = s[s.length - 1 - i]; //0 = 5, 1 = 4. 2 = 3
15+
s[s.length - 1 - i] = temp;
16+
}
17+
*/
18+
19+
//sol2 Runtime 1 ms Memory 53 MB
20+
int st = 0, en = s.length - 1;
21+
while (st < en) {
22+
char temp = s[st];
23+
s[st] = s[en];
24+
s[en] = temp;
25+
st++;
26+
en--;
27+
}
28+
29+
//print the result
30+
System.out.println(Arrays.toString(s));
31+
}
32+
33+
public static void main(String[] args) {
34+
char[] ex1 = {'h', 'e', 'l', 'l', 'o' };
35+
char[] ex2 = {'H', 'a', 'n', 'n', 'a', 'h' };
36+
37+
reverseString(ex1);
38+
reverseString(ex2);
39+
System.out.println("ex1 ans: [\"o\",\"l\",\"l\",\"e\",\"h\"]");
40+
System.out.println("ex2 ans: [\"h\",\"a\",\"n\",\"n\",\"a\",\"H\"]");
41+
}
42+
}

0 commit comments

Comments
 (0)