Using divide and remainder as basic idea In Java


  • 0
    R

    public String convertToBase7(int num) {
    // corner case
    if (num == 0) return "0";
    boolean isNegative = false;
    if (num < 0) {
    num = -num;
    isNegative = true;
    }
    StringBuilder sb = new StringBuilder();
    while (num != 0) {
    sb.append(num % 7);
    num /= 7;
    }
    if (isNegative) sb.append("-");
    return sb.reverse().toString();
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.