#include#include#include#define MAXINT 0x7fffffff#define MININT 0X80000000//字符串中第一个只出现一次的字符char firstSingle(char *str){ int a[255]; memset(a, 0, 255 *...
分类:
编程语言 时间:
2015-08-01 09:58:43
阅读次数:
138
在一个字符串中找到第一个只出现一次的字符。
如输入”abaccdeff”,输出’b’解析:
使用一个数组,记录每个字符出现的次数,最后遍历计数数组,第一个个数为 1 的即为结果。
由于字符char,只有8 bit, 只有255种可能,因此只需声明一个255大小的数组。遍历一次字符串,遍历2次计数数组:时间复杂度O(n)
空间占用255*int = 512 Byte,是一个固定大小:空间复杂度...
分类:
其他好文 时间:
2015-07-28 23:06:50
阅读次数:
129
find the first unique character in a string and you can just traverse this string only one time. if there is no such character, just return '#' and '#' will not appear in the string, else return the ...
分类:
其他好文 时间:
2015-04-21 22:53:16
阅读次数:
167
题目:在一个字符串中找到第一个只出现一次的字符。比如"lavor_zl"第一个只出现一次的字符是'a'。
解题思路:
C/C++字符使用Ascii编码,一个字符占一个字节即可以表示2的8次方个数,那么C/C++字符可以表示的256个字符,因此可以用一个256的数组来保存各个字符出现的次数,当然256个字符的Ascii值是0-255之间的所有数,而且'\0'的Ascii值0,所以可以用数...
分类:
其他好文 时间:
2015-01-15 20:28:10
阅读次数:
130
/***只允许遍历一遍字符串*/publicclass找出字符串中第一个只出现一次的字符{ publicstaticvoidmain(String[]args){ //测试字符串 Stringstr="asdsacjj"; //字符串转化成字符 char[]strToChar=str.toCharArray(); intlen=strToChar.length;//字符串长度 //hashset用于判..
分类:
编程语言 时间:
2014-10-15 04:55:00
阅读次数:
258
可以稍微让代码写的好看,不用直接写双循环的话,就可以写成函数的调用,重用性也很高。
import java.util.Scanner;
public class findOnlyOnceChar {
public static boolean FindChar(String pInputString, char pChar){
int count=0;
for(int i=0;i<pI...
分类:
其他好文 时间:
2014-07-09 11:48:07
阅读次数:
397