File tree Expand file tree Collapse file tree
프로그래머스/0/120902. 문자열 계산하기 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # [ level 0] 문자열 계산하기 - 120902
2+
3+ [ 문제 링크] ( https://school.programmers.co.kr/learn/courses/30/lessons/120902 )
4+
5+ ### 성능 요약
6+
7+ 메모리: 62.3 MB, 시간: 0.21 ms
8+
9+ ### 구분
10+
11+ 코딩테스트 연습 > 코딩테스트 입문
12+
13+ ### 채점결과
14+
15+ 정확성: 100.0<br />합계: 100.0 / 100.0
16+
17+ ### 제출 일자
18+
19+ 2026년 05월 20일 09:07:39
20+
21+ ### 문제 설명
22+
23+ <p ><code >my_string</code >은 "3 + 5"처럼 문자열로 된 수식입니다. 문자열 <code >my_string</code >이 매개변수로 주어질 때, 수식을 계산한 값을 return 하는 solution 함수를 완성해주세요.</p >
24+
25+ <hr >
26+
27+ <h5 >제한사항</h5 >
28+
29+ <ul >
30+ <li >연산자는 +, -만 존재합니다.</li >
31+ <li >문자열의 시작과 끝에는 공백이 없습니다.</li >
32+ <li >0으로 시작하는 숫자는 주어지지 않습니다.</li >
33+ <li >잘못된 수식은 주어지지 않습니다.</li >
34+ <li >5 ≤ <code >my_string</code >의 길이 ≤ 100</li >
35+ <li ><code >my_string</code >을  ; 계산한 결과값은 1 이상 100,000 이하입니다.
36+
37+ <ul >
38+ <li ><code >my_string</code >의 중간 계산 값은 -100,000 이상 100,000 이하입니다.</li >
39+ <li >계산에 사용하는 숫자는 1 이상 20,000 이하인 자연수입니다.</li >
40+ <li ><code >my_string</code >에는 연산자가 적어도 하나 포함되어 있습니다.</li >
41+ </ul ></li >
42+ <li >return type 은 정수형입니다.</li >
43+ <li ><code >my_string</code >의 숫자와 연산자는 공백 하나로 구분되어 있습니다.</li >
44+ </ul >
45+
46+ <hr >
47+
48+ <h5 >입출력 예</h5 >
49+ <table class =" table " >
50+ <thead><tr>
51+ <th >my_string</th >
52+ <th >result</th >
53+ </tr >
54+ </thead >
55+ <tbody><tr>
56+ <td >"3 + 4"</td >
57+ <td >7</td >
58+ </tr >
59+ </tbody >
60+ </table>
61+ <hr >
62+
63+ <h5 >입출력 예 설명</h5 >
64+
65+ <p >입출력 예 #1</p >
66+
67+ <ul >
68+ <li >3 + 4 = 7을 return 합니다.</li >
69+ </ul >
70+
71+
72+ > 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ class Solution {
4+ public int solution (String my_string ) {
5+ int answer = 0 ;
6+
7+ String [] strArr = my_string .split (" " );
8+ Queue <Integer > q = new ArrayDeque <>();
9+ int flag = 1 ;
10+
11+ for (String s : strArr ){
12+ if (s .equals ("+" )){
13+ continue ;
14+ }
15+ else if (s .equals ("-" )){
16+ flag = -1 ;
17+ }
18+ else {
19+ int num = Integer .parseInt (s ) * flag ;
20+ flag = 1 ;
21+ q .add (num );
22+ }
23+ }
24+
25+ while (!q .isEmpty ()){
26+ int cur = q .poll ();
27+ answer += cur ;
28+ }
29+
30+
31+
32+
33+ return answer ;
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments