컨트롤 제트

han.user();·2023년 4월 5일
0

프로그래머스

목록 보기
39/87
post-thumbnail

class Solution {
    public int solution(String s) {
        String temp = "";  // 숫자를 저장하기 위한 임시 변수
        int answer = 0;  // 결과값을 저장하기 위한 변수
        char noSpaceChar;  // 공백이 아닌 문자를 저장하기 위한 변수

        for (int i = 0; i < s.length(); i++) {  // 문자열을 순회합니다.
            char c = s.charAt(i);  // 현재 위치의 문자를 가져옵니다.
            if (c != ' ') {  // 공백이 아닌 경우에만 문자를 저장함
                noSpaceChar = c;
            }
            if (Character.isDigit(c) || c == '-') {  // 문자가 숫자인 경우
                temp += c;  // temp 변수에 숫자를 추가합니다.
            } else {  // 문자가 숫자가 아닌 경우
                if (!temp.equals("")) {  // temp 변수에 숫자가 저장되어 있는 경우
                    answer += Integer.parseInt(temp);  // temp 변수에 저장된 숫자를 결과값에 더합니다.
                    if (s.charAt(i + 1) == 'Z') {  // 현재 문자의 다음 문자가 'Z'인 경우
                        answer -= Integer.parseInt(temp);  // temp 변수에 저장된 숫자를 결과값에서 뺍니다.
                    }
                    temp = "";  // temp 변수를 초기화합니다.
                }
            }
        }
        if (!temp.equals("")) {  // temp 변수에 숫자가 저장되어 있는 경우
            answer += Integer.parseInt(temp);  // temp 변수에 저장된 숫자를 결과값에 더합니다.
        }
        return answer;  // 결과값을 반환합니다.
    }
}
profile
I'm still hungry.

0개의 댓글