Topic
Given a continuous stream of numbers, write a function that returns the ' the ' the ' the ' the ' the ' a unique number whenever terminating number is Reached (include terminating number). If there no unique number before terminating number or you can ' t find this terminating number, return-1. examples
Given a Stream [1, 2, 2, 1, 3, 4, 4, 5, 6] and a number 5
Return 3
Given a Stream [1, 2, 2, 1, 3, 4, 4, 5, 6] and a number 7
Return-1
Train of thought
1. Convert integer array to set AL, do not use Arrays.aslist (), because the data in the array is the base data type int, if directly to the collection, the element type in the collection is an array type int[], so you should create a collection yourself and then iterate through the array. Saves the value of an array to the collection. Then use the contains () method to determine whether there is a terminating number, if it does not exist, directly return-1, if present, jump to 2 steps;
2, using the set of indexof () to find the first occurrence of terminating number index, to determine whether the index is 0, if it is 0, then return-1, if not 0, then use al.sublist (0, index) to remove a subset of the list , the child set is the scope that needs to be searched;
3. Iterate through the values in the child collection and mark the values and corners in the HashMap collection, and if there are duplicate values, the angular value is the last occurrence of the label.
4. Re-traverse subset and list, for the element of the HashMap I, look at its corresponding value in the set, if it is the same as I, it means that it is not a repeating element, the value is returned, if it is not the same as I, it is necessary to change the corresponding value in the HashMap set to I, This will not return an error value when iterating over the values. If there is no return value at the end of the traversal, return-1. Code
public class Solution {* * * @param: A continuous stream of numbers * @param: a number * @return: RET Urns the ' the ' the ' the ' ' Unique number/public int firstuniquenumber (int[] nums, int number) {//Write your code
Here arraylist<integer> al = new Arraylist<integer> ();
for (int i = 0; i < nums.length i++) {al.add (nums[i]);
} if (Al.contains (number)) {int index = al.indexof (number);
if (index!= 0) {list<integer> List = al.sublist (0, index);
Hashmap<integer, integer> HM = new Hashmap<integer, integer> ();
for (int i = 0; i < list.size (); i++) {Hm.put (List.get (i), i); for (int i = 0; i < list.size (); i++) {if (i = = h M.get (List.get (i)) {return list.get (i);
else {hm.put (List.get (i), i);
}}} return-1; }
};