审错题了
找到第一个只出现一次的字符
我以为是找第一个出现两次的字符。行吧
HashMap<Character, Integer[]> map = new HashMap<Character, Integer[]>();
Integer[] a = new Integer[2]; int就不行
import java.util.*; public class Solution { public int FirstNotRepeatingChar(String str){ char[] chars = str.toCharArray(); HashMap<Character, Integer[]> map = new HashMap<Character, Integer[]>(); for(int i =0; i<chars.length;i++){ if(!map.containsKey(chars[i])){ Integer[] a = new Integer[2]; a[0] = 1; a[1] = i; map.put(chars[i],a); } else{ Integer[] a = new Integer[2]; a[0] = map.get(chars[i])[0] + 1; a[1] = i; map.put(chars[i],a); } } for(int i =0; i<chars.length;i++){ if(map.get(chars[i])[0] == 1) {return map.get(chars[i])[1];} } return -1; } }