当前位置:网站首页>884. 两句话中的不常见单词-哈希表
884. 两句话中的不常见单词-哈希表
2022-07-26 19:46:00 【Mr Gao】
884. 两句话中的不常见单词
句子 是一串由空格分隔的单词。每个 单词 仅由小写字母组成。
如果某个单词在其中一个句子中恰好出现一次,在另一个句子中却 没有出现 ,那么这个单词就是 不常见的 。
给你两个 句子 s1 和 s2 ,返回所有 不常用单词 的列表。返回列表中单词可以按 任意顺序 组织。
示例 1:
输入:s1 = “this apple is sweet”, s2 = “this apple is sour”
输出:[“sweet”,“sour”]
示例 2:
输入:s1 = “apple apple”, s2 = “banana”
输出:[“banana”]
解题代码如下:
/** * Note: The returned array must be malloced, assume caller calls free(). */
#define sizeh 128
struct hash{
char *s;
int size;
int count;
struct hash *next;
};
void add_hash(struct hash *h,int size,char *s){
struct hash *p=(struct hash *)malloc(sizeof(struct hash));
p->s=(char *)malloc(sizeof(char)*(size+1));
p->count=1;
p->size=size;
// printf("%d ",p->size);
char ch=s[size];
s[size]='\0';
strcpy(p->s,s);
s[size]=ch;
//printf("%s %d %d ",p->s,p->count,p->size);
p->next=h->next;
h->next=p;
}
bool find_hash(struct hash *h,int size,char *s){
struct hash *p=h->next;
char ch=s[size];
// printf("fsa");
printf("%s|| ",s);
while(p){
// printf("%s ",p->s);
// printf("%d %d",p->size,size);
if(p->size=size){
s[size]='\0';
int r=strcmp(p->s,s);
s[size]=ch;
// printf("--%d ",r);
if(r==0){
p->count++;
return true;
}
}
p=p->next;
}
// printf("fsa");
return false;
}
char ** uncommonFromSentences(char * s1, char * s2, int* returnSize){
struct hash *h=(struct hash *)malloc(sizeof(struct hash)*sizeh);
int len=strlen(s1);
// printf("%d ",len);
char **re=(char **)malloc(sizeof(char *)*len);
int i;
// printf("len %d %d ",strlen(s1),strlen(s2));
for(i=0;i<sizeh;i++){
(h+i)->next=NULL;
}
for(i=0;s1[i]!='\0';i++){
if(s1[i]!=' '){
int size=i;
while(s1[size]!='\0'&&s1[size]!=' '){
size++;
}
if(find_hash(h+s1[i]%sizeh,size-i,s1+i)){
i=size;
if(s1[i]=='\0'){
break;
}
continue;
}
// printf("() %d %d ",i,size);
// printf("--%d ",size-i);
add_hash(h+s1[i]%sizeh,size-i,s1+i);
i=size;
// printf("%d %d ",i,size);
if(s1[i]=='\0'){
break;
}
// printf("%d %d ",i,size);
}
}
printf("dfas");
for(i=0;s2[i]!='\0';i++){
if(s2[i]!=' '){
int size=i;
while(s2[size]!='\0'&&s2[size]!=' '){
size++;
}
if(find_hash(h+s2[i]%sizeh,size-i,s2+i)){
i=size;
if(s2[i]=='\0'){
break;
}
continue;
}
add_hash(h+s2[i]%sizeh,size-i,s2+i);
i=size;
if(s2[i]=='\0'){
break;
}
// printf("%d %d ",i,size);
}
}
printf("df");
int sizere=0;
for(i=0;i<sizeh;i++){
struct hash *p=(h+i)->next;
while(p){
p->size=strlen(p->s);
p=p->next;
}
}
printf("df");
for(i=0;i<sizeh;i++){
struct hash *p=(h+i)->next;
while(p){
printf("%s %d %d ",p->s,p->count,p->size);
if(p->count==1){
re[sizere]=(char *)malloc(sizeof(char)*(p->size+100));
strcpy(re[sizere],p->s);
re[sizere][p->size]='\0';
// printf("--%s ",re[sizere]);
sizere++;
}
p=p->next;
}
}
*returnSize=sizere;
return re;
}
边栏推荐
- smoothscroll-polyfill插件的用法
- three. JS tag and pop-up the earth
- HM中如何获取CU块划分信息并用Matlab绘图
- Typescrip异步函数Promise使用
- How to wait for the return results of multiple asynchronous tasks synchronously?
- Week 6 Convolutional Neural Networks (CNNs)
- cv2.resize()
- Vs how to read data in MySQL (by the way, the problem of Chinese garbled code is solved through code)
- Dio problem summary
- The Sandbox 和艺术家 Alec Monopoly 达成合作
猜你喜欢

Solve attributeerror: module 'win32com.gen_ py. 00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu

I hope some suggestions on SQL optimization can help you who are tortured by SQL like me

一文读懂 Kubernetes的四种服务类型!

The first training course was a perfect success (๑ㅂ•) و*

C # use the default transformation method

App Uploader下载安装

Gartner发布最新《中国AI初创企业市场指南》,弘玑Cyclone再次被评为代表性企业

BUU刷题记-网鼎杯专栏2

three. JS tag and pop-up the earth

福音!微信个人公众号可以改名了!
随机推荐
第一次培训课完美成功(๑•ㅂ•)و*
Task 1 report
This point - super classic interview questions
Record an analysis of a.Net property management background service stuck
gospel! Wechat personal official account can be renamed!
解决AttributeError: module ‘win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x9‘ has no attribu
一维数组定义与使用
arpspoof 安装和使用
What are the key technologies of digital factory
打字比赛圆满结束!
Easycvr device management list page, paging data does not display problem repair
C # use the default transformation method
this指向,最简单的规则记住它
安全测试初学体验
cv2.resize()
Student's t distribution
如何优雅地赞美他人?不妨尝试下这几种方式
Typescrip异步函数Promise使用
使用百度飞桨 EasyDL 完成垃圾分类
Shell script basic programming commands