20.12.23 leetcode 387

题目链接:https://leetcode-cn.com/problems/first-unique-character-in-a-string/

题意:返回一个字符串中最早出现的只出现过一次字符的索引。

分析:JAVA快乐局

class Solution {
    public int firstUniqChar(String s) {
        int[] book = new int[26];
        for(int i=0;i<s.length();i++){
            book[s.charAt(i)-'a']++;
        }
        for(int i=0;i<s.length();i++){
            if(book[s.charAt(i)-'a']==1)return i;
        }
        return -1;
    }
}

 

posted @ 2020-12-23 15:18  清酒令  阅读(36)  评论(0编辑  收藏  举报