注意相加的顺序,从高位加到低位
class Solution { public: int alternateDigitSum(int n) { vector<int> num; for(; n; n /= 10) num.push_back(n % 10); int ans = 0, sign = 1, length=num.size(); for(int i=length-1; i>=0; i--){ ans += num[i] * sign; sign = -sign; } return ans; } };
注意: