我认为:最好理解的代码

反转数字

https://www.nowcoder.com/practice/1a3de8b83d12437aa05694b90e02f47a

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param x int整型 
     * @return int整型
     */
    int reverse(int x) {
        // write code here
        string s;
        long long ans = 0;
        s = to_string(x);
        for(int i = s.size() -1;i >= 0;i--){
            if(s[i] == '-')
                ans *= -1;
            else 
                ans *= 10,ans += s[i] - '0';
        }
        if(ans == (int)ans)
            return ans;
        else 
            return 0;
    }
};

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务