LeetCode #8

String to Integer (atoi)

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
class Solution {
public:
    int myAtoi(string s) {
        long long ret = 0;
        bool minus = false, found = false;
        for(char ch: s) {
            if(ch == '-') {
                if(found) break;
                minus = true, found = true;
            }
            else if(ch == '+') {
                if(found) break;
                found = true;
            }
            else if('0' <= ch && ch <= '9') {
                found = true;
                ret = ret * 10 + ch - 48;
                if(minus && ret > 2147483648LL) ret = 2147483648;
                else if(!minus && ret > 2147483647LL)ret = 2147483647;
            }
            else if(ch == ' ') {
                if(found) break;
            }
            else break;
        }
        return minus ? -ret : ret;
    }
};

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.