列表

详情


FED70. 二进制转换

描述

将给定数字转换成二进制字符串。如果字符串长度不足 8 位,则在前面补 0 到满8位。

示例1

输入:

65

输出:

01000001

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 853ms, 内存消耗: 77776KB, 提交时间: 2021-05-03

{"css":"","js":"function convertToBinary(num) {\nvar s = num.toString(2);\n    while(s.length<8){\n        s = \"0\"+s;\n    }\n    return s;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 853ms, 内存消耗: 77800KB, 提交时间: 2021-01-17

{"css":"","js":"function convertToBinary(num) {\n    var s = num.toString(2);\n    return \"00000000\".slice(s.length) + s;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 854ms, 内存消耗: 77772KB, 提交时间: 2021-04-03

{"css":"","js":"function convertToBinary(num) {\n        var res = num.toString(2);\n        var resArr = res.split('');\n        if (resArr.length < 8) resArr.splice(0,0,0);\n        resArr = resArr.join(\"\")\n        return resArr;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 856ms, 内存消耗: 77804KB, 提交时间: 2021-06-05

{"css":"","js":"function convertToBinary(num) {\n    // 大部分实现是通过长度实现的\n    var s = num.toString(2)\n    var l = s.length\n    if(l<8){\n        var s1 = \"00000000\"\n        var s2 = s1.slice(0, 8-l)\n        s = s2 + s\n    }\n    return s\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 857ms, 内存消耗: 77788KB, 提交时间: 2021-04-04

{"css":"","js":"function convertToBinary(num) {\n    let res = num.toString(2);\n    if (res.length < 8) {\n        for(let i = 0; i <= 8 - res.length; i++) {\n            res = '0' + res;\n        }\n    }\n    return res;\n}","html":"","libs":[]}

上一题