문제
문자열 my_string이 매개변수로 주어질 때,
my_string 안에 있는 숫자만 골라 오름차순 정렬한 리스트를 return 하도록 solution 함수를 작성해보세요.
내 풀이
public int[] solution(String my_string) {
String intStr = my_string.replaceAll("[^0-9]", "");
int[] answer = new int[intStr.length()];
for (int i = 0; i < intStr.length(); i++){
answer[i] = intStr.charAt(i) - '0';
}
Arrays.sort(answer);
return answer;
}String intStr = my_string.replaceAll("[^0-9]", ""); 정규표현식을 사용해서 풀었다.
[]한개의 문자 ^ 아닌거 0-9 숫자가 아닌거를 ""으로 만든다.
'문제 풀이 > 프로그래머스' 카테고리의 다른 글
| 프로그래머스 lv1 x만큼간격이있는n개의숫자 (0) | 2023.02.23 |
|---|---|
| 프로그래머스 lv0 문자열 정렬하기2 (0) | 2023.02.22 |
| 프로그래머스 lv0 제곱수판별하기 (0) | 2023.02.22 |
| 프로그래머스 lv0 모음제거 (0) | 2023.02.22 |
| 프로그래머스 lv0 배열자르기 (0) | 2023.02.21 |