Skip to content

Commit 04dac47

Browse files
committed
feat: add solutions to leetcode problem: No.1822. Sign of the Product of an Array
1 parent 6d5860d commit 04dac47

File tree

7 files changed

+104
-7
lines changed

7 files changed

+104
-7
lines changed

Diff for: lcof/面试题66. 构建乘积数组/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
class Solution:
3535
def constructArr(self, a: List[int]) -> List[int]:
3636
n = len(a)
37-
output = [1 for _ in a]
37+
output = [1] * n
3838
left = right = 1
3939
for i in range(n):
4040
output[i] = left

Diff for: lcof/面试题66. 构建乘积数组/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution:
22
def constructArr(self, a: List[int]) -> List[int]:
33
n = len(a)
4-
output = [1 for _ in a]
4+
output = [1] * n
55
left = right = 1
66
for i in range(n):
77
output[i] = left
88
left *= a[i]
99
for i in range(n - 1, -1, -1):
1010
output[i] *= right
1111
right *= a[i]
12-
return output
12+
return output

Diff for: solution/1800-1899/1822.Sign of the Product of an Array/README.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
<li><code>-100 <= nums[i] <= 100</code></li>
5454
</ul>
5555

56-
5756
## 解法
5857

5958
<!-- 这里可写通用的实现逻辑 -->
@@ -65,15 +64,49 @@
6564
<!-- 这里可写当前语言的特殊实现逻辑 -->
6665

6766
```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
6976
```
7077

7178
### **Java**
7279

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

7582
```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+
```
7694

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+
};
77110
```
78111

79112
### **...**

Diff for: solution/1800-1899/1822.Sign of the Product of an Array/README_EN.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,54 @@
4949
<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
5050
</ul>
5151

52-
5352
## Solutions
5453

5554
<!-- tabs:start -->
5655

5756
### **Python3**
5857

5958
```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
6168
```
6269

6370
### **Java**
6471

6572
```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+
```
6684

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+
};
67100
```
68101

69102
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

0 commit comments

Comments
 (0)