您的位置:首页 > 产品设计 > UI/UE

Easy 387题 First Unique Character in a String

2016-11-19 08:05 323 查看
Question:

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.


Solution:

public class Solution {
public int firstUniqChar(String s) {
int[] freq= new int[26];
for(int i=0;i<=s.length()-1;i++){
freq[s.charAt(i)-'a']++;
}
for(int i=0;i<=s.length()-1;i++){
if(freq[s.charAt(i)-'a']==1)
return i;
}
return -1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: