[LeetCode]Reverse Integer




  1. class Solution {
  2. public:
  3. int reverse(int x) {
  4. int re;
  5. string s = to_string(x);
  6. auto l = s.begin();
  7. auto r = prev(s.end());
  8. if(*l=='-')l++;
  9. while(l<r){
  10. char temp = *l;
  11. *l = *r;
  12. *r = temp;
  13. l++;
  14. r--;
  15. }
  16. try{
  17. re = stoi(s);
  18. }catch(exception e){
  19. re = 0;
  20. }
  21. return re;
  22. }
  23. };
to_string函数是将int型或者浮点型转换为字符串
stoi函数是将string类型转换为string类型的。
atoi函数是将char* 类型的转换为string类型




























原文地址:https://www.cnblogs.com/zhuzhenfeng/p/6b2e5781004566ff6ca9bdddfb765948.html