/**
*
*/
package interview35;
/**
* 第一次只出现一次的字符
* 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符,并返回它的位置
*@author: Administrator
*@date: 2017-1-9 下午07:34:07
*/
import java.util.Scanner;
public class Solution {
public int FirstNotRepeatingChar(String str) {
if(str==null||str==" ")return -1;
int count[]=new int[1000];
for(int i=0;i<str.length();i++){
count[str.charAt(i)]++;
}
for(int i=0;i<str.length();i++){
if(count[str.charAt(i)]==1)
return i;
}
return 0;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str=sc.next().toUpperCase();
sc.close();
Solution s=new Solution();
System.out.println(s.FirstNotRepeatingChar(str));
}
}
剑指offer——第一次只出现一次的字符
最新推荐文章于 2024-12-26 13:15:41 发布