Skip to main content
added 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

faster Faster code for leetcode reverse int

This is my solution for the leetcode reverse int question:

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123 Output: 321 Example 2:

Input: -123 Output: -321 Example 3:

Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

thisThis is my solution for leetcode reverse int question.the output:

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123 Output: 321 Example 2:

Input: -123 Output: -321 Example 3:

Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer rangeRuntime: [−2^3152 ms, 2^31 − 1]faster than 62. For the purpose65% of this problem, assume that your function returns 0 when the reversed integer overflowsC# online submissions for Reverse Integer.

this is the output Runtime: 52 ms, faster than 62.65% of C# online submissions for Reverse Integer.

Can you please comment on how can I make this code run faster? I I guess the modulo and division are the key factors of speed here. Thanks

faster code for leetcode reverse int

this is my solution for leetcode reverse int question.

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123 Output: 321 Example 2:

Input: -123 Output: -321 Example 3:

Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

this is the output Runtime: 52 ms, faster than 62.65% of C# online submissions for Reverse Integer.

Can you please comment on how can I make this code run faster? I guess the modulo and division are the key factors of speed here. Thanks

Faster code for leetcode reverse int

This is my solution for the leetcode reverse int question:

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123 Output: 321 Example 2:

Input: -123 Output: -321 Example 3:

Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

This is the output:

Runtime: 52 ms, faster than 62.65% of C# online submissions for Reverse Integer.

Can you please comment on how can I make this code run faster? I guess the modulo and division are the key factors of speed here.

edited tags
Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191
Source Link
Gilad
  • 5.4k
  • 5
  • 38
  • 65

faster code for leetcode reverse int

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArrayQuestions
{
    /// <summary>
    /// Given a 32-bit signed integer, reverse digits of an integer.
    ///
    /// Example 1:
    ///
    /// Input: 123
    /// Output: 321
    /// Example 2:
    ///
    /// Input: -123
    /// Output: -321
    /// Example 3:
    ///
    /// Input: 120
    /// Output: 21
    /// Note:
    /// Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            int temp = 1534236469;
            int result = Reverse(temp);
            Assert.AreEqual(0, result);
        }

        [TestMethod]
        public void TestMethod2()
        {
            int temp = 123;
            int result = Reverse(temp);
            Assert.AreEqual(321, result);
        }
        public int Reverse(int x)
        {
            int result = 0;
            bool negative = false;
            if (x < 0)
            {
                negative = true;
                x = -x;
            }

            int prevRevNum = 0;
            while (x != 0)
            {
                int currentDigit = x % 10;
                result = (result * 10) + currentDigit;

                if ((result - currentDigit) / 10 != prevRevNum)
                {
                    return 0;
                }
                prevRevNum = result;
                x = x / 10;
            }

            if (negative)
            {
                return -result;
            }

            return result;
        }
    }
}

this is my solution for leetcode reverse int question.

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123 Output: 321 Example 2:

Input: -123 Output: -321 Example 3:

Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

this is the output Runtime: 52 ms, faster than 62.65% of C# online submissions for Reverse Integer.

Can you please comment on how can I make this code run faster? I guess the modulo and division are the key factors of speed here. Thanks