var addBinary = function(a, b) { let maxLen = Math.max(a.length,b.length) a = a.padStart(maxLen, '0') b = b.padStart(maxLen, '0') let flag = 0 let res = '' let i = maxLen - 1 while(i >= 0) { flag = Number(a[i]) + Number(b[i]) + flag res = flag % 10 + res; flag = Math.floor(flag/10) i-- } res = flag == 1 ? '1' + res : res return res }
/** * @param {string}a * @param {string}b * @return {string} */ var addBinary = function(a, b) { let maxLen = Math.max(a.length,b.length) a = a.padStart(maxLen, '0') b = b.padStart(maxLen, '0') let flag = 0 let res = '' let i = maxLen - 1 while(i >= 0) { flag = Number(a[i]) + Number(b[i]) + flag res = flag % 2 + res; flag = Math.floor(flag/2) i-- } res = flag == 1 ? '1' + res : res return res };
/** * @param {string}a * @param {string}b * @param {number}radix 进制数 * @return {string} */ var addBinary = function(a, b, radix) { let maxLen = Math.max(a.length,b.length) a = a.padStart(maxLen, '0') b = b.padStart(maxLen, '0') let flag = 0 let res = '' let i = maxLen - 1 while(i >= 0) { flag = Number(a[i]) + Number(b[i]) + flag res = flag % radix + res; flag = Math.floor(flag/radix) i-- } res = flag == 1 ? '1' + res : res return res };
/** * @param {string}num1 * @param {string}num2 * @return {string} */ var addStrings = function(a, b) { let l1 = a.length - 1 let l2 = b.length - 1 let flag = 0 let res = '' while(l1 >= 0 || l2 >= 0) { let x = l1 >=0 ? + a[l1] : 0 let y = l2 >=0 ? + b[l2] : 0 flag = x + y + flag res = flag % 10 + res; flag = Math.floor(flag/10) l1-- l2-- } res = flag == 1 ? '1' + res : res return res };