LeetCode 面试题 05.08. 绘制直线
题目
解题
解题一:一位一位修改
// javascript
var drawLine = function(length, w, x1, x2, y) {
const line = new Array(length).fill(0);
const width = w / 32;
for (let b = x1; b <= x2; b++) {
line[y * width + Math.floor(b / 32)] |= 1 << (31 - b % 32);
}
return line;
};
解题二:高效解法
书上是以 byte 为单位存储的,LeetCode 上是以 word 为单位。
// javascript
var drawLine = function(length, w, x1, x2, y) {
const screen = new Array(length).fill(0);
const position