File tree 7 files changed +104
-7
lines changed
solution/1800-1899/1822.Sign of the Product of an Array
7 files changed +104
-7
lines changed Original file line number Diff line number Diff line change 34
34
class Solution :
35
35
def constructArr (self , a : List[int ]) -> List[int ]:
36
36
n = len (a)
37
- output = [1 for _ in a]
37
+ output = [1 ] * n
38
38
left = right = 1
39
39
for i in range (n):
40
40
output[i] = left
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def constructArr (self , a : List [int ]) -> List [int ]:
3
3
n = len (a )
4
- output = [1 for _ in a ]
4
+ output = [1 ] * n
5
5
left = right = 1
6
6
for i in range (n ):
7
7
output [i ] = left
8
8
left *= a [i ]
9
9
for i in range (n - 1 , - 1 , - 1 ):
10
10
output [i ] *= right
11
11
right *= a [i ]
12
- return output
12
+ return output
Original file line number Diff line number Diff line change 53
53
<li><code>-100 <= nums[i] <= 100</code></li>
54
54
</ul >
55
55
56
-
57
56
## 解法
58
57
59
58
<!-- 这里可写通用的实现逻辑 -->
65
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
65
67
66
``` python
68
-
67
+ class Solution :
68
+ def arraySign (self , nums : List[int ]) -> int :
69
+ res = 1
70
+ for num in nums:
71
+ if num == 0 :
72
+ return 0
73
+ if num < 0 :
74
+ res *= - 1
75
+ return res
69
76
```
70
77
71
78
### ** Java**
72
79
73
80
<!-- 这里可写当前语言的特殊实现逻辑 -->
74
81
75
82
``` java
83
+ class Solution {
84
+ public int arraySign (int [] nums ) {
85
+ int res = 1 ;
86
+ for (int num : nums) {
87
+ if (num == 0 ) return 0 ;
88
+ if (num < 0 ) res *= - 1 ;
89
+ }
90
+ return res;
91
+ }
92
+ }
93
+ ```
76
94
95
+ ### ** JavaScript**
96
+
97
+ ``` js
98
+ /**
99
+ * @param {number[]} nums
100
+ * @return {number}
101
+ */
102
+ var arraySign = function (nums ) {
103
+ let res = 1 ;
104
+ for (let num of nums) {
105
+ if (num == 0 ) return 0 ;
106
+ if (num < 0 ) res *= - 1 ;
107
+ }
108
+ return res;
109
+ };
77
110
```
78
111
79
112
### ** ...**
Original file line number Diff line number Diff line change 49
49
<li><code>-100 <= nums[i] <= 100</code></li>
50
50
</ul >
51
51
52
-
53
52
## Solutions
54
53
55
54
<!-- tabs:start -->
56
55
57
56
### ** Python3**
58
57
59
58
``` python
60
-
59
+ class Solution :
60
+ def arraySign (self , nums : List[int ]) -> int :
61
+ res = 1
62
+ for num in nums:
63
+ if num == 0 :
64
+ return 0
65
+ if num < 0 :
66
+ res *= - 1
67
+ return res
61
68
```
62
69
63
70
### ** Java**
64
71
65
72
``` java
73
+ class Solution {
74
+ public int arraySign (int [] nums ) {
75
+ int res = 1 ;
76
+ for (int num : nums) {
77
+ if (num == 0 ) return 0 ;
78
+ if (num < 0 ) res *= - 1 ;
79
+ }
80
+ return res;
81
+ }
82
+ }
83
+ ```
66
84
85
+ ### ** JavaScript**
86
+
87
+ ``` js
88
+ /**
89
+ * @param {number[]} nums
90
+ * @return {number}
91
+ */
92
+ var arraySign = function (nums ) {
93
+ let res = 1 ;
94
+ for (let num of nums) {
95
+ if (num == 0 ) return 0 ;
96
+ if (num < 0 ) res *= - 1 ;
97
+ }
98
+ return res;
99
+ };
67
100
```
68
101
69
102
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int arraySign (int [] nums ) {
3
+ int res = 1 ;
4
+ for (int num : nums ) {
5
+ if (num == 0 ) return 0 ;
6
+ if (num < 0 ) res *= -1 ;
7
+ }
8
+ return res ;
9
+ }
10
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ var arraySign = function ( nums ) {
6
+ let res = 1 ;
7
+ for ( let num of nums ) {
8
+ if ( num == 0 ) return 0 ;
9
+ if ( num < 0 ) res *= - 1 ;
10
+ }
11
+ return res ;
12
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def arraySign (self , nums : List [int ]) -> int :
3
+ res = 1
4
+ for num in nums :
5
+ if num == 0 :
6
+ return 0
7
+ if num < 0 :
8
+ res *= - 1
9
+ return res
You can’t perform that action at this time.
0 commit comments