在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).


import java.util.HashMap;
import java.util.Map;
public class Solution {
    public int FirstNotRepeatingChar(String str) {
        if(str==null||str.length()==0){
            return -1;
        }
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        char[] array = str.toCharArray();
        for(int i=0; i < str.length(); i++ ){
            if(!map.containsKey(array[i])){
                map.put(array[i],1);
            }else {
                map.put(array[i], map.get(array[i])+1);
            }
        }
        //System.out.println(map.toString());
        for(int i = 0; i<str.length();i++){
            if(map.get(array[i]) == 1) {
                return i;
            }
        }
        return -1;
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM