题解 | #第一个只出现一次的字符#

第一个只出现一次的字符

http://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c

public class Solution {
    public int FirstNotRepeatingChar(String str) {
        class CharCount {
            public char ch;
            public int count;
            public int index;

            public CharCount() {
                count = 0;
            }
        }
        CharCount[] arr = new CharCount[64];
        int arr_len = 0;
        char tmp;
        boolean isFound;
        for (int i = 0; i < str.length(); i++) {
            tmp = str.charAt(i);
            isFound = false;
            for (int j = 0; j < arr_len; j++) {
                if (arr[j].ch == tmp) {
                    isFound = true;
                    arr[j].count++;
                }
            }
            if (!isFound) {
                CharCount ct = new CharCount();
                arr[arr_len] = ct;
                arr[arr_len].ch = tmp;
                arr[arr_len].count++;
                arr[arr_len].index = i;
                arr_len++;
            }
        }
        for (int i = 0; i < arr_len; i++) {
            if (arr[i].count == 1) {
                return arr[i].index;
            }
        }
        return -1;
    }
}
全部评论

相关推荐

船长想实习:我啥技术不会决定去试试,然后进去也不干活就搅局可以吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务