现在的位置: 首页 > 综合 > 正文

在一个字符串中找到第一个只出现一次的字符(JAVA实现)

2017年12月04日 ⁄ 综合 ⁄ 共 441字 ⁄ 字号 评论关闭
/**
	 * @author PLA  
	 * 在一个字符串中找到第一个只出现一次的字符
	 */
	public static void main(String[] args) {
		String s = "dtoghohronogddddew";
		int judge = find(s);
		if(judge!=-1)
			 System.out.println(s.charAt(judge));
		else
			System.out.println("There is no number adaptability!");
	}
	private static int find(String s) {
		// TODO Auto-generated method stub
		char[] ch = s.toCharArray();
		int[] count = new int[26];
		for(char c:ch){
			count[c-'a']++;
		}
		for(int i=0;i<count.length;i++){
			if(count[s.charAt(i)-'a']==1){
			return i;
			}
		}
		return -1;
	}

抱歉!评论已关闭.