🔗 코드 처리하기

 

문자열 code를 특정 규칙에 따라 읽어가며 새로운 문자열 ret을 생성하는 문제이다. code를 순회하면서 현재 문자가 "1"이면 mode를 0이나 1로 전환한다. 현재 문자가 "1"이 아니고 mode가 0일 때, 인덱스가 짝수인 경우만 문자를 추가한다. 현재 문자가 "1"이 아니고 mode가 1일 때, 인덱스가 홀수인 경우만 문자를 추가한다.


✨ 전체코드 ✨

class Solution {
    public String solution(String code) {
        String ret = "";
        int mode = 0;
        for(int idx=0; idx<code.length(); idx++){
            char ch = code.charAt(idx);
            
            if(ch != '1') {
                if(idx % 2 == mode) ret += ch;
            } else mode = (mode == 0 ? 1 : 0);
        } return ret.equals("") ? "EMPTY" : ret;
    }
}

 

🌀 성능

  • 메모리 : 381 MB
  • 시간 : 367.16 ms

 

 

 


🔗 등차수열의 특정한 항만 더하기

 

✨ 전체코드 ✨

class Solution {
    public int solution(int a, int d, boolean[] included) {
        int answer = 0;
        for(int i=0; i<included.length; i++){
            if(included[i]) answer += (d*i+a);
        } return answer;
    }
}

 

🌀 성능

  • 메모리 : 77.1 MB
  • 시간 : 0.04 ms

 

 

 


🔗 주사위 게임 2

✨ 전체코드 ✨

class Solution {
    public int solution(int a, int b, int c) {
        int answer = 0;
        
        if(a==b && b==c) answer = ((a+b+c) * (a*a + b*b + c*c) * (a*a*a + b*b*b + c*c*c));
        else if (a!=b && b!=c && c!=a) answer = a+b+c;
        else answer = ((a+b+c) * (a*a + b*b + c*c));
        
        return answer;
    }
}

 

🌀 성능

  • 메모리 : 79.3 MB
  • 시간 : 0.02 ms

 

 

 


🔗 원소들의 곱과 합

✨ 전체코드 ✨

class Solution {
    public int solution(int[] num_list) {
        int mul = 1;
        int sum = 0;
        
        for(int i=0; i<num_list.length; i++){
            mul *= num_list[i];
            sum += num_list[i];
        }
        
        return mul < sum*sum ? 1 : 0;
    }
}

 

🌀 성능

  • 메모리 : 77.3 MB
  • 시간 : 0.03 ms

 

 

 


🔗 이어 붙인 수

두 번째 테스트케이스

✨ 방법 1 : 문자열 변환✨

class Solution {
    public int solution(int[] num_list) {
        String even = "";
        String odd = "";
        
        for(int num : num_list){
            if(num % 2 == 0) even += num + "";
            else odd += num + "";
        } return Integer.parseInt(even) + Integer.parseInt(odd);
    }
}

 

🌀 성능

  • 메모리 : 67.4 MB
  • 시간 : 13.54 ms

 

🌟 방법 2 : 곱셈🌟

class Solution {
    public int solution(int[] num_list) {
        int even = 0;
        int odd = 0;

        for(int num : num_list) {
            if(num % 2 == 0) {
                even *= 10;
                even += num;
            } else {
                odd *= 10;
                odd += num;
            }
        } return even + odd;
    }
}

 

🌀 성능

  • 메모리 : 71.1 MB
  • 시간 : 0.03 ms