Lintcode--first unique number in Stream__java

Source: Internet
Author: User
Tags lintcode
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; }
};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.