본문 바로가기

study_IT/알고리즘 노트

[programmers] 코딩테스트 입문 Day 12

728x90
반응형

1. 모음 제거

class Solution {
    public String solution(String my_string) {
        String answer = "";
        
        for(int i=0; i<my_string.length(); i++){
            if( my_string.charAt(i) == 'a' || my_string.charAt(i) == 'e' ||
               my_string.charAt(i) == 'i' || my_string.charAt(i) == 'o' ||
               my_string.charAt(i) == 'u'){
            
            } else{
                answer += my_string.charAt(i);
            }
        }        
        return answer;
    }
}

2. 문자열 정렬하기 (1)

import java.util.Arrays;

class Solution {
    public int[] solution(String my_string) {
        
      
        String num = "";
        
        char[] result = new char[my_string.length()];
        
        
        for(int i=0; i<my_string.length(); i++){
           result[i] = my_string.charAt(i);
        }
        
        for(int j=0; j<result.length; j++){
            if(Character.isDigit(result[j])) {
                num += result[j];
            }
        }
        
        char[] numChar = new char[num.length()];
        int[] answer = new int[numChar.length];
        
       for(int x=0; x<num.length(); x++){
           numChar[x] = num.charAt(x);
        }
        
        Arrays.sort(numChar);
        
        int a = (int)numChar[0];
        
       for(int y=0; y<numChar.length; y++){
           answer[y] = Character.getNumericValue(numChar[y]);
       }
        
        return answer;
    }
}

3. 숨어있는 숫자의 덧셈 (1)

class Solution {
    public int solution(String my_string) {
        int answer = 0;
        char[] toChar = new char[my_string.length()];
        String num = "";
        
        for(int i=0; i<my_string.length(); i++){
            toChar[i] = my_string.charAt(i);
        }
        for(int j=0; j<toChar.length; j++){
            if(Character.isDigit(toChar[j])){
                num+=toChar[j];
            }
        }
        for(int x=0; x<num.length(); x++){
           answer+= Character.getNumericValue(num.charAt(x));
        }
        
        return answer;
    }
}

4. 소인수분해

class Solution {
    public int[] solution(int n) {
  
        int count = 0;
        int numArraySize = 0;
        int a = 0;
       
        
        for(int i=2; i<=n; i++){
           if (n % i == 0){
               for(int j = 1; j<= i; j++){
                   if( i % j == 0){
                       count++;
                   }
                   
               }
               if(count == 2){
                 numArraySize++;
               }
               count = 0;
           }
        }

        int answer[] = new int[numArraySize];
        
        for(int x=2; x<=n; x++){
           if (n % x == 0){
               for(int y = 1; y<= x; y++){
                   if( x % y == 0){
                       count++;
                   }
                   
               }
               if(count == 2){
                answer[a] = x;
                   a++;
               }
               count = 0;
           }
        }
        
        
        return answer;
    }
}
728x90
반응형