File tree Expand file tree Collapse file tree 1 file changed +43
-3
lines changed
src/test/java/algorithm/basic Expand file tree Collapse file tree 1 file changed +43
-3
lines changed Original file line number Diff line number Diff line change 11package algorithm .basic ;
22
3- /**
4- * Created by Jbee on 2017. 6. 5..
5- */
3+ import org .junit .Test ;
4+ import utils .Utils ;
5+
6+ import java .util .Stack ;
7+
8+ import static org .hamcrest .CoreMatchers .is ;
9+ import static org .junit .Assert .assertThat ;
10+
611public class StringReverse {
12+
13+ /*
14+ TASK
15+ 주어진 문자열을 역순으로 출력한다.
16+ */
17+
18+ @ Test
19+ public void test () {
20+ assertThat (solution1 ("abc" ), is ("cba" ));
21+ assertThat (solution1 ("abcd" ), is ("dcba" ));
22+
23+ assertThat (solution2 ("abc" ), is ("cba" ));
24+ assertThat (solution2 ("abcd" ), is ("dcba" ));
25+ }
26+
27+ // 1. 새로운 배열에 담기
28+ public String solution1 (String str ) {
29+ char [] charArr = str .toCharArray ();
30+ char [] resultArr = new char [charArr .length ];
31+
32+ for (int i = 0 ; i < charArr .length ; i ++) {
33+ resultArr [charArr .length - i - 1 ] = charArr [i ];
34+ }
35+ return new String (resultArr );
36+ }
37+
38+ // 2. swap하기
39+ public String solution2 (String str ) {
40+ char [] charArr = str .toCharArray ();
41+
42+ for (int i = 0 ; i < charArr .length / 2 ; i ++) {
43+ Utils .swapValue (charArr , i , charArr .length - 1 - i );
44+ }
45+ return new String (charArr );
46+ }
747}
You can’t perform that action at this time.
0 commit comments