Hi, There!
안녕하세요, 바오밥입니다.
목차
- 문제
- 풀이
문제
문제 내용
https://school.programmers.co.kr/learn/courses/30/lessons/120850
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
나의 풀이
- ASCII 코드 값을 활용해 숫자 값만 List 자료 구조의 넣은 후 정렬하고, 다시 int[]형 배열로 반환
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
class Solution {
public int[] solution(String my_string) {
List<Integer> intList = new ArrayList<Integer>();
for(char c : my_string.toCharArray())
if((int)c >= 48 && (int)c <= 57)
intList.add(c-'0');
Collections.sort(intList);
return intList.stream().mapToInt(x->x).toArray();
}
}
다른 사람의 풀이
- replaceAll() 메서드에 모든 영소문자에 대한 정규표현식을 전달해 공백으로 치환
- 반복문 통해서 int[]에 값을 옮기고 정렬 후 반환
import java.util.*;
class Solution {
public int[] solution(String my_string) {
my_string = my_string.replaceAll("[a-z]","");
int[] answer = new int[my_string.length()];
for(int i =0; i<my_string.length(); i++){
answer[i] = my_string.charAt(i) - '0';
}
Arrays.sort(answer);
return answer;
}
}
'Dev > PS' 카테고리의 다른 글
[프로그래머스-코딩테스트 입문] 소인수분해 (0) | 2023.08.14 |
---|---|
[프로그래머스-코딩테스트 입문] 숨어있는 숫자의 덧셈 (1) (0) | 2023.08.13 |
[프로그래머스-코딩테스트 입문] 모음 제거 (0) | 2023.08.13 |
[프로그래머스-코딩테스트 입문] 팩토리얼 (0) | 2023.08.12 |
[프로그래머스-코딩테스트 입문] 최댓값 만들기 (1) (0) | 2023.08.12 |