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

 

posted @ 2016-08-24 20:03  MazeHong  阅读(151)  评论(0编辑  收藏  举报