본문 바로가기

프로그래머스/lv0

문자열 정렬하기 (1)

/* 문자열 정렬하기 (1)
 * 문자열 my_string이 매개변수로 주어질 때,
 * my_string 안에 있는 숫자만 골라 오름차순 정렬한 리스트를 return
 *
 * my_string    result
 * "hi12392"    [1, 2, 2, 3, 9]
 * "p2o4i8gj2"  [2, 2, 4, 8]
 * "abcde0"     [0]
 */

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.*;
public class programmer_0_71 {
    static String a1 = "hi12392";
    static String a2 = "p2o4i8gj2";
    static String a3 = "abcde0";
 
    public int[] solution(String my_string) {
        String[] ar = my_string.replaceAll("[a-zA-Z]""").split("");
        int[] answer = new int[ar.length];
        
        for(int i = 0 ; i < ar.length; i++){
            answer[i] = Integer.parseInt(ar[i]);
        }
        Arrays.sort(answer);
 
        return answer;
    }
    public static void main(String args[]){
        programmer_0_71 t = new programmer_0_71();
 
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        // System.out.println("result2 = " + t.solution(a2));
        // System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

개미 군단  (0) 2022.12.05
가위 바위 보  (0) 2022.12.02
외계행성의 나이  (0) 2022.12.02
진료순서 정하기  (0) 2022.12.02
모스부호 (1)  (2) 2022.12.02