码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Reverse Integer

时间:2015-10-02 22:34:33      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:



技术分享
  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类型




























[LeetCode]Reverse Integer

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!