504. Base 7
Given an integer, return its base 7 string representation.
Example 1:
Input: 100
Output: “202”
Example 2:
Input: -7
Output: “-10”
Note: The input will be in range of [-1e7, 1e7].
Resolution
1 | class Solution { |
Time Complexity: O(logn)
Space Complexity: O(logn)
note:
important: num.toString( ); ——> wrong. Object numberdo not has the method “toString( )”
don’t foget to check the initial condition firstly.
A good solution of java Integer
1 | public String convertToBase7(int num) { |
C++ solution
1 | class Solution { |
key:implicit handle of special case 0 —>> using do{ }while( );
Time Complexity: O(logn)
Space Complexity: O(logn)