第一个只出现一次的字符

来源:互联网 发布:网络搭建方案 编辑:程序博客网 时间:2024/06/06 02:24

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


import java.util.HashMap;public class Solution {    public int FirstNotRepeatingChar(String str) {        if(str == null || str.length() == 0)             return -1;         HashMap<Character, Integer> map = new HashMap<>();         for(int i = 0; i < str.length(); i++)         {             char c = str.charAt(i);             if(map.containsKey(c))             {                 int time = map.get(c);                 time++;                 map.put(c, time);             }             else                 map.put(c,1);         }         for(int i = 0; i < str.length(); i++)         {             char c = str.charAt(i);             if(map.get(c) == 1)                 return i;         }         return -1;    }}
0 0
原创粉丝点击