The first distinct character in the character stream (Java implementation) __java

Source: Internet
Author: User

The title of the sword refers to the offer face question 55

New Ket Network test address: https://www.nowcoder.com/questionTerminal/00de97733b8e4f97a3fb5c680ee10720


The first character in a character stream that does not repeat the heat index: 26323 time limit: 1 seconds space limit: 32768K Please implement a function to find the first character in the character stream that appears only once. For example, when you read only the first two characters "go" from the character stream, the first character that appears only once is "G". When you read the first six characters, "Google," from the stream of characters, the first character that appears only once is "L".
Output Description:

Returns the # character if the current character stream does not exist with a single occurrence.
The Java code is as follows:

Package go.jacob.day531;

public class Demo3 {
	//character range (after turning int): -128~127
	int[] hash=new int[256];
	StringBuilder sb=new StringBuilder ();
	Insert one char from StringStream public
    void Insert (char ch) {
    	hash[ch+128]++;
    	Sb.append (CH);
    	
    }
  Return the ' the ' the ' appearence once char in ' stringstream public
    char firstappearingonce () {for
    	(char S:SB . toString (). ToCharArray ())
    		if (hash[s+128]==1) return
    			s;
    	Return ' # ';
    }
}

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.