2215. Find the Difference of Two Arrays

2215. Find the Difference of Two Arrays

Question

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

  • answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
  • answer[1] is a list of all distinct integers in nums2 which are not present in nums1.

Note that the integers in the lists may be returned in any order.

Read more
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;
}
}
1491-Average-Salary-Excluding-the-Minimum-and-Maximum-Salary