1 import java.util.ArrayList; 2 3 /** 4 * 5 * @author gentleKay 6 * 题目描述 7 * 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中 8 * 找到第一个只出现一次的字符,并返回它的位置, 9 * 如果没有则返回 -1(需要区分大小写). 10 */ 11 12 public class Main34 { 13 14 public static void main(String[] args) { 15 // TODO Auto-generated method stub 16 String str = "googgle"; 17 int letter = Main34.FirstNotRepeatingChar(str); 18 System.out.println(letter); 19 } 20 21 public static int FirstNotRepeatingChar(String str) { 22 char[] ch = str.toCharArray(); 23 int[] num = new int[‘z‘+1]; 24 for (int i=0;i<str.length();i++) { 25 num[ch[i]]++; 26 } 27 for(int i=0;i<str.length();i++) { 28 if(num[ch[i]]==1) { 29 return i; 30 } 31 } 32 return -1; 33 } 34 }
原文:https://www.cnblogs.com/strive-19970713/p/11166287.html