📌Back-End/프로그래머스

Java 프로그래머스 코딩테스트 입문 Day 12 문자열, 정렬, 사칙연산, 수학

구 일 2024. 7. 4. 18:44
728x90
반응형

 

모음 제거

프로그래머스 모음 제거 자바

 

 

/**
 * 코딩테스트 입문 Day 12 문자열, 정렬, 사칙연산, 수학
 * 모음 제거
 */
public class Main1 {
    public static void main(String[] args) {
        System.out.println(solution("bus"));
        System.out.println(solution("nice to meet you"));
    }

    public static String solution(String my_string) {
        return my_string.replaceAll("[aeiou]", "");
    }
}

 

 

문자열 정렬하기 (1)

프로그래머스 문자열 정렬하기 (1) 자바

 

 

import java.util.Comparator;
import java.util.LinkedList;

/**
 * 코딩테스트 입문 Day 12 문자열, 정렬, 사칙연산, 수학
 * 문자열 정렬하기 (1)
 */
public class Main2 {
    public static void main(String[] args) {
        int[] result = solution("h112392");
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution("p2o4i8gj2");
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution("abcde0");
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    public static int[] solution(String my_string) {
        LinkedList<Integer> list = new LinkedList<>();

        char[] charArr = my_string.toCharArray();

        for (char c : charArr) {
            if (Character.isDigit(c)) {
                list.add(Integer.parseInt(String.valueOf(c)));
            }
        }

        list.sort(Comparator.naturalOrder());

        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

 

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

프로그래머스 숨어있는 숫자의 덧셈 (1) 자바

 

 

/**
 * 코딩테스트 입문 Day 12 문자열, 정렬, 사칙연산, 수학
 * 숨어있는 숫자의 덧셈 (1)
 */
public class Main3 {
    public static void main(String[] args) {
        System.out.println(solution("aAb1B2cC34oOp"));
        System.out.println(solution("1a2b3c4d123"));
    }

    public static int solution(String my_string) {
        int answer = 0;
        String[] strArr = my_string.replaceAll("[a-zA-Z]", "")
                .split("");

        for (String s : strArr) {
            answer += Integer.parseInt(s);
        }

        return answer;
    }
}

 

 

소인수분해

프로그래머스 소인수분해 자바

 

 

 

import java.util.LinkedList;

/**
 * 코딩테스트 입문 Day 12 문자열, 정렬, 사칙연산, 수학
 * 소인수분해
 */
public class Main4 {
    public static void main(String[] args) {
        int[] result = solution(12);
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution(17);
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();

        result = solution(420);
        for (int num : result) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    public static int[] solution(int n) {
        LinkedList<Integer> list = new LinkedList<>();

        for (int i = 2; i <= n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                list.add(i);
            }
        }

        return list.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

 

728x90
반응형