Cattle off network character stream is not a repeat of the first character

topic:

Please implement a function to find the character stream for the first time a character appears only. For example, when the character stream reads only the first two characters "go", the first character only occurs once a "g". When reading out the first six characters "google" from the character stream, first appears only one character is "l".
Output Description:
If the current character stream does not appear there is a character, returns the # character.

Problem solving:

import java.util.*;
public class Solution {
    HashMap<Character, Integer> map = new HashMap<Character, Integer>();
    The ArrayList <Character> List = new new the ArrayList <Character> (); // character stream The characters
     // the Insert One Stream String char from 
    public  void the Insert ( char CH)
    {
        if(map.containsKey(ch)){
            map.put(ch, map.get(ch) + 1);
        }else{
            map.put(ch, 1);
        }
        list.add(ch);//按顺序添加了
    }
    //return the first appearance once char in current string stream
    public char FirstAppearingOnce()
    {
        for(char key : list){
            if(map.get(key) == 1){
                return key;
            }
        }
        return '#';
    }
}

 

Guess you like

Origin www.cnblogs.com/yanhowever/p/12297605.html