leetcode 7
stringstream和INT_MAX和INT_MIN
1 class Solution { 2 public: 3 int reverse(int x) { 4 std::stringstream ss; 5 std::string str,result; 6 long long answer; 7 ss<<x; 8 ss>>str; 9 int start=0,end; 10 if(str[0]=='-') 11 { 12 result="-"; 13 start=1; 14 } 15 else 16 result=""; 17 end=str.size()-1; 18 for(int i=end;i>=start;i--) 19 result.push_back(str[i]); 20 ss.clear(); 21 ss.str(""); 22 ss<<result; 23 ss>>answer; 24 if(answer>INT_MAX||answer<INT_MIN) 25 return 0; 26 return answer; 27 } 28 };