685. 数据流中第一个唯一的数字
给一个连续的数据流,写一个函数返回终止数字到达时的第一个唯一数字(包括终止数字),如果找不到这个终止数字, 返回
-1
.
样例
样例1
输入:
[1, 2, 2, 1, 3, 4, 4, 5, 6]
5
输出: 3
样例2
输入:
[1, 2, 2, 1, 3, 4, 4, 5, 6]
7
输出: -1
样例3
输入:
[1, 2, 2, 1, 3, 4]
3
输出: 3
public class Solution {
/**
* @param nums: a continuous stream of numbers
* @param number: a number
* @return: returns the first unique number
*/
public int firstUniqueNumber(int[] nums, int number) {
HashMap<Integer, Boolean> map = new HashMap<>(nums.length);
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], !map.containsKey(nums[i]));
if (nums[i] == number) {
for (int j = 0; j <= i; j++) {
if (map.get(nums[j])) {
return nums[j];
}
}
break;
}
}
return -1;
}
}