【笔试】47、第一个只出现一次的字符

/****************************************************************************************
 *题目:第一个只出现一次的字符
 *		在字符串中找出第一个只出现一次的字符。如输入“abaccdeff”,则输出’b’。
 *时间:2015年10月3日08:58:57
 *文件:FirstNotRepeatingChar.java
 *作者:cutter_point
 ****************************************************************************************/
package bishi.Offer50.y2015.m10.d03;

import org.junit.Test;

public class FirstNotRepeatingChar
{
	public char firstNotRepeat(String pString)
	{
		if(pString == null)
			return '\0';
		final int tableSize = 256;	//用ascii码来存放所有的字符
		int hashTable[] = new int[tableSize];
		for(int i = 0; i < tableSize; ++i)
		{
			//初始化数组
			hashTable[i] = 0;
		}//for
		int index = 0;
		while(index != pString.length())
		{
			//把对应的字符转化为ascii然后对应的下标数字++
			hashTable[pString.charAt(index++)]++;
		}//while
		
		//遍历hash表,返回第一个出现一次的数组
		index = 0;
		while(index != pString.length())
		{
			//注意,这里是对键值进行遍历,而不是从0开始到最后的值的遍历,也就是这里的循环只有pString.length()次
			if(hashTable[pString.charAt(index)] == 1)
			{
				return pString.charAt(index);
			}//if
			++index;
		}//while
		
		return ' ';
	}
	
	@Test
	public void test()
	{
		String s = "abaccdeff";
		FirstNotRepeatingChar f = new FirstNotRepeatingChar();
		System.out.println(f.firstNotRepeat(s));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值