Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0aa109

Browse files
feat: add solutions to lcci problem: No.05.08 (doocs#2672)
1 parent c993d20 commit a0aa109

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

‎lcci/05.08.Draw Line/README.md‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,64 @@
1919

2020
## 解法
2121

22+
### 方法一:位运算
23+
24+
我们先算出 $x_1$ 和 $x_2$ 在结果数组中的位置,记为 $i$ 和 $j$。然后将 $i$ 到 $j$ 之间的元素置为 $-1$。
25+
26+
如果 $x_1 \bmod 32 \neq 0,ドル我们需要将 $i$ 位置的元素的前 $x_1 \bmod 32$ 位置为 0ドル$。
27+
28+
如果 $x_2 \bmod 32 \neq 31,ドル我们需要将 $j$ 位置的元素的后 31ドル - x_2 \bmod 32$ 位置为 0ドル$。
29+
30+
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
31+
32+
<!-- tabs:start -->
33+
34+
```python
35+
class Solution:
36+
def drawLine(self, length: int, w: int, x1: int, x2: int, y: int) -> List[int]:
37+
ans = [0] * length
38+
i = (y * w + x1) // 32
39+
j = (y * w + x2) // 32
40+
for k in range(i, j + 1):
41+
ans[k] = -1
42+
ans[i] = (ans[i] & 0xFFFFFFFF) >> (x1 % 32) if x1 % 32 else -1
43+
ans[j] &= -0x80000000 >> (x2 % 32)
44+
return ans
45+
```
46+
47+
```java
48+
class Solution {
49+
public int[] drawLine(int length, int w, int x1, int x2, int y) {
50+
int[] ans = new int[length];
51+
int i = (y * w + x1) / 32;
52+
int j = (y * w + x2) / 32;
53+
for (int k = i; k <= j; ++k) {
54+
ans[k] = -1;
55+
}
56+
ans[i] = ans[i] >>> (x1 % 32);
57+
ans[j] &= 0x80000000 >> (x2 % 32);
58+
return ans;
59+
}
60+
}
61+
```
62+
63+
```cpp
64+
class Solution {
65+
public:
66+
vector<int> drawLine(int length, int w, int x1, int x2, int y) {
67+
vector<int> ans(length);
68+
int i = (y * w + x1) / 32;
69+
int j = (y * w + x2) / 32;
70+
for (int k = i; k <= j; ++k) {
71+
ans[k] = -1;
72+
}
73+
ans[i] = ans[i] & unsigned(-1) >> (x1 % 32);
74+
ans[j] = ans[j] & unsigned(-1) << (31 - x2 % 32);
75+
return ans;
76+
}
77+
};
78+
```
79+
80+
<!-- tabs:end -->
81+
2282
<!-- end -->

‎lcci/05.08.Draw Line/README_EN.md‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,64 @@
2727

2828
## Solutions
2929

30+
### Solution 1: Bit Manipulation
31+
32+
First, we calculate the positions of $x_1$ and $x_2$ in the result array, denoted as $i$ and $j$. Then, we set the elements between $i$ and $j$ to $-1$.
33+
34+
If $x_1 \bmod 32 \neq 0,ドル we need to set the first $x_1 \bmod 32$ bits of the element at position $i$ to 0ドル$.
35+
36+
If $x_2 \bmod 32 \neq 31,ドル we need to set the last 31ドル - x_2 \bmod 32$ bits of the element at position $j$ to 0ドル$.
37+
38+
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
39+
40+
<!-- tabs:start -->
41+
42+
```python
43+
class Solution:
44+
def drawLine(self, length: int, w: int, x1: int, x2: int, y: int) -> List[int]:
45+
ans = [0] * length
46+
i = (y * w + x1) // 32
47+
j = (y * w + x2) // 32
48+
for k in range(i, j + 1):
49+
ans[k] = -1
50+
ans[i] = (ans[i] & 0xFFFFFFFF) >> (x1 % 32) if x1 % 32 else -1
51+
ans[j] &= -0x80000000 >> (x2 % 32)
52+
return ans
53+
```
54+
55+
```java
56+
class Solution {
57+
public int[] drawLine(int length, int w, int x1, int x2, int y) {
58+
int[] ans = new int[length];
59+
int i = (y * w + x1) / 32;
60+
int j = (y * w + x2) / 32;
61+
for (int k = i; k <= j; ++k) {
62+
ans[k] = -1;
63+
}
64+
ans[i] = ans[i] >>> (x1 % 32);
65+
ans[j] &= 0x80000000 >> (x2 % 32);
66+
return ans;
67+
}
68+
}
69+
```
70+
71+
```cpp
72+
class Solution {
73+
public:
74+
vector<int> drawLine(int length, int w, int x1, int x2, int y) {
75+
vector<int> ans(length);
76+
int i = (y * w + x1) / 32;
77+
int j = (y * w + x2) / 32;
78+
for (int k = i; k <= j; ++k) {
79+
ans[k] = -1;
80+
}
81+
ans[i] = ans[i] & unsigned(-1) >> (x1 % 32);
82+
ans[j] = ans[j] & unsigned(-1) << (31 - x2 % 32);
83+
return ans;
84+
}
85+
};
86+
```
87+
88+
<!-- tabs:end -->
89+
3090
<!-- end -->

‎lcci/05.08.Draw Line/Solution.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
vector<int> drawLine(int length, int w, int x1, int x2, int y) {
4+
vector<int> ans(length);
5+
int i = (y * w + x1) / 32;
6+
int j = (y * w + x2) / 32;
7+
for (int k = i; k <= j; ++k) {
8+
ans[k] = -1;
9+
}
10+
ans[i] = ans[i] & unsigned(-1) >> (x1 % 32);
11+
ans[j] = ans[j] & unsigned(-1) << (31 - x2 % 32);
12+
return ans;
13+
}
14+
};

‎lcci/05.08.Draw Line/Solution.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int[] drawLine(int length, int w, int x1, int x2, int y) {
3+
int[] ans = new int[length];
4+
int i = (y * w + x1) / 32;
5+
int j = (y * w + x2) / 32;
6+
for (int k = i; k <= j; ++k) {
7+
ans[k] = -1;
8+
}
9+
ans[i] = ans[i] >>> (x1 % 32);
10+
ans[j] &= 0x80000000 >> (x2 % 32);
11+
return ans;
12+
}
13+
}

‎lcci/05.08.Draw Line/Solution.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def drawLine(self, length: int, w: int, x1: int, x2: int, y: int) -> List[int]:
3+
ans = [0] * length
4+
i = (y * w + x1) // 32
5+
j = (y * w + x2) // 32
6+
for k in range(i, j + 1):
7+
ans[k] = -1
8+
ans[i] = (ans[i] & 0xFFFFFFFF) >> (x1 % 32) if x1 % 32 else -1
9+
ans[j] &= -0x80000000 >> (x2 % 32)
10+
return ans

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /