Very Easy C# solution


  • 0
    X
    public class Solution {
        public int Reverse(int x) {
            var q=x.ToString();
            long ans=0;
            if(x>0){
                for(var i=q.Length-1;i>=0;i--){
                    var tmp=Convert.ToInt32(q[i])-48;
                      ans*=10;
                    ans+=tmp;
                }
            }
            else if(x<0){
                for(var i=q.Length-1;i>0;i--){
                    var tmp=Convert.ToInt32(q[i])-48;
                      ans*=10;
                    ans-=tmp;
                }
            }
            return (ans<-2147483648 || ans>2147483647) ? 0 : (int)ans;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.