在一个字符串中找到第一个只出现一次的字符
2013-04-30 23:08
337 查看
public class Test { public static void main(String args[]) { String str = "abccdab"; getFirstChar(str); } public static void getFirstChar(String string) { int[] bitMap = new int[26]; char[] ch = string.toCharArray(); for (int i = 0; i < ch.length; ++i) { bitMap[ch[i] - 'a']++; } int i = 0; while (i++ < ch.length) { if (bitMap[ch[i] - 'a'] == 1) { System.out.println(ch[i]); break; } } if (i >= ch.length) System.out.print("sorry, there is no need what u wanted."); } }
空间换时间:
/********************************************
* 在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b
*********************************************/
#include <stdio.h>
#include <string.h>
#define N 256
int charHash
= {0};
void initCharHash(const char *s)
{
while(*s)
{
charHash[*s]++;
s++;
}
}
char findFirstOnceChar(const char *s)
{
while(*s)
{
if(charHash[*s] == 1)
{
return *s;
}
s++;
}
return 0;
}
int main(void)
{
char *str = "abaccdeff";
initCharHash(str);
printf("%c\n",findFirstOnceChar(str));
return 0;
}
/*********
b
Process returned 0 (0x0) execution time : 1.254 s
Press any key to continue.
**********/
相关文章推荐
- 17 在一个字符串中找到第一个只出现一次的字符
- 17.在一个字符串中找到第一个只出现一次的字符
- HWOJ 在一个字符串中找到第一个只出现一次字符
- 每日一道算法题:在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
- 第十七题:在一个字符串中找到第一个只出现一次的字符。
- 在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
- 在一个字符串中找到第一个只出现一次的字符(java实现)
- 在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
- 在一个字符串中找到第一个仅仅出现一次的字符
- 在一个字符串中找到第一个只出现一次的字符
- 在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b
- 在一个字符串中找到第一个只出现一次的字符
- 【剑指Offer-时间效率平衡】在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置
- 在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b
- 在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
- 在一个字符串中找到第一个只出现一次的字符
- 在一个字符串中找到第一个只出现一次的字符
- 在一个字符串中找到第一个只出现一次的字符。如输入abaccdeff,则输出b。
- 在一个字符串中找到第一个只出现一次的字符
- 在一个字符串中找到第一个只出现一次的字符。