Develop

[프로그래머스] 코드 처리하기 본문

개인 공부/프로그래머스

[프로그래머스] 코드 처리하기

개발 기록 2024. 1. 18. 08:34

문제

 

 

내풀이

 

<주석 없는 버전>

class Solution {
    public String solution(String code) {
        int mode = 0;
        String ret = "";
        for(int x=0; x < code.length(); x++){
            if(mode == 0){ 
                if(code.substring(x,x+1).equals("1")){
                    mode = 1;
                }else{ 
                    ret += (x%2 == 0)? code.substring(x,x+1) : "";
                }
            }else{
                if(code.substring(x,x+1).equals("1")){ 
                    mode = 0;
                }else{
                    ret += (x%2 == 1)? code.substring(x,x+1) : "";
                }
            }
        } // for문 끝

        if(ret.length() == 0){
            ret = "EMPTY";
        }

        return ret;
    }
}

 

 

<주석 버전>

class Solution {
    public String solution(String code) {
        int mode = 0;
        String ret = "";

        for(int x=0; x < code.length(); x++){

            if(mode == 0){ // mode가 0이면
                //System.out.println("현재모드0");
                if(code.substring(x,x+1).equals("1")){ // 1이면 모드 변경 (0=>1)
                    mode = 1;
                   // System.out.println("0=>1 모드변경");
                }else{ // 1이 아니므로 idx가 짝수일 때만 ret에 값 추가
                    //System.out.println("짝수만 추가  : " + x);
                    ret += (x%2 == 0)? code.substring(x,x+1) : "";
                }

            }else{ // mode가 1이면
                //System.out.println("현재모드1");
                if(code.substring(x,x+1).equals("1")){ // 1이면 모드 변경 (1=>0)
                    mode = 0;
                    //System.out.println("1=>0 모드변경");
                }else{ // 1이 아니므로 idx가 홀수일 때만 ret에 값 추가
                    //System.out.println("홀수만 추가  : " + x);
                    ret += (x%2 == 1)? code.substring(x,x+1) : "";
                }

            }

            //System.out.println("code["+x+"] = " + code.substring(x,x+1) );

        } // for문 끝

        //System.out.println(ret);
        if(ret.length() == 0){
            ret = "EMPTY";
        }

        return ret;
    }
}

 

느낀점

 

System.out.println()으로 확인하면서 코드를 짰다

간단한건 3항연산자로 했으며

2중 if문을 쓰지 않고 풀고싶었지만 실패했다..

 

마지막 ret.length() 는 공백비교보단 길이 확인이 쉽고 확실해서 길이를 비교했다

 

 

다른 풀이

class Solution {
    public String solution(String code) {
        StringBuilder answer = new StringBuilder();
        int mode = 0;
        for (int i = 0; i < code.length(); i++) {
            char current = code.charAt(i);
            if (current == '1') {
                mode = mode == 0 ? 1 : 0;
                continue;
            }

            if (i % 2 == mode) {
                answer.append(current);
            }
        }
        return answer.length() == 0 ? "EMPTY" : answer.toString();
    }
}

 

느낀점

 

 - substring 대신 charAt사용

: 배열처럼 문자열도 하나씩 가져오는 방법이 궁금했는데 charAt을 사용하면 되겠다!!

 

- i%2 == mode 를 사용

: 문제를 꿰뚫어본 느낌이다 모드가 0일때나 1일때나 만족하는 조건을 생각한다는건 이런건가보다

 

- += 대신 append사용

: 이 함수도 유용하게 쓸 것 같다