绘制直线
难度:
标签:
题目描述
A monochrome screen is stored as a single array of int, allowing 32 consecutive pixels to be stored in one int. The screen has width w
, where w
is divisible by 32 (that is, no byte will be split across rows). The height of the screen, of course, can be derived from the length of the array and the width. Implement a function that draws a horizontal line from (x1, y)
to (x2, y)
.
Given the length of the array, the width of the array (in bit), start position x1
(in bit) of the line, end position x2
(in bit) of the line and the row number y
of the line, return the array after drawing.
Example1:
Input: length = 1, w = 32, x1 = 30, x2 = 31, y = 0 Output: [3] Explanation: After drawing a line from (30, 0) to (31, 0), the screen becomes [0b000000000000000000000000000000011].
Example2:
Input: length = 3, w = 96, x1 = 0, x2 = 95, y = 0 Output: [-1, -1, -1]
代码结果
运行时间: 27 ms, 内存: 16.3 MB
/*
* 思路:
* 1. 与普通 Java 方法相同的逻辑,只是利用 Java Stream 来处理。
* 2. 我们将使用 IntStream 和 lambda 表达式来处理屏幕的绘制。
*/
import java.util.stream.IntStream;
public int[] drawLine(int length, int w, int x1, int x2, int y) {
int[] screen = new int[length];
int startIdx = y * (w / 32) + (x1 / 32);
int endIdx = y * (w / 32) + (x2 / 32);
int startOffset = x1 % 32;
int endOffset = x2 % 32;
if (startIdx == endIdx) {
screen[startIdx] |= ((1 << (endOffset - startOffset + 1)) - 1) << (31 - endOffset);
} else {
screen[startIdx] |= ((1 << (32 - startOffset)) - 1) << startOffset;
IntStream.range(startIdx + 1, endIdx).forEach(i -> screen[i] = -1);
screen[endIdx] |= (1 << (endOffset + 1)) - 1;
}
return screen;
}
解释
方法:
这个题解通过分段绘制直线来处理问题。首先计算出每行32个像素对应一个整数值,再根据输入的x1和x2计算出要修改的整数范围。题解的主要步骤包括:1. 计算影响到的起始和结束索引。2. 对于起始和结束索引内的所有整数,设置为全1(即-1,因为整数中的32个1表示的二进制数转换为整数是-1)。3. 对于起始和结束索引,根据x1和x2的位置设置相应的位。4. 如果x1和x2在同一个整数内,需要特别处理这一整数的位。5. 使用_to32bit函数处理整数的表示,确保返回的整数值是正确的(处理了整数的符号位)。
时间复杂度:
O(length)
空间复杂度:
O(length)
代码细节讲解
🦆
为什么在初始化结果数组时,使用的是长度为`length`的数组,而不是基于屏幕宽度`w`和高度来计算所需数组的大小?
▷🦆
在计算起始和结束索引时,为什么要加上`width * y`,这里的`y`是如何影响整个索引的计算的?
▷🦆
函数`_to32bit`用于处理负数情况,但是这里的例子是否真的会出现整数值为负数的情况?如果会,是在什么情况下?
▷