Skip to content

Commit 826ad67

Browse files
committed
feat: add python and java solutions to leetcode problem: No.1652
1652. Defuse the Bomb
1 parent 66b1edc commit 826ad67

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

solution/1600-1699/1652.Defuse the Bomb/README.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,55 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
数组下标取模,累加求每一项即可。
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

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

6971
```python
70-
72+
class Solution:
73+
def decrypt(self, code: List[int], k: int) -> List[int]:
74+
n = len(code)
75+
res = [0] * n
76+
if k == 0:
77+
return res
78+
for i in range(n):
79+
if k > 0:
80+
for j in range(i + 1, i + k + 1):
81+
res[i] += code[j % n]
82+
else:
83+
for j in range(i + k, i):
84+
res[i] += code[(j + n) % n]
85+
return res
7186
```
7287

7388
### **Java**
7489

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

7792
```java
78-
93+
class Solution {
94+
public int[] decrypt(int[] code, int k) {
95+
int n = code.length;
96+
int[] res = new int[n];
97+
if (k == 0) return res;
98+
for (int i = 0; i < n; ++i) {
99+
if (k > 0) {
100+
for (int j = i + 1; j <= i + k; ++j) {
101+
res[i] += code[j % n];
102+
}
103+
} else {
104+
for (int j = i + k; j <= i - 1; ++j) {
105+
res[i] += code[(j + n) % n];
106+
}
107+
}
108+
}
109+
return res;
110+
}
111+
}
79112
```
80113

81114
### **...**

solution/1600-1699/1652.Defuse the Bomb/README_EN.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,44 @@
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def decrypt(self, code: List[int], k: int) -> List[int]:
65+
n = len(code)
66+
res = [0] * n
67+
if k == 0:
68+
return res
69+
for i in range(n):
70+
if k > 0:
71+
for j in range(i + 1, i + k + 1):
72+
res[i] += code[j % n]
73+
else:
74+
for j in range(i + k, i):
75+
res[i] += code[(j + n) % n]
76+
return res
6477
```
6578

6679
### **Java**
6780

6881
```java
69-
82+
class Solution {
83+
public int[] decrypt(int[] code, int k) {
84+
int n = code.length;
85+
int[] res = new int[n];
86+
if (k == 0) return res;
87+
for (int i = 0; i < n; ++i) {
88+
if (k > 0) {
89+
for (int j = i + 1; j <= i + k; ++j) {
90+
res[i] += code[j % n];
91+
}
92+
} else {
93+
for (int j = i + k; j <= i - 1; ++j) {
94+
res[i] += code[(j + n) % n];
95+
}
96+
}
97+
}
98+
return res;
99+
}
100+
}
70101
```
71102

72103
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int[] decrypt(int[] code, int k) {
3+
int n = code.length;
4+
int[] res = new int[n];
5+
if (k == 0) return res;
6+
for (int i = 0; i < n; ++i) {
7+
if (k > 0) {
8+
for (int j = i + 1; j <= i + k; ++j) {
9+
res[i] += code[j % n];
10+
}
11+
} else {
12+
for (int j = i + k; j <= i - 1; ++j) {
13+
res[i] += code[(j + n) % n];
14+
}
15+
}
16+
}
17+
return res;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def decrypt(self, code: List[int], k: int) -> List[int]:
3+
n = len(code)
4+
res = [0] * n
5+
if k == 0:
6+
return res
7+
for i in range(n):
8+
if k > 0:
9+
for j in range(i + 1, i + k + 1):
10+
res[i] += code[j % n]
11+
else:
12+
for j in range(i + k, i):
13+
res[i] += code[(j + n) % n]
14+
return res

0 commit comments

Comments
 (0)