一个数组里,数都是两两出现的,但是有三个数是唯一出现的,找出这三个数。

这个题我用的是trie索引树,不知道对不对,但是我写完程序运行后结果是对的,先贴上程序,一起探讨:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define branch_num 10
typedef struct trie_node
{
	int count;
    struct trie_node *child[branch_num];
}TrieNode,*TrieTree;
TrieNode *trie_create_node()
{
	TrieNode *temp=(TrieNode*)malloc(sizeof(TrieNode));
	temp->count=0;
	memset(temp->child,0,sizeof(temp->child));
	return temp;
}
void trie_insert_node(TrieTree t,int n)
{
	TrieNode *location=t;
	char *p=(char*)malloc(sizeof(char)*10);
	sprintf(p,"%d",n);
	while(*p!='\0')
	{
		if(location->child[*p-'0']==NULL)
		location->child[*p-'0']=trie_create_node();
		location=location->child[*p-'0'];
		p++;
	}
	location->count+=1;
}
int trie_count_word(TrieTree t,int n)
{
	TrieNode *location=t;
	char *p=(char*)malloc(sizeof(char)*10);
	sprintf(p,"%d",n);
	while(*p!='\0'&&location!=NULL)
	{
		location=location->child[*p-'0'];
		p++;
	}
	return location->count;
}
void main()
{
	int num[]={23,43,45,67,12,98,23,43,45,67,12,98,2,5,6};
	TrieTree t=trie_create_node();
	int len=sizeof(num)/sizeof(int);
	int i;
	for(i=0;i<len;i++)
		trie_insert_node(t,num[i]);
	for(i=0;i<len;i++)
		if(trie_count_word(t,num[i])==1)
			printf("%d\t",num[i]);
	printf("\n");
}


你可能感兴趣的:(struct,null,insert,branch)