- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathStringReverse.java
More file actions
Latest commit
45 lines (35 loc) · 1.12 KB
/
Copy pathStringReverse.java
File metadata and controls
45 lines (35 loc) · 1.12 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
packagealgorithm.basic;
importorg.junit.Test;
importutils.Utils;
importstaticorg.hamcrest.CoreMatchers.is;
importstaticorg.junit.Assert.assertThat;
publicclassStringReverse {
/*
TASK
주어진 문자열을 역순으로 출력한다.
*/
@Test
publicvoidtest() {
assertThat(solution1("abc"), is("cba"));
assertThat(solution1("abcd"), is("dcba"));
assertThat(solution2("abc"), is("cba"));
assertThat(solution2("abcd"), is("dcba"));
}
// 1. 새로운 배열에 담기
publicStringsolution1(Stringstr) {
char[] charArr = str.toCharArray();
char[] resultArr = newchar[charArr.length];
for (inti = 0; i < charArr.length; i++) {
resultArr[charArr.length - i - 1] = charArr[i];
}
returnnewString(resultArr);
}
// 2. swap하기
publicStringsolution2(Stringstr) {
char[] charArr = str.toCharArray();
for (inti = 0; i < charArr.length / 2; i++) {
Utils.swapValue(charArr, i, charArr.length - 1 - i);
}
returnnewString(charArr);
}
}