剑指Offer—34—第一个只出现一次的字符位置

来源:互联网 发布:三星手机 数据恢复 编辑:程序博客网 时间:2024/05/29 16:49

第一个只出现一次的字符位置 : 在一个字符串(1 <= 字符串长度 <= 10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置.

package A34第一个只出现一次的字符;public class Solution {    public int FirstNotRepeatingChar(String str) {        char[] a = str.toCharArray();        int[] count = new int[26];        int[] count2 = new int[26];        for (int i = 0; i < a.length; i++) {            if (a[i]-'a' >=0 && a[i]-'a' <= 25 ){                count[a[i]-'a']++;            }else if (a[i]-'A' >=0 && a[i]-'A' <= 25){                count2[a[i]-'A']++;            }        }        for (int i = 0; i < a.length; i++) {            if (a[i]-'a' >=0 && a[i]-'a' <= 25 && count[a[i]-'a'] == 1 ){                return i;            }else if (a[i]-'A' >=0 && a[i]-'A' <= 25 && count2[a[i]-'A'] == 1){                return i;            }        }        return -1;    }    public static void main(String[] args) {        Solution solution = new Solution();        //System.out.println(solution.FirstNotRepeatingChar("kYVmIJVzYWPQLtIdKMmvkVSoKtqJANOfCCOfLVJEjjhbnPrDOwKCDeqhts"));        System.out.println('z'-'a');    }}
阅读全文
0 0
原创粉丝点击