Created
December 15, 2017 03:08
-
-
Save jianminchen/847e881cac523ca0d4a9ad16270d404f to your computer and use it in GitHub Desktop.
convert to string - code review written in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Write a function that converts an int into its alpha-numeric equivalent represented as a null terminated string. | |
The function should accept an int as input and return a string as output. For instance, calling the function with | |
an int value of 324 would return a null terminated string containing "324". Ensure that your function checks for | |
appropriate boundary conditions and edge cases. Assume you cannot use any standard libraries (for example, no itoa or sprintf). | |
**/ | |
class Solution{ | |
char[] convertToString(int n) | |
{ | |
if (n == 0){ | |
return new char[] {'0'}; | |
} | |
return helper(n).toCharArray(); | |
} | |
// base case - n is one digit, n > 0 | |
// 1 -> "1" | |
// 9 - "9 | |
// base case n < 10 | |
// '-' + | |
String helper(long n){ | |
if (n < 0){ | |
return "-" + helper(Math.abs(n)); | |
} | |
if (n < 10 0){ | |
return API('0' + n); | |
} | |
String s = ""; | |
int rightMost = n % 10; | |
char c = '0' + digit; | |
return helper(n / 10) + StrAPI(c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment