stringbuilder java


  • 0
    J
    public class Solution {
        public String convertToBase7(int num) {
            if(num == 0) return "0";
            
            StringBuilder sb = new StringBuilder();
            
            boolean neg = num < 0;
            num = Math.abs(num);
            
            while(num > 0){
                Integer r = num % 7;
                sb.append(r.toString());
                
                num /= 7;
            }
            
            if(neg) 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.