Loading

Leetcode387.字符串的第一个唯一字符

题目链接:387.字符串的第一个唯一字符

代码:

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

 

执行用时:6 ms, 在所有 Java 提交中击败了87.90%的用户
内存消耗:39 MB, 在所有 Java 提交中击败了59.61%的用户
posted @ 2020-12-23 21:31  yoyuLiu  阅读(52)  评论(0编辑  收藏  举报