
String/char Number
"7" : 문자열 (String)'7' : 문자 (Char)| 변환 | 함수 / 연산식 | 비고 |
|---|---|---|
| String Int | std::stoi(str) | 문자열을 정수(int)로 변환 (String To Integer) |
| String Long Long | std::stoll(str) | 문자열을 큰 정수(long long)로 변환 (String To Long Long) |
| String Float | std::stof(str) | 문자열을 단정밀도 실수(float)로 변환 (String To Float) |
| String Double | std::stod(str) | 문자열을 배정밀도 실수(double)로 변환 (String To Double) |
| Number String | std::to_string() | 정수, 실수 등의 숫자를 문자열로 변환 |
| Char Number | c - '0' | 숫자 문자의 ASCII 값에서 '0'(48)을 빼서 정수로 변환 |
| Number Char | num + '0' | 0~9 사이의 정수에 '0'(48)을 더해 숫자 문자로 변환 |
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
int x1, x2;
for (auto& c : tokens) {
if (c == "*" || c == "/" || c == "+" || c == "-") {
x2 = st.top(); st.pop();
x1 = st.top(); st.pop();
if (c == "*") st.push(x1*x2);
else if (c == "/") st.push(x1/x2);
else if (c == "+") st.push(x1+x2);
else if (c == "-") st.push(x1-x2);
}
else st.push(stoi(c));
}
return st.top();
}
};
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
int x1, x2;
for (const auto& c : tokens) {
if (c == '*' || c == '/' || c == '+' || c == '-') {
x2 = st.top(); st.pop();
x1 = st.top(); st.pop();
switch (c) {
case '*': st.push(x1*x2); break;
case '/': st.push(x1/x2); break;
case '+': st.push(x1+x2); break;
case '-': st.push(x1-x2); break;
}
}
else st.push(stoi(token));
}
return st.top();
}
};
for (auto& c : tokens) 대신 for (const auto& token : tokens)를 사용: 문자열 복사 비용 방지(읽기 전용 reference)switch 문 사용: if-else 중복 검사를 줄이고 가독성 좋아짐
데구리님 for 문 안에는 switch 안쓰는게 좋긴해여.
왜냐구요?