ACWING87. The string into an integer (the offer to prove safety)

Two input lists, find their first common node.

When there is a common node, the node returns empty.

Sample
Given two lists as follows:
A: A1 A2 →

C1 C2 → C3 →

B: B1 → B2 → B3

A first common output node c1

class Solution {
public:
    typedef long long ll;
    const ll maxn = (1 << 31) - 1;
    const ll minn = -(1 << 31);
    
    int strToInt(string str) {
        int n = str.size();
        ll ans = 0;
        int posi = 1;
        int flag = 1;
        for(int i = 0;i < n;i++) {
            if(str[i] == '+') posi = 1;
            else if(str[i] == '-') posi = -1;
            else if(str[i] == ' ') continue;
            else if(str[i] >= '0' && str[i] <= '9') {
                ans = ans * 10 + str[i] - '0';
                if(posi == 1 && ans > maxn) ans = maxn;
                else if(posi == -1 && ans > -minn) ans = -minn;
                flag = 0;
            }
            else if(flag) return 0;
        }
        return ans * posi;
    }
};
Published 843 original articles · won praise 28 · views 40000 +

Guess you like

Origin blog.csdn.net/tomjobs/article/details/104978512