문제 설명

솔루션
class Solution {
public int solution(String my_string) {
int answer = 0;
String[] str_arr = my_string.split("[a-zA-Z]+");//알파벳 쪼개서 String 배열 만들기
for(int i = 0; i < str_arr.length; i++){
if(str_arr[i].matches("[0-9]+")){//숫자인지
answer += Integer.parseInt(str_arr[i]);//Integer.parseInt해서 answer 더하기
}
}
return answer;
}
}
다른 방법
class Solution {
public int solution(String my_string) {
int answer = 0;
String[] str = my_string.replaceAll("[a-zA-Z]", " ").split(" ");//알파벳 죄다 없애기
for(String s : str){
if(!s.equals("")) answer += Integer.valueOf(s);//빈칸이 아닐경우 더하기
}
return answer;
}
}'java' 카테고리의 다른 글
| [프로그래머스 - 자바] 한번만 등장한 문자 (0) | 2023.09.01 |
|---|---|
| [프로그래머스 - 자바] 진료 순서 정하기 (0) | 2023.09.01 |
| [프로그래머스 - 자바] 가까운 수 (0) | 2023.09.01 |
| [프로그래머스 - 자바] k의 개수 (0) | 2023.08.29 |
| [프로그래머스 - 자바] A로 B만들기 (0) | 2023.08.29 |