2215. Find the Difference of Two Arrays

2215. Find the Difference of Two Arrays

Question

There is a function signFunc(x) that returns:

  • 1 if x is positive.
  • -1 if x is negative.
  • 0 if x is equal to 0.

You are given an integer array nums. Let product be the product of all values in the array nums.

Return signFunc(product).

Solution

遍历并检查当前元素的正负性,如果为负值则改变符号。

如果为0则返回0。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int arraySign(int[] nums) {
int res = 1;

for(int i = 0; i < nums.length; i++){
if(nums[i] < 0){
res = -res;
}
else if(nums[i] == 0){
return 0;
}
}
return res;
}
}
Author

Xander

Posted on

2023-05-02

Updated on

2023-05-05

Licensed under

Comments