此博客链接:
题目链接:
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
示例:
s = "leetcode"
返回 0
s = "loveleetcode"
返回 2
使用哈希表,找第一个value中只有一个数字的值
class Solution { public int firstUniqChar(String s) { Map<Character,Integer> map=new HashMap(); for(int i=0;i<s.length();i++){ char ch=s.charAt(i); map.put(ch,map.getOrDefault(ch,0)+1); } for(int i=0;i<s.length();i++){ if(map.get(s.charAt(i))==1) return i; } return -1; } }
原文:https://www.cnblogs.com/ping2yingshi/p/14692069.html