Skip to content

Commit e36087b

Browse files
committed
feat: add solutions to lc problem: No.3842
1 parent be29173 commit e36087b

File tree

7 files changed

+207
-8
lines changed

7 files changed

+207
-8
lines changed

solution/3800-3899/3842.Toggle Light Bulbs/README.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,100 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3842.To
7878

7979
<!-- solution:start -->
8080

81-
### 方法一
81+
### 方法一:模拟
82+
83+
我们用一个长度为 $101$ 的数组 $\textit{st}$ 来记录每个灯泡的状态,初始时所有元素为 $0$,表示所有灯泡均为关闭状态。对于数组 $\textit{bulbs}$ 中的每一个元素 $\textit{bulbs}[i]$,我们将 $\textit{st}[\textit{bulbs}[i]]$ 的值取反(即 $0$ 变为 $1$,$1$ 变为 $0$)。最后,我们遍历 $\textit{st}$ 数组,将值为 $1$ 的索引加入结果列表中,并返回结果。
84+
85+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{bulbs}$ 的长度。空间复杂度 $O(M)$,其中 $M$ 是灯泡的最大编号。
8286

8387
<!-- tabs:start -->
8488

8589
#### Python3
8690

8791
```python
88-
92+
class Solution:
93+
def toggleLightBulbs(self, bulbs: list[int]) -> list[int]:
94+
st = [0] * 101
95+
for x in bulbs:
96+
st[x] ^= 1
97+
return [i for i, x in enumerate(st) if x]
8998
```
9099

91100
#### Java
92101

93102
```java
94-
103+
class Solution {
104+
public List<Integer> toggleLightBulbs(List<Integer> bulbs) {
105+
int[] st = new int[101];
106+
for (int x : bulbs) {
107+
st[x] ^= 1;
108+
}
109+
List<Integer> ans = new ArrayList<>();
110+
for (int i = 0; i < st.length; ++i) {
111+
if (st[i] == 1) {
112+
ans.add(i);
113+
}
114+
}
115+
return ans;
116+
}
117+
}
95118
```
96119

97120
#### C++
98121

99122
```cpp
100-
123+
class Solution {
124+
public:
125+
vector<int> toggleLightBulbs(vector<int>& bulbs) {
126+
vector<int> st(101, 0);
127+
for (int x : bulbs) {
128+
st[x] ^= 1;
129+
}
130+
vector<int> ans;
131+
for (int i = 0; i < 101; ++i) {
132+
if (st[i]) {
133+
ans.push_back(i);
134+
}
135+
}
136+
return ans;
137+
}
138+
};
101139
```
102140
103141
#### Go
104142
105143
```go
144+
func toggleLightBulbs(bulbs []int) []int {
145+
st := make([]int, 101)
146+
for _, x := range bulbs {
147+
st[x] ^= 1
148+
}
149+
ans := make([]int, 0)
150+
for i := 0; i < 101; i++ {
151+
if st[i] == 1 {
152+
ans = append(ans, i)
153+
}
154+
}
155+
return ans
156+
}
157+
```
106158

159+
#### TypeScript
160+
161+
```ts
162+
function toggleLightBulbs(bulbs: number[]): number[] {
163+
const st: number[] = new Array(101).fill(0);
164+
for (const x of bulbs) {
165+
st[x] ^= 1;
166+
}
167+
const ans: number[] = [];
168+
for (let i = 0; i < 101; i++) {
169+
if (st[i] === 1) {
170+
ans.push(i);
171+
}
172+
}
173+
return ans;
174+
}
107175
```
108176

109177
<!-- tabs:end -->

solution/3800-3899/3842.Toggle Light Bulbs/README_EN.md

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,100 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3842.To
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Simulation
80+
81+
We use an array $\textit{st}$ of length $101$ to record the state of each light bulb. Initially, all elements are $0$, indicating that all light bulbs are in the off state. For each element $\textit{bulbs}[i]$ in the array $\textit{bulbs}$, we toggle the value of $\textit{st}[\textit{bulbs}[i]]$ (i.e., $0$ becomes $1$, and $1$ becomes $0$). Finally, we traverse the $\textit{st}$ array, add the indices with a value of $1$ to the result list, and return the result.
82+
83+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{bulbs}$. The space complexity is $O(M)$, where $M$ is the maximum bulb number.
8084

8185
<!-- tabs:start -->
8286

8387
#### Python3
8488

8589
```python
86-
90+
class Solution:
91+
def toggleLightBulbs(self, bulbs: list[int]) -> list[int]:
92+
st = [0] * 101
93+
for x in bulbs:
94+
st[x] ^= 1
95+
return [i for i, x in enumerate(st) if x]
8796
```
8897

8998
#### Java
9099

91100
```java
92-
101+
class Solution {
102+
public List<Integer> toggleLightBulbs(List<Integer> bulbs) {
103+
int[] st = new int[101];
104+
for (int x : bulbs) {
105+
st[x] ^= 1;
106+
}
107+
List<Integer> ans = new ArrayList<>();
108+
for (int i = 0; i < st.length; ++i) {
109+
if (st[i] == 1) {
110+
ans.add(i);
111+
}
112+
}
113+
return ans;
114+
}
115+
}
93116
```
94117

95118
#### C++
96119

97120
```cpp
98-
121+
class Solution {
122+
public:
123+
vector<int> toggleLightBulbs(vector<int>& bulbs) {
124+
vector<int> st(101, 0);
125+
for (int x : bulbs) {
126+
st[x] ^= 1;
127+
}
128+
vector<int> ans;
129+
for (int i = 0; i < 101; ++i) {
130+
if (st[i]) {
131+
ans.push_back(i);
132+
}
133+
}
134+
return ans;
135+
}
136+
};
99137
```
100138
101139
#### Go
102140
103141
```go
142+
func toggleLightBulbs(bulbs []int) []int {
143+
st := make([]int, 101)
144+
for _, x := range bulbs {
145+
st[x] ^= 1
146+
}
147+
ans := make([]int, 0)
148+
for i := 0; i < 101; i++ {
149+
if st[i] == 1 {
150+
ans = append(ans, i)
151+
}
152+
}
153+
return ans
154+
}
155+
```
104156

157+
#### TypeScript
158+
159+
```ts
160+
function toggleLightBulbs(bulbs: number[]): number[] {
161+
const st: number[] = new Array(101).fill(0);
162+
for (const x of bulbs) {
163+
st[x] ^= 1;
164+
}
165+
const ans: number[] = [];
166+
for (let i = 0; i < 101; i++) {
167+
if (st[i] === 1) {
168+
ans.push(i);
169+
}
170+
}
171+
return ans;
172+
}
105173
```
106174

107175
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
vector<int> toggleLightBulbs(vector<int>& bulbs) {
4+
vector<int> st(101, 0);
5+
for (int x : bulbs) {
6+
st[x] ^= 1;
7+
}
8+
vector<int> ans;
9+
for (int i = 0; i < 101; ++i) {
10+
if (st[i]) {
11+
ans.push_back(i);
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func toggleLightBulbs(bulbs []int) []int {
2+
st := make([]int, 101)
3+
for _, x := range bulbs {
4+
st[x] ^= 1
5+
}
6+
ans := make([]int, 0)
7+
for i := 0; i < 101; i++ {
8+
if st[i] == 1 {
9+
ans = append(ans, i)
10+
}
11+
}
12+
return ans
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public List<Integer> toggleLightBulbs(List<Integer> bulbs) {
3+
int[] st = new int[101];
4+
for (int x : bulbs) {
5+
st[x] ^= 1;
6+
}
7+
List<Integer> ans = new ArrayList<>();
8+
for (int i = 0; i < st.length; ++i) {
9+
if (st[i] == 1) {
10+
ans.add(i);
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def toggleLightBulbs(self, bulbs: list[int]) -> list[int]:
3+
st = [0] * 101
4+
for x in bulbs:
5+
st[x] ^= 1
6+
return [i for i, x in enumerate(st) if x]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function toggleLightBulbs(bulbs: number[]): number[] {
2+
const st: number[] = new Array(101).fill(0);
3+
for (const x of bulbs) {
4+
st[x] ^= 1;
5+
}
6+
const ans: number[] = [];
7+
for (let i = 0; i < 101; i++) {
8+
if (st[i] === 1) {
9+
ans.push(i);
10+
}
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)