File tree 4 files changed +101
-4
lines changed
solution/1600-1699/1652.Defuse the Bomb
4 files changed +101
-4
lines changed Original file line number Diff line number Diff line change 60
60
61
61
<!-- 这里可写通用的实现逻辑 -->
62
62
63
+ 数组下标取模,累加求每一项即可。
64
+
63
65
<!-- tabs:start -->
64
66
65
67
### ** Python3**
66
68
67
69
<!-- 这里可写当前语言的特殊实现逻辑 -->
68
70
69
71
``` 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
71
86
```
72
87
73
88
### ** Java**
74
89
75
90
<!-- 这里可写当前语言的特殊实现逻辑 -->
76
91
77
92
``` 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
+ }
79
112
```
80
113
81
114
### ** ...**
Original file line number Diff line number Diff line change 60
60
### ** Python3**
61
61
62
62
``` 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
64
77
```
65
78
66
79
### ** Java**
67
80
68
81
``` 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
+ }
70
101
```
71
102
72
103
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments