Skip to content

Commit 86bf3e0

Browse files
solves sign of the product of an array
1 parent 6c4124b commit 86bf3e0

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@
442442
| 1805 | [Number of Different Integers in a String](https://leetcode.com/problems/number-of-different-integers-in-a-string) | [![Java](assets/java.png)](src/NumberOfDifferentIntegersInString.java) | |
443443
| 1812 | [Determine Color of a Chessboard Square](https://leetcode.com/problems/determine-color-of-a-chessboard-square) | [![Java](assets/java.png)](src/DetermineColorOfChessboardSquare.java) | |
444444
| 1816 | [Truncate Sentence](https://leetcode.com/problems/truncate-sentence) | [![Java](assets/java.png)](src/TruncateSentences.java) | |
445-
| 1822 | [Sign of the Product of an Array](https://leetcode.com/problems/sign-of-the-product-of-an-array) | | |
445+
| 1822 | [Sign of the Product of an Array](https://leetcode.com/problems/sign-of-the-product-of-an-array) | [![Java](assets/java.png)](src/SignOfTheProductOfAnArray.java) | |
446446
| 1826 | [Faulty Sensor](https://leetcode.com/problems/faulty-sensor) | | |
447447
| 1827 | [Minimum Operations to Make the Array Increasing](https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing) | | |
448448
| 1832 | [Check if the Sentence Is Pangram](https://leetcode.com/problems/check-if-the-sentence-is-pangram) | | |

src/SignOfTheProductOfAnArray.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class SignOfTheProductOfAnArray {
2+
public int arraySign(int[] nums) {
3+
int sign = 1;
4+
for (int element : nums) {
5+
sign *= signum(element);
6+
}
7+
return sign;
8+
}
9+
10+
private int signum(int x) {
11+
if (x == 0) return 0;
12+
return x > 0 ? 1 : -1;
13+
}
14+
}

0 commit comments

Comments
 (0)