首页 > 学院 > 开发设计 > 正文

7. Reverse Integer

2019-11-06 08:15:48
字体:
来源:转载
供稿:网友
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Note:The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.Subscribe to see which companies asked this question.

简单题

class Solution {public: int reverse(int x) { string s,re; s=to_string(x); if(s[0]=='-') s.erase(0,1); for(auto x:s) re=x+re; if(x>0) return stoll(re)>INT32_MAX?0:stoll(re); else return stoll(re)>INT32_MAX?0:-1*stoll(re); }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表