์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋‹จ์–ด ๋’ค์ง‘๊ธฐ

2023. 10. 4. 14:47ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

https://hyejin.tistory.com/1199

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋ฌธ์žฅ ์† ๋‹จ์–ด

https://hyejin.tistory.com/1198 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜ https://hyejin.tistory.com/1197 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ : ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋ฌธ์ž ์ฐพ๊ธฐ https://www.i

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

4. ๋‹จ์–ด ๋’ค์ง‘๊ธฐ 

 

์„ค๋ช…

N๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ๊ฐ ๋‹จ์–ด๋ฅผ ๋’ค์ง‘์–ด ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ

์ฒซ ์ค„์— ์ž์—ฐ์ˆ˜ N(3<=N<=20)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๊ฐ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋‹จ์–ด๋Š” ์˜์–ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ

N๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์ž…๋ ฅ๋œ ์ˆœ์„œ๋Œ€๋กœ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋’ค์ง‘์–ด์„œ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

3
good
Time
Big

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

doog
emiT
giB

 

 

๋ฌธ์ œ ํ’€์ด 1

public String[] solution(String[] input) {
    String[] answer = new String[input.length];
    Arrays.fill(answer, "");
    for (int i = 0; i < input.length; i++) {
        String[] s = input[i].split("");
        for (int j = s.length - 1; j >= 0; j--) {
            answer[i] += s[j];
        }
    }
    return answer;
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ๋‚˜๋Š” ์šฐ์„  ์ด๋ฒˆ์—๋„.. split์ด ๋– ์˜ฌ๋ผ์„œ split ์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. 

์šฐ์„  ์ฃผ์–ด์ง„ ๋ฌธ์ž๋ฅผ ๋นˆ๋ฌธ์ž์—ด ""๋กœ ์ชผ๊ฐ  ๋‹ค์Œ ๊ทธ ๋ฌธ์ž ๋ฐฐ์—ด์„ ๊ฐ€์ง€๊ณ  for๋ฌธ์„ ๋ฐ˜๋Œ€๋กœ ๋Œ๋ฉด์„œ answer์— ๊ทธ ๋ฌธ์ž๋ฅผ ๋”ํ•ด์คฌ๋‹ค. 

(์•„๋งˆ..? ์ด๋ ‡๊ฒŒ ํ’€๋ฉด ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„  ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ์—์„œ ๋–จ์–ด์กŒ์„ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ .. ์ผ๋‹จ ์—ฌ๊ธฐ์„œ๋Š” ์ •๋‹ต์ด์—ˆ๋‹ค.) 

์ด๋•Œ answer ๋ฌธ์ž์—ด ๋ฐฐ์—ด์—์„œ ""๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ๊ฐ’์ด ๋ชจ๋‘ null ๋กœ ์ดˆ๊ธฐํ™”๋˜์–ด ์žˆ์–ด์„œ ๊ทธ๋Œ€๋กœ null ์ด ๋ถ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ Arrays.fill ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ answer ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ ๋ชจ๋‘ "" ๋นˆ ๋ฌธ์ž์—ด๋กœ ์ดˆ๊ธฐํ™”ํ•ด์คฌ๋‹ค. 

 

 

๋ฌธ์ œ ํ’€์ด 2

public ArrayList<String> solution2(int n, String[] str) {
    ArrayList<String> answer = new ArrayList<>();
    for (String s : str) {
        String tmp = new StringBuilder(s).reverse().toString();
        answer.add(tmp);
    }

    return answer;
}

๐Ÿ‘พ : StringBuilder ๋ฅผ ์™œ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ์„๊นŒ..!!! ๋จผ์ € str์„ StringBuilder์— ๋„ฃ์–ด์ฃผ๊ณ , reverse๋ฅผ ํ•ด์ฃผ๋ฉด ์•Œ์•„์„œ ๊ทธ ๋ฌธ์ž์—ด์ด ๋ฐ˜๋Œ€๋กœ ๋’ค์ง‘ํžŒ๋‹ค.. 

 

 

๋ฌธ์ œ ํ’€์ด 3

public ArrayList<String> solution3(int n, String[] str) {
    ArrayList<String> answer = new ArrayList<>();
    for (String s : str) {
        char[] chars = s.toCharArray();
        int lt = 0, rt = s.length() - 1;
        while (lt < rt) {
            char tmp = chars[lt];
            chars[lt] = chars[rt];
            chars[rt] = tmp;
            lt ++;
            rt--;
        }
        String tmp = String.valueOf(chars);
        answer.add(tmp);
    }
    return answer;
}

๐Ÿ‘พ : StringBuilder์˜ reverse ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„ํŽธํ•˜๊ฒŒ ๋‹จ์–ด๋ฅผ ๋’ค์ง‘์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ง์ ‘ ๋’ค์ง‘์„ ์ˆ˜๋„ ์žˆ๋‹ค. 

์šฐ์„  ์ฃผ์–ด์ง„ ๋ฌธ์ž๋ฐฐ์—ด str์—์„œ for๋ฌธ์„ ๋Œ๋ฉด์„œ ๊ฐ ๋ฌธ์ž๋ฅผ toCharArray() ๋ฅผ ํ†ตํ•ด์„œ ๋ฌธ์ž ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ , 

lt, rt ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์„œ lt๋Š” ๋ฌธ์ž ๋ฐฐ์—ด์˜ ์•ž๋ถ€๋ถ„๋ถ€ํ„ฐ, rt๋Š” ๋ฌธ์ž ๋ฐฐ์—ด์˜ ๋’ท๋ถ€๋ถ„๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ๋‚˜๊ฐ€๋„๋ก ํ–ˆ๋‹ค. 

lt๊ฐ€ rt ์œ„์น˜๋ณด๋‹ค ์ž‘์„๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ( lt ์œ„์น˜๊ฐ€ rt ์œ„์น˜๋ณด๋‹ค ์ปค์ง„๋‹ค๋Š” ๊ฑด, ๋ฌธ์ž์—ด์„ ์„œ๋กœ ๋‹ค ๋’ค์ง‘์—ˆ๋‹ค๋Š” ๊ฒƒ!) ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ์„œ๋กœ ๋ณ€๊ฒฝํ•ด์คฌ๋‹ค. ๊ทธ ๋‹ค์Œ lt ๋Š” +1 ํ•˜๊ณ , rt ๋Š” -1 ํ•ด์„œ ๊ฐ๊ฐ ๊ทธ ๋‹ค์Œ ๋ฌธ์ž๋ฅผ ์„œ๋กœ ๊ตํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

๊ทธ ๋‹ค์Œ ๊ฒฐ๊ณผ๋Š” ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— String.value ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๊ณ , arraylist์— add ํ•ด์คฌ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90