Leetcode
  • Leetcode Questions
  • Runtime Screenshots
  • DataStructure
    • Leetcode 394. Decode String
    • Leetcode 225. Implement Stack Using Queues
    • Leetcode 336. Palindrome Pairs
    • Leetcode 316. Remove Duplicate Letters
    • Leetcode 206. Reverse Linked List
    • Leetcode 347. Top K Frequent Elements
    • Leetcode 227. Basic Calculator II
    • Leetcode 224. Basic Calculator
  • Linear
    • Leetcode 23. Merge k Sorted Lists
    • Leetcode 48. Rotate Image
    • Leetcode 6. ZigZag Conversion
    • Leetcode 438. Find All Anagrams in a String
    • Leetcode 189. Rotate Array
    • LeetCode 56. Merge Intervals
    • Leetcode 4. Median of Two Sorted Array
    • Leetcode 3. Longest Substring Without Repeating Characters
    • Leetcode 8. String to Integer (atoi)
    • Leetcode 5. Longest Palindromic Substring
    • Leetcode 11. Container With Most Water
  • Tree
    • Leetcode 103. Binary Tree Zigzag Level Order Traversal
    • Leetcode 508. Most Frequent Subtree Sum
    • Leetcode 226. Invert Binary Tree
    • Leetcode 222. Count Complete Tree Nodes
    • Leetcode 250. Count Univalue Subtrees
    • Leetcode 285. Inorder Successor in BST
    • Leetcode 230. Kth Smallest Element in a BST
    • Leetcode 543. Diameter of Binary Tree
    • Leetcode 199. Binary Tree Right Side View
  • Math
    • Leetcode 50. Power(x, n)
    • Leetcode 166. Fraction to Recurring Decimal
    • Leetcode 7. Reverse Integer
    • Leetcode 360. Sort Transformed Array
    • Leetcode 367. Valid Perfect Square
    • Leetcode 12. Integer to Roman
  • DynamicProgramming
    • Leetcode 10. Regular Expression Matching
    • Leetcode 253. Meeting Rooms II
    • Leetcode 303. Range Sum Query
    • Leetcode 22. Generate Parentheses
  • Graph
    • Leetcode 142. Linked List Cycle II
    • Leetcode 261. Graph Valid Tree
    • Leetcode 339. Nested List Weight Sum
    • Leetcode 207. Course Schedule
  • OODesign
    • Leetcode 295. Find Media From Data Stream
Powered by GitBook
On this page
  • Question
  • Thoughts
  • Solution
  • Complexity Analysis
  • Extension
  • Summary
  • Reference

Was this helpful?

  1. Math

Leetcode 7. Reverse Integer

Question

Given a 32-bit 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 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.

Thoughts

The word "reverse" reminds us of stack or queue. However, in this question we don't need an actual data structure, but we can mimick such behaviour with integer operations.

Given any integer x, we can get its digits from right to left using following "pop" operation.

repeat while x > 0:
    digit = x % 10          // get last digit
    x /= 10                 // remove last digit

Since we are able to get those digits in reverse order, we only need a "queue" to store the result. Again, we can use integer operations to mimick "push to back".

while we have digits:
    result *= 10                // make space for a new rightmost digit
    result += digit             // push to back the new rightmost digit

Then we only need to handle some the edge cases such as integer overflow. We can compare the integer result with Integer.MAX_VALUE / 10, or we can simply use a long type to store intermediate result. I prefer the latter. Nonetheless, do confirm what types you can use in a given context.

Solution

This solution can certainly be simplified. No need to use sign or the long type result. Yet, I write it as such to ensure the best readability. Codes are for humans, not machines.

class Solution {
    public int reverse(int x) {

        int sign = (x >= 0) ? 1 : -1;

        // get digits in reverse order
        x = Math.abs(x);
        long res = 0;
        while (x > 0) {

            res = res * 10 + x % 10; // push to back the peeked value
            x /= 10;                 // pop 

            // check overflow
            if (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) return 0;       

        }
        return (int) res * sign;
    }
}

Complexity Analysis

  • Time Complexity: O(n). We iterate through every digit of the input integer. n is the number of digits x has, which is approximately log10(x).

  • Space Complexity: O(1). We don't need any extra space since we ues integer operations to mimick the stack/queue data structure.

Extension

If the input is of type float, can you reverse it and keep the number of decimals? Also, we can use this method to solve Leetcode 9, palindromic integer.

Summary

To solve this type of question, we first consider the underlying core ideas: stack or queue that reverses something. Then we move beyond that to see if we can achieve the same behaviour without wasting extra space.

As a side note, this is the first Leetcode question I tried two years ago when I was a freshman. Even though I ranked the very top in all three of intro cs courses (programming and data structures), I spent over 45 minutes and still failed twice. Back then I didn't have the fundamental instincts for algorithm questions. Now, after one month of light practice and consistent summary, I began to grasp the hints and core ideas behind these questions. I solved it under 10 mintues and beated 100% in first submission.

Reference

PreviousLeetcode 166. Fraction to Recurring DecimalNextLeetcode 360. Sort Transformed Array

Last updated 5 years ago

Was this helpful?

original series

Mao's