Skip to content

Commit 080e7b3

Browse files
committed
feat: add solutions to lc problem: No.1826
No.1826.Faulty Sensor
1 parent fb88c46 commit 080e7b3

File tree

9 files changed

+252
-62
lines changed

9 files changed

+252
-62
lines changed

solution/0000-0099/0020.Valid Parentheses/README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -138,28 +138,28 @@ private:
138138
```go
139139
func isValid(s string) bool {
140140
stack := newStack()
141-
for _, str := range s {
141+
for _, str := range s {
142142
if str == '(' || str == '[' || str == '{' {
143-
stack.push(byte(str))
144-
} else if str == ')' {
145-
if stack.pop() != (byte('(')) {
143+
stack.push(byte(str))
144+
} else if str == ')' {
145+
if stack.pop() != (byte('(')) {
146146
return false
147147
}
148-
} else if str == ']' {
149-
if stack.pop() != (byte('[')) {
148+
} else if str == ']' {
149+
if stack.pop() != (byte('[')) {
150150
return false
151151
}
152-
} else if str == '}' {
153-
if stack.pop() != (byte('{')) {
152+
} else if str == '}' {
153+
if stack.pop() != (byte('{')) {
154154
return false
155155
}
156-
}
156+
}
157157
}
158-
return stack.size() == 0
158+
return stack.size() == 0
159159
}
160160
161161
type Stack struct {
162-
data []byte
162+
data []byte
163163
index int
164164
}
165165
@@ -170,25 +170,25 @@ func newStack() *Stack {
170170
}
171171
172172
func (s *Stack) pop() byte {
173-
if s.index == 0 {
174-
return 0
175-
}
176-
s.index--
177-
r := s.data[s.index]
178-
return r
173+
if s.index == 0 {
174+
return 0
175+
}
176+
s.index--
177+
r := s.data[s.index]
178+
return r
179179
}
180180
181181
func (s *Stack) push(b byte) {
182-
if len(s.data) - 1 <= s.index {
182+
if len(s.data)-1 <= s.index {
183183
newData := make([]byte, len(s.data))
184-
s.data = append(s.data, newData[:]...)
184+
s.data = append(s.data, newData[:]...)
185185
}
186186
s.data[s.index] = b
187187
s.index++
188188
}
189189
190190
func (s *Stack) size() int {
191-
return s.index
191+
return s.index
192192
}
193193
```
194194

solution/0000-0099/0020.Valid Parentheses/README_EN.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,28 +123,28 @@ private:
123123
```go
124124
func isValid(s string) bool {
125125
stack := newStack()
126-
for _, str := range s {
126+
for _, str := range s {
127127
if str == '(' || str == '[' || str == '{' {
128-
stack.push(byte(str))
129-
} else if str == ')' {
130-
if stack.pop() != (byte('(')) {
128+
stack.push(byte(str))
129+
} else if str == ')' {
130+
if stack.pop() != (byte('(')) {
131131
return false
132132
}
133-
} else if str == ']' {
134-
if stack.pop() != (byte('[')) {
133+
} else if str == ']' {
134+
if stack.pop() != (byte('[')) {
135135
return false
136136
}
137-
} else if str == '}' {
138-
if stack.pop() != (byte('{')) {
137+
} else if str == '}' {
138+
if stack.pop() != (byte('{')) {
139139
return false
140140
}
141-
}
141+
}
142142
}
143-
return stack.size() == 0
143+
return stack.size() == 0
144144
}
145145
146146
type Stack struct {
147-
data []byte
147+
data []byte
148148
index int
149149
}
150150
@@ -155,25 +155,25 @@ func newStack() *Stack {
155155
}
156156
157157
func (s *Stack) pop() byte {
158-
if s.index == 0 {
159-
return 0
160-
}
161-
s.index--
162-
r := s.data[s.index]
163-
return r
158+
if s.index == 0 {
159+
return 0
160+
}
161+
s.index--
162+
r := s.data[s.index]
163+
return r
164164
}
165165
166166
func (s *Stack) push(b byte) {
167-
if len(s.data) - 1 <= s.index {
167+
if len(s.data)-1 <= s.index {
168168
newData := make([]byte, len(s.data))
169-
s.data = append(s.data, newData[:]...)
169+
s.data = append(s.data, newData[:]...)
170170
}
171171
s.data[s.index] = b
172172
s.index++
173173
}
174174
175175
func (s *Stack) size() int {
176-
return s.index
176+
return s.index
177177
}
178178
```
179179

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
func isValid(s string) bool {
22
stack := newStack()
3-
for _, str := range s {
3+
for _, str := range s {
44
if str == '(' || str == '[' || str == '{' {
5-
stack.push(byte(str))
6-
} else if str == ')' {
7-
if stack.pop() != (byte('(')) {
5+
stack.push(byte(str))
6+
} else if str == ')' {
7+
if stack.pop() != (byte('(')) {
88
return false
99
}
10-
} else if str == ']' {
11-
if stack.pop() != (byte('[')) {
10+
} else if str == ']' {
11+
if stack.pop() != (byte('[')) {
1212
return false
1313
}
14-
} else if str == '}' {
15-
if stack.pop() != (byte('{')) {
14+
} else if str == '}' {
15+
if stack.pop() != (byte('{')) {
1616
return false
1717
}
18-
}
18+
}
1919
}
20-
return stack.size() == 0
20+
return stack.size() == 0
2121
}
2222

2323
type Stack struct {
24-
data []byte
24+
data []byte
2525
index int
2626
}
2727

@@ -32,23 +32,23 @@ func newStack() *Stack {
3232
}
3333

3434
func (s *Stack) pop() byte {
35-
if s.index == 0 {
36-
return 0
37-
}
38-
s.index--
39-
r := s.data[s.index]
40-
return r
35+
if s.index == 0 {
36+
return 0
37+
}
38+
s.index--
39+
r := s.data[s.index]
40+
return r
4141
}
4242

4343
func (s *Stack) push(b byte) {
44-
if len(s.data) - 1 <= s.index {
44+
if len(s.data)-1 <= s.index {
4545
newData := make([]byte, len(s.data))
46-
s.data = append(s.data, newData[:]...)
46+
s.data = append(s.data, newData[:]...)
4747
}
4848
s.data[s.index] = b
4949
s.index++
5050
}
5151

5252
func (s *Stack) size() int {
53-
return s.index
53+
return s.index
5454
}

solution/1800-1899/1826.Faulty Sensor/README.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,81 @@
6767
<!-- 这里可写当前语言的特殊实现逻辑 -->
6868

6969
```python
70-
70+
class Solution:
71+
def badSensor(self, sensor1: List[int], sensor2: List[int]) -> int:
72+
i, n = 0, len(sensor1)
73+
while i < n - 1:
74+
if sensor1[i] != sensor2[i]:
75+
break
76+
i += 1
77+
while i < n - 1:
78+
if sensor1[i + 1] != sensor2[i]:
79+
return 1
80+
if sensor1[i] != sensor2[i + 1]:
81+
return 2
82+
i += 1
83+
return -1
7184
```
7285

7386
### **Java**
7487

7588
<!-- 这里可写当前语言的特殊实现逻辑 -->
7689

7790
```java
91+
class Solution {
92+
public int badSensor(int[] sensor1, int[] sensor2) {
93+
int i = 0;
94+
int n = sensor1.length;
95+
for (; i < n - 1 && sensor1[i] == sensor2[i]; ++i) {}
96+
for (; i < n - 1; ++i) {
97+
if (sensor1[i + 1] != sensor2[i]) {
98+
return 1;
99+
}
100+
if (sensor1[i] != sensor2[i + 1]) {
101+
return 2;
102+
}
103+
}
104+
return -1;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int badSensor(vector<int>& sensor1, vector<int>& sensor2) {
115+
int i = 0;
116+
int n = sensor1.size();
117+
for (; i < n - 1 && sensor1[i] == sensor2[i]; ++i) {}
118+
for (; i < n - 1; ++i)
119+
{
120+
if (sensor1[i + 1] != sensor2[i]) return 1;
121+
if (sensor1[i] != sensor2[i + 1]) return 2;
122+
}
123+
return -1;
124+
}
125+
};
126+
```
78127
128+
### **Go**
129+
130+
```go
131+
func badSensor(sensor1 []int, sensor2 []int) int {
132+
i, n := 0, len(sensor1)
133+
for ; i < n-1 && sensor1[i] == sensor2[i]; i++ {
134+
}
135+
for ; i < n-1; i++ {
136+
if sensor1[i+1] != sensor2[i] {
137+
return 1
138+
}
139+
if sensor1[i] != sensor2[i+1] {
140+
return 2
141+
}
142+
}
143+
return -1
144+
}
79145
```
80146

81147
### **...**

solution/1800-1899/1826.Faulty Sensor/README_EN.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,79 @@ The fourth data point from sensor 1 is dropped, and the last value of sensor 1 i
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def badSensor(self, sensor1: List[int], sensor2: List[int]) -> int:
63+
i, n = 0, len(sensor1)
64+
while i < n - 1:
65+
if sensor1[i] != sensor2[i]:
66+
break
67+
i += 1
68+
while i < n - 1:
69+
if sensor1[i + 1] != sensor2[i]:
70+
return 1
71+
if sensor1[i] != sensor2[i + 1]:
72+
return 2
73+
i += 1
74+
return -1
6275
```
6376

6477
### **Java**
6578

6679
```java
80+
class Solution {
81+
public int badSensor(int[] sensor1, int[] sensor2) {
82+
int i = 0;
83+
int n = sensor1.length;
84+
for (; i < n - 1 && sensor1[i] == sensor2[i]; ++i) {}
85+
for (; i < n - 1; ++i) {
86+
if (sensor1[i + 1] != sensor2[i]) {
87+
return 1;
88+
}
89+
if (sensor1[i] != sensor2[i + 1]) {
90+
return 2;
91+
}
92+
}
93+
return -1;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int badSensor(vector<int>& sensor1, vector<int>& sensor2) {
104+
int i = 0;
105+
int n = sensor1.size();
106+
for (; i < n - 1 && sensor1[i] == sensor2[i]; ++i) {}
107+
for (; i < n - 1; ++i)
108+
{
109+
if (sensor1[i + 1] != sensor2[i]) return 1;
110+
if (sensor1[i] != sensor2[i + 1]) return 2;
111+
}
112+
return -1;
113+
}
114+
};
115+
```
67116
117+
### **Go**
118+
119+
```go
120+
func badSensor(sensor1 []int, sensor2 []int) int {
121+
i, n := 0, len(sensor1)
122+
for ; i < n-1 && sensor1[i] == sensor2[i]; i++ {
123+
}
124+
for ; i < n-1; i++ {
125+
if sensor1[i+1] != sensor2[i] {
126+
return 1
127+
}
128+
if sensor1[i] != sensor2[i+1] {
129+
return 2
130+
}
131+
}
132+
return -1
133+
}
68134
```
69135

70136
### **...**

0 commit comments

Comments
 (0)