雜湊表初識 查詢第一次只出現一次的字元

2021-06-23 04:30:45 字數 672 閱讀 1164

問題:在乙個只有大小寫字串中查詢第乙個只出現一次的字母

input: aacddcvghhgii

output: v

思路:使用hashtable 來使得時間複雜度為o(n) 

建立hashtable 

第一次接觸雜湊表。

#include#include#include#define max 100

using namespace std;

int hashfunction(char ch)

}void maphashtable(char str,int n,char hashtable)

} return -1;

}int main()

{ char str[max];

cout<<"請輸入字串:";

cin>>str;

int n=strlen(str);

char hashtable[52];

memset(hashtable,0,sizeof(hashtable)/sizeof(char));//初始化雜湊表

maphashtable(str,n,hashtable);

char ch=hashsearch(hashtable,n,str);

if(ch!=-1)

cout<<"第一次只出現一次的數為:"<

第一次只出現一次的字元

include include includechar firstnotrepeatingchar char pstring if pstring null return 0 const int tablesize 256 unsigned int hashtable tablesize for u...

第一次只出現一次的字元

在字串中找出第乙個只出現一次的字元。如輸 abaccdeff 則輸出 b include string include map include iostream using namespace std 法1 用map o nlogn char firstnotrepeatedchar0 char s...

第一次只出現一次的字元

題目劍指offer50 第一次只出現一次的字元 在乙個字串 0 字串長度 10000,全部由字母組成 中找到第乙個只出現一次的字元,並返回它的位置,如果沒有則返回 1 需要區分大小寫 總結 方法一 基於linkedhashmap的方法 由於題目與字元出現的次數有關,所以可以想到用乙個容器統計每個字元...