1. 程式人生 > >leetcode練習:504. Base 7

leetcode練習:504. Base 7

quest str tco turn res ring ase oba bsp

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

一個七進制轉換問題。

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(‘‘); } };

leetcode練習:504. Base 7