zl程序教程

您现在的位置是:首页 >  其它

当前栏目

leetcode387

2023-09-27 14:29:12 时间

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.
找出第一次出现的不重复的字符
public class Solution {
    public int firstUniqChar(String s) {
        //97-122
        int[] map = new int[123];
        int i=0;
        int length = s.length();
        for(i=0;i<length;i++)
        {
            map[s.charAt(i)]++;
        }
        for(i=0;i<length;i++)
        {
            if(map[s.charAt(i)] == 1)
                return i;
        }
        return -1;
    }
}

除了两次for循环,暂时没有想到N时间复杂度的解法。