---
# System prepended metadata

title: \#504 Base 7
tags: [leetcode, leetcode-easy]

---

# \#504 Base 7
## *給定十進位整數num, 回傳7進位*
## Log
 - build 20210929 by syhuang

## 遞迴版
 - leetcode submit runtime 70%, memory 38%
```javascript=
var convertToBase7 = function(num) {
    if(num<0) return '-'+convertToBase7(-num);
    if(num<7) return num+'';
    return convertToBase7(Math.floor(num/7)) + convertToBase7(num%7);
};
```
## 初見
 - leetcode submit runtime 21%, memory 8%
```javascript=
var convertToBase7 = function(num) {
    let ifNeg = num<0?'-':'';
    let t = Math.abs(num);
    let d = 0;
    let result = [];
    
    //先求最高次方
    for(; Math.pow(7,d) <= t; d++){}
    
    //開始降冪
    while(d>1){
        let com = Math.floor(t/Math.pow(7,d-1));
        result.push(com);
        t -= Math.pow(7,d-1)*com;
        d--;
    }
    result.push(t);
    return ifNeg+result.join('');
};
```
## 備註
## 參考
- [參考](https://leetcode.com/problems/base-7/discuss/98364/JavaC%2B%2BPython-Iteration-and-Recursion)
###### tags: `leetcode`, `leetcode-easy`