码迷,mamicode.com
首页 > 其他好文 > 详细

first-unique-character-in-a-string

时间:2016-08-29 12:50:49      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/first-unique-character-in-a-string/

public class Solution {
    public int firstUniqChar(String s) {
        int[] index = new int[26];
        for (int i=0; i<s.length(); i++) {
            int ch = s.charAt(i) - ‘a‘;
            if (index[ch] == 0) {
                index[ch] = i + 1;
            }
            else if (index[ch] > 0) {
                index[ch] = -1;
            }
        }
        int ret = s.length() + 1;
        for (int i=0; i<26; i++) {
            if (index[i] > 0 && index[i] < ret) {
                ret = index[i];
            }
        }
        if (ret == s.length() + 1) {
            return -1;
        }
        else {
            return ret - 1;
        }
    }
}

 

first-unique-character-in-a-string

标签:

原文地址:http://www.cnblogs.com/charlesblc/p/5817503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!