1. [Java] 암호 해독 - cipher.charAt(i - 1)


https://school.programmers.co.kr/learn/courses/30/lessons/120892

오답

class Solution {
    public String solution(String cipher, int code) {
        StringBuilder sb = new StringBuilder();
        for (int i = code; i <= cipher.length(); code++;) {
            sb.append(cipher[i-1]); // 인덱스는 0부터 시작
        }
        return sb.toString();
    }
}


✅ for문의 증가식 오류
for (int i = code; i <= cipher.length(); code++;)


✅ 문자열 인덱스 접근 오류


정답 코드

class Solution {
    public String solution(String cipher, int code) {
        StringBuilder sb = new StringBuilder();
        for (int i = code; i <= cipher.length(); i += code) {
            sb.append(cipher.charAt(i - 1)); // 인덱스는 0부터 시작
        }
        return sb.toString();
    }
}




Revision #9
Created 16 May 2025 00:32:51 by Dain
Updated 23 May 2025 04:25:52 by Dain