LeetCode Reverse Integer

来源:互联网 发布:java我要自学网 编辑:程序博客网 时间:2024/06/12 01:19

题目链接:https://oj.leetcode.com/problems/reverse-integer/


Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321


不解释


class Solution {public:    int reverse(int x) {        bool flag=false;        if(x<0) flag=true;        x=abs(x);        int ans=0;        while(x)        {            ans=ans*10+x%10;            x/=10;        }        if(flag)            ans=-ans;        return ans;    }};


0 0
原创粉丝点击