leetcode練習:504. Base 7

Given an integer, return its base 7 string representation.spa

Example 1:code

Input: 100
Output: "202"

 

Example 2:blog

Input: -7
Output: "-10"

 

Note: The input will be in range of [-1e7, 1e7].ip

一個七進制轉換問題。input

var convertToBase7 = function(num) {
    var num1 = Math.abs(num);    
    var i,j;
    var cur; 
    var res =[];
    
    if(num==0 || num==  -0){
        return "0";
    }
    
    while(num1 > 0) {
        res.unshift(num1 % 7);
        num1 = parseInt(num1/7);
    }
    
    if(num >= 0)
    return res.join('');
    
    if(num < 0){
        res.unshift("-");
        return res.join('');
    }
    
};
相關文章
相關標籤/搜索