Sword refers to "the first non-repeated character in the character stream" of the Offer series

Please implement a function to find the first character that appears only once in the character stream. For example, when the first two characters "go" are read only from the character stream, the first character that appears only once is "g". When reading the first six characters "google" from the character stream, the first character that appears only once is "l".

If there is no character that appears once in the current character stream, the # character is returned.

Idea :
Using only one array, not only can it be judged whether it appears multiple times in the array, but also when it appears once, the array stores the position of the character in the character stream for the first time, and the array index corresponds to the ASCII code of the character. Since the ASCII code has 128 characters, the size of the array is 128sufficient.
  Assign -1 to the initial value of the array. When a character appears for the first time, update -1 to the index (position) of the current character in the character stream. When it appears for the second time, the previously assigned index is overwritten, and there is no need to enter the subsequent judgment, here the assignment is selected as -2). 也就是说,没出现过,数组记录-1,出现一次,数组记录位置,出现多次,数组记录成-2.
  In the FirstAppearingOnce method, we have to traverse the array to find the correct solution, so we have to do two things:

  1. Find all the characters that appear once, because we are required to return the characters that appear for the first time;
  2. Compare the position of each character that appears once in the character stream, and find the character that appears first, that is, the character with the smallest position. Therefore, you also need to set a variable that records the position of a certain character. When you find the next character that appears once, compare the positions of the two characters that only appear once, and see who is higher.

In summary, this method only uses an array to record whether a character appears once, and the position in the character stream when it appears once.

import java.util.*;

public class Solution {
    
    
    
    private int index = 0;
    private static int[] arr = new int[128];
    static{
    
    
        Arrays.fill(arr,-1);
    }
    //Insert one char from stringStream
    public void Insert(char ch){
    
    
        if(arr[ch] == -1){
    
    
            arr[ch] = index;
        }else if(arr[ch]>=0){
    
    
            arr[ch] = -2;
        }
        index++;
        
    }
    //return the first appearance once char in current stringStream
    public char FirstAppearingOnce(){
    
    
        int minIndex = Integer.MAX_VALUE;
        char res = '#';
        for(int i=0; i<128; i++){
    
    
            if(arr[i]>=0 && arr[i] < minIndex){
    
    
                res = (char)i;
                minIndex = arr[i];
                
            }
        }
        return res;
    }
}

Time complexity: O(N)
Space complexity: O(N)

Guess you like

Origin blog.csdn.net/weixin_44471490/article/details/108957215