标签:int 记录 length cte color nta put style 没有
1 public int FirstNotRepeatingChar(String str) {//map my 2 LinkedHashMap<Character,Integer> map = new LinkedHashMap<>(); 3 for(int i=0;i<str.length();i++){ 4 char c = str.charAt(i); 5 if(map.containsKey(c)){ 6 map.put(c,map.get(c)+1); 7 } 8 else{ 9 map.put(c,1); 10 } 11 } 12 Character c = null; 13 Iterator iter =map.entrySet().iterator(); 14 while(iter.hasNext()){ 15 Map.Entry entry = (Map.Entry) iter.next(); 16 if(1 == (Integer)entry.getValue()){ 17 c = (Character) entry.getKey(); 18 break; 19 } 20 } 21 int re = -1; 22 if(c!=null){ 23 for(int i=0;i<str.length();i++){ 24 if(c ==str.charAt(i)){ 25 re = i; 26 break; 27 } 28 } 29 } 30 return re; 31 }
优化后
public int FirstNotRepeatingChar(String str) {//map mytip Map<Character,Integer> map = new HashMap<>(); for(int i=0;i<str.length();i++){ char c = str.charAt(i); if(map.containsKey(c)){ map.put(c,map.get(c)+1); } else{ map.put(c,1); } } int re = -1; for(int i=0;i<str.length();i++){ if(map.get(str.charAt(i))==1){ re = i; break; } } return re; }
标签:int 记录 length cte color nta put style 没有
原文地址:https://www.cnblogs.com/zhacai/p/10710018.html