[C++] LeeCode 7. Reverse Integer
Language Information : C++ Description : Reverse Integer URL : https://leetcode.com/problems/reverse-integer/ Code : class Solution { public: int reverse(long long int x) { long long int sum = 0; long long int num = abs(x); vector<int> vec; while(num>0){ long long int temp = num%10; vec.push_back(temp); num = num/10; } for(int i=0;i<vec.size();i++){ sum += vec[i]*pow(10,vec.size()-i-1); } if(x<0){ sum = 0-sum; } if(sum < INT_MIN || sum > INT_MAX){ return 0; }else{ return (int)sum; } } }; - 使用迴圈去%10並存入vector, 在使用迴圈反過來乘上十的N次方, 要注意負數, 再來要注意到此題要求最終答案小於int的範圍要回傳0。