FirstUniqueCharacterInString

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.

s=""
return -1;

Note:

You may assume the string contain only lowercase letters.

 1 public class Solution{
 2     public int firstUniqChar(String s){
 3         if (s == null || s.equals("")) return -1;
 4         char[] c = s.toCharArray();
 5         int[] cnt = new int[256];
 6                 
 7         for (int i = 0; i < c.length; i++) {
 8             cnt[c[i]]++;
 9             System.out.println(cnt[c[i]]);
10         }
11 
12         for (int i = 0; i < c.length; i++) {
13             if (cnt[c[i]] == 1) return i; 
14         }
15         return -1;
16     }
17 }
View Code
原文地址:https://www.cnblogs.com/MazeHong/p/5804409.html