剑指offer——查找一个字符串中第一次出现一次的字符(哈希直接定址法)

来源:互联网 发布:深入浅出python豆瓣 编辑:程序博客网 时间:2024/05/19 03:26

哈希表——直接定址法的应用

哈希表(Hash table,也叫散列表),是根据关键码而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。

直接定址法——取关键字的某个线性函数为散列地址,Hash(key)=key或者Hash(key)=A*key+B,A和B是两个常数。

剑指offer面试题:查找一个字符串中第一次出现一次的字符


"test.cpp"

<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1#include <iostream>using namespace std;#include <assert.h>char FindFirstOneChar(char* str){assert(str);int tables[256] = {0};//Ascii表中有256个字符char* tmp = str;while (*tmp != '\0'){//不加unsigned char也可以tables[(unsigned char)*tmp]++;tmp++;//也可以一步到位//tables[(unsigned char)*tmp++]++;}tmp = str;while (*tmp != '\0'){//不加unsigned char也可以if (tables[(unsigned char)*tmp] == 1){return *tmp;} else{tmp++;}}}//查找一个字符串中第一个只出现一次的字符void Test(){char* str = "hhddadeechrhg";char ret = FindFirstOneChar(str);cout<<ret<<endl;}int main(){Test();system("pause");return 0;}</span></strong>


0 0