# \#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`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up