A very easy Solution by Stringsteam C++


  • 0
    Y

    This problem isn't interesting at all. AC it by stringsteam, don't look it again because it doesn't worth any effort on it.

    #include <sstream>
    class Solution {
    public:
        int val=0;
        void add(char x)
        {
            val=val*10+x-'0';
        }
        int myAtoi(string str) {
            stringstream st;
            if(str.length()==0) return 0;
            st<<str;
            int val=0;
            try
            {
                st>>val;
            }catch(...)
            {
                return 0;
            }
            return val;
        }
    };
    

  • 0
    F

    are you serious...


  • 0
    Y

    Yes, if you try to solve step by step instead of by stringstream you have to consider all possibility of input , which is so annoying .


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.