34、第一个只出现一次的字符

一、题目

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

二、解法

 1 import java.util.HashMap;
 2 public class Solution {
 3     HashMap<Character,Integer> hashmapValue = new HashMap<Character,Integer>();
 4      public int FirstNotRepeatingChar(String str) {
 5         if(str == null)
 6             return -1;
 7         int len = str.length();//计算字符串长度
 8         //计算每个字符的次数
 9         for(int i = 0; i < len; i++){
10             if(hashmapValue.containsKey(str.charAt(i))){
11                 int value = hashmapValue.get(str.charAt(i));
12                 hashmapValue.put(str.charAt(i), value+1);
13             }else{
14                 hashmapValue.put(str.charAt(i), 1);
15             }
16         }
17         for(int i = 0; i < len; i++){
18             if(hashmapValue.get(str.charAt(i)) == 1)
19                 return i;
20         }
21         return -1;
22      }
23 }
原文地址:https://www.cnblogs.com/fankongkong/p/7455547.html