My AC codes for Reverse Integer: Any better one?


  • 0
    W

    It's not necessary to use reverse function. As following aims to make as simple as possible:

    class Solution {
    public:
        int reverse(int x) {
            char intArray[32];
            char index=0;
            int res = 0;
            res = x<0?0-x:x;
            while(res != 0){
                intArray[index++] = res %10;
                res /= 10;
            }
            for(int i=0; i<index; ++i){
                res = res*10 + intArray[i];
            }
            if(x < 0)
                res = 0-res;
            return res;
        }
    };

  • 0
    W
    This post is deleted!

  • 0
    S
    This post is deleted!

  • 4
    P

    my answer is as following,runtime 92ms

    class Solution {
    public:
        int reverse(int x) {
            int i,s = 0;
    		for(i = 0; x != 0; i++)
    		{
    			s = (s+x % 10)*10;
    			x = x / 10;	
    		}
    		if(x < 0)
    			s = -s;
    		return s/10;
        }
    	
    };

  • 2
    V

    Space complexity of the above algorithm is O(1) though the array takes an additional auxiliary space, because the array size is fixed and does not vary with input, space complexity can be expressed as c*O(1) and here c is 32 .

    Although a constant does not affect how the function behaves as input sizes grow towards infinity, but one might be interested in a large constant overhead and how the function performs for smaller input sizes, thus the constant size can be reduced to one by using a single integer variable.

    public class Solution{
    public int reverse(int x) {
    	boolean flag = false;
    	if (x < 0) {
    		x = 0 - x;
    		flag = true;
    	}
    	int res = 0;
    	int p = x;
     
    	while (p > 0) {
    		int mod = p % 10;
    		p = p / 10;
    		res = res * 10 + mod;
    	}
     	if (flag) {
    		res = 0 - res;
    	}
     	return res;
    }
    }
    

  • 0
    W

    Thanks for the reply! But I can't agree your space complexity and time complexity. In my understanding the space and time complexity of my algo is both O(1), because the space and iteration times both are independent with input.


  • 0
    V

    I modified the post based on your suggestions..time complexity O(logn) as in every iteration the input integer is divided by 10, so time complexity is of order log to base 10.


  • 5
    C
    class Solution{
    public:
        int reverse(int x){
            int s = 0;         
            while (x != 0) {
                s =s*10 + x%10;
                x = x/10;
            }
            return s;
        }
    };
    

    this is my ac code.


  • 0
    C

    you can delete the code on line:
    if(x < 0)
    s = -s;

    when the for loop is done. x == 0. the code if(x < 0) never be execute.


  • 1
    R
    class Solution {
    public:
        int reverse(int x) {
            stringstream t;
            t<<x;
            string str;
            t>>str;
            if(str[0]=='-')
                std::reverse(str.begin()+1,str.end());
            else
                std::reverse(str.begin(),str.end());
            t.clear();
            t<<str;
            int result;
            t>>result;
            return result;
        }
    };

  • 0
    W

    Refer to my solution

    http://whiteboardcoding.blogspot.com/2015/05/reverse-integer.html

    Programming Language: C#


  • 0
    Y

    My answer is the same . but it show wrong


Log in to reply
 

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