剑指offer(牛客网)第一个只出现一次的字符

it2022-05-05  207

 

这个可以用 ASCII 码值来作为索引  O(n) 即可解决 public class Solution { public int FirstNotRepeatingChar(String str) { if(str == "" || str == null) { return -1; } // 58 = 122 -65 + 1 int[] indexs = new int [58]; for(int i = 0; i < str.length(); i++) { indexs[Integer.valueOf(str.charAt(i)) - 65]++; } for(int i = 0; i < str.length(); i++) { if(indexs[Integer.valueOf(str.charAt(i)) - 65] == 1) { return i; } } return -1; } }

 


最新回复(0)