[Programmers] 코딩테스트 입문 120864. 숨어있는 숫자의 덧셈(2)

이지현·2023년 3월 6일
0

Algorithm

목록 보기
62/81
post-thumbnail

✔️ Problem URL

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


✔️ Problem

문자열 my_string이 매개변수로 주어집니다. my_string은 소문자, 대문자, 자연수로만 구성되어있습니다. my_string안의 자연수들의 합을 return하도록 solution 함수를 완성해주세요.


✔️ Code

class Solution {
    public int solution(String my_string) {
        String[] arr = my_string.replaceAll("[^0-9]", " ").split(" ");
        int answer = 0;
        
        for(int i = 0; i < arr.length; i++) {
            if(arr[i].equals("")) {
                continue;
            }
            else {
                answer += Integer.parseInt(arr[i].trim());
            }
        }
        return answer;
    }
}
profile
2023.09 ~ 티스토리 이전 / 2024.04 ~ 깃허브 블로그 이전

0개의 댓글