Reversing Integers on LeetCode with Java

🚀 Day 9 of My DSA Journey Today I solved the “Reverse Integer” problem on LeetCode using Java. This problem focuses on number manipulation, loops, and handling integer overflow, which makes it slightly tricky compared to basic problems. 💡 Problem Overview We are given a signed 32-bit integer x, and the task is to reverse its digits. Example: Input: 123 → Output: 321 Input: -123 → Output: -321 However, there is an important condition: ⚠️ If reversing the number causes it to go outside the 32-bit integer range, we must return 0. The valid integer range is: −2³¹ to 2³¹ − 1 ⚙️ Approach I Used I solved this problem using mathematical digit extraction: 1️⃣ Extract the last digit using x % 10 2️⃣ Remove the last digit from the number using x / 10 3️⃣ Build the reversed number using sum = sum * 10 + digit 4️⃣ Before adding the digit, check overflow condition using Integer.MAX_VALUE and Integer.MIN_VALUE. 📊 Complexity Analysis Time Complexity: O(log₁₀ n) Because the number of digits in n determines the number of loop iterations. Space Complexity: O(1) No extra memory is used. 📚 Key Learnings ✔ Practiced digit manipulation using modulus and division ✔ Learned how to handle integer overflow conditions ✔ Strengthened understanding of mathematical logic in algorithms 💻 Result ✅ Accepted ⚡ Runtime: 1 ms (Beats 99.88%) Small progress every day is building a strong foundation in Data Structures and Algorithms. On to Day 10 tomorrow. 💪 #DSA #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #LearningInPublic #SoftwareDevelopment #Consistency

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories