剑指offer-第一个只出现一次的字符

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

ac代码:

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 public class Solution {
 4     public int FirstNotRepeatingChar(String str) {
 5         Map<Character,Integer>map=new HashMap<Character,Integer>();
 6             
 7             char x;
 8             int k=0;
 9             for(int i=0;i<str.length();i++){
10                 x=str.charAt(i);
11                 if(!map.containsKey(x)){
12                     map.put(x, 1);
13                 }else{
14                     map.put(x, map.get(x)+1);
15                 }
16             }
17             for(int i=0;i<str.length();i++){
18                 x=str.charAt(i);
19                 if(map.get(x)==1)
20                 {
21                     return i;
22                     
23                 }
24             }
25             return -1;
26     }
27 }
原文地址:https://www.cnblogs.com/llsq/p/8809565.html