剑指Offer-题50(二)(Java版):字符流中第一个只出现一次的字符

参考自:《剑指Offer——名企面试官精讲典型编程题》

题目:字符流中第一个只出现一次的字符
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符”go”时,第一个只出现一次的字符是’g’。当从该字符流中读出前六个字符”google”时,第一个只出现一次的字符是’l’。

主要思路:每从字符流中读入一个字符,若该字符没有重复,则记录该字符读入的位置;若该字符在之前已经被读入过,则忽略该字符。最后,从记录中找出最早出现的非重复字符。可以使用数组来记录字符读入位置。

关键点:记录字符读入的位置

时间复杂度:O(n)

public class FirstAppearingOnceChar
{
    private static int[] positionRecords; //字符读出位置记录
    private static int position;  //字符的读出位置

    static
    {
        positionRecords = new int[256];
        for (int i = 0; i < positionRecords.length; i++)
        {
            positionRecords[i] = -1;
        }
        position = 0;
    }

    public static void main(String[] args)
    {
        String str = "google";
        //String str = "hello word";
        test(str);
    }

    private static void test(String str)
    {
        for (int i = 0; i < str.length(); i++)
        {
            insert(str.charAt(i));
            System.out.println(firstAppearingOnceChar());
        }
    }

    //从字符流中读出字符
    private static void insert(char ch)
    {
        if (positionRecords[ch] == -1)
            positionRecords[ch] = position;  //记录读出位置
        else if (positionRecords[ch] >= 0)
            positionRecords[ch] = -2;  //该字符出现多次
        position++;
    }

    //字符流中第一个只出现一次的字符
    private static char firstAppearingOnceChar()
    {
        char result = '#';
        int minIndex = Integer.MAX_VALUE;  //非重复字符最早出现的位置
        for (int i = 0; i < positionRecords.length; i++)
        {
            if (positionRecords[i] >= 0 && positionRecords[i] < minIndex)
            {
                result = (char) i;
                minIndex = positionRecords[i];
            }
        }
        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37862405/article/details/80378040