08-Draw-Line-LCCI
题目详情
解题思路
代码实现
class Solution {
public int[] drawLine(int length, int w, int x1, int x2, int y) {
int[] result=new int[length];
int width=w/32;
if(y>(length/width)){
return result;
}
if(x2>=w){
return result;
}
//定位行数所在的下标
int index=y*width;
//firstIndex为第一个数x1所在的下标
int firstIndex=x1/32+y*w/32;
//secondIndex为第二个数x2所在的下标
int secondIndex=x2/32+y*w/32;
for(int i=firstIndex;i<=secondIndex;i++){
result[i]=-1;
}
//处理首尾元素
//头元素采用不带符号位右移
result[firstIndex]=result[firstIndex]&(-1>>>(x1%32));
//尾元素采用带符号位右移
result[secondIndex]=result[secondIndex]&(Integer.MIN_VALUE>>(x2%32));
return result;
}
}课后总结
Last updated