LeetCode has a problem to reverse digits of an integer number without using any library method like reverse() method of StringBuffer. In LeetCode, you can solve this problem with many different languages e.g. Java, C, C++, C#, Python, Ruby and even JavaScript. BTW, in article, we will learn how to solve this problem in Java. Before approaching solution let's first read the problem statement :
Reverse digits of an integer.
Example 1: x = 123, return 321
Example 2: x = -123, return -321
Problem looks simple but its not simple there are many things you need to consider in order to produce a solution which is accepted by LeetCode, which has thousands of test cases to test your solution. For example, you need to consider not just about positive integer but also about negative integer. Remember, positive integer can also be written using + sign, so you need to handle that as well. If the integer's last digit is 0, what should the output be? i.e. cases such as 10, 100. If return type of method is integer than you can simply return 1, its perfectly Ok, but if return type of method is String then you may need to return 001 or 0001. For purpose of this solution, we expect our method to return integer, so 1 is fine. By the way, if you are solving LeetCode problems as part of your interview preparation then you can also see Programming Interviews Exposed and Cracking the Coding Interview, two of the most useful books for preparing programming job interviews. You will learn more in short time.
Read more »
Reverse digits of an integer.
Example 1: x = 123, return 321
Example 2: x = -123, return -321
Problem looks simple but its not simple there are many things you need to consider in order to produce a solution which is accepted by LeetCode, which has thousands of test cases to test your solution. For example, you need to consider not just about positive integer but also about negative integer. Remember, positive integer can also be written using + sign, so you need to handle that as well. If the integer's last digit is 0, what should the output be? i.e. cases such as 10, 100. If return type of method is integer than you can simply return 1, its perfectly Ok, but if return type of method is String then you may need to return 001 or 0001. For purpose of this solution, we expect our method to return integer, so 1 is fine. By the way, if you are solving LeetCode problems as part of your interview preparation then you can also see Programming Interviews Exposed and Cracking the Coding Interview, two of the most useful books for preparing programming job interviews. You will learn more in short time.
No comments:
Post a Comment