<address id="n1vvb"></address>
          <font id="n1vvb"></font>

            <thead id="n1vvb"></thead>

                LeetCode #8 简单题 (小模拟一下就行了)

                题目:实现atoi

                class Solution {
                public:
                    int myAtoi(string str) {
                        int start = -1;
                        for (int i = 0; i < str.size(); ++i){
                            if (str[i] !=   && str[i] != 0){
                                start = i;
                                break;
                            }
                        }
                        if (start == -1)return 0;
                                
                        long long ans = 0;
                        int isLess0 = 1;
                        int INT_MINN = -2147483648, INT_MAXX = 2147483647;
                        if (str[start] == -) isLess0 = -1;
                        else if (str[start] == +){
                            isLess0 = 1;
                            start += 1;
                        }
                        int left = isLess0 == -1 ? start + 1 : start;
                        for (int i = left; i < str.size(); ++i){
                            if (str[i] >= 0 && str[i] <= 9){
                                ans = ans * 10 + (str[i] - 0);
                            }
                            else{
                                break;
                            }
                            if (ans * isLess0 < INT_MINN){
                                return INT_MINN;
                            }
                            if (ans * isLess0 > INT_MAXX){
                                return INT_MAXX;
                            }
                        }
                        return (int)(ans * isLess0);
                    }
                };
                相关文章
                相关标签/搜索
                特马资料最准2019四肖期期准四肖三期内必出四肖期期准免费公开四肖中特1肖1码期期大公开