首页 > 其他 > 详细

(Easy) Base 7 LeetCode

时间:2019-08-12 18:08:24      阅读:86      评论:0      收藏:0      [点我收藏+]

Description:

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].

 

Solution:

class Solution {
    public String convertToBase7(int num) {
        if(num==0){
            return "0";
        }
        int dig = 0;
         String result = "";
        if(num>0){
        result = "";
        do {
            
            dig = num%7;
            num = num/7;
            
            result = result + dig;
            
            
        }   
        
            while (num>0);
                
                
        return Reverse(result);
                
        }
        else{
            
            num = -num;
            
               do {
            
            dig = num%7;
            num = num/7;
            
            result = result + dig;
            
            
        }   
        
            while (num>0);

    
        result = result+"-";
            return Reverse(result);
    }
    
}
    
    
    public String Reverse(String a ){
        
        StringBuffer sb = new StringBuffer(a);
        return sb.reverse().toString();
    }
}

 

(Easy) Base 7 LeetCode

原文:https://www.cnblogs.com/codingyangmao/p/11341616.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!