[Leetcode]-Contains Duplicate HashTbale重解

原创 2015年07月07日 21:54:23

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Hide Tags: Array, Hash Table
题目:输入int型数组,检测其中是否有重复的元素,如果有重复的元素则反复true,否则返回false
解法一:暴力求解,两层for循环,1600ms

//暴力求解
bool containsDuplicate(int* nums, int numsSize) {
    int i = 0,j=0;
    for(i=0;i<numsSize;i++)
    {
        for(j=i+1;j<numsSize;j++)
            if(nums[i] == nums[j])
                return true;
    }
    return false;
}

解法二:hashTable解法16ms

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
//hashtable
typedef int ElementType;

//hashtable
typedef unsigned int index1;
typedef struct listnode* position;
typedef struct listnode* list;
struct listnode{
    ElementType data;
    position next;
};

typedef struct hashtbl*  hashtable;
struct hashtbl {
    int tablesize;
    list *thelists;
};

index1 HashFunc(const ElementType key,int tablesize)
{
    //unsigned int hashval = 0;
    //while(*key != '\0')   
        //hashval = (hashval << 5) + *key++;
    //return hashval % tablesize;

    int index = key % tablesize;
    return  index = index >= 0? index: index+tablesize;
}

hashtable InitializeHashTable(int tablesize)
{
    hashtable H;
    H = (hashtable)malloc(sizeof(hashtable));
    if(NULL == H) return NULL;

    H->tablesize = tablesize;

    H->thelists = (list*)malloc(sizeof(list) * H->tablesize);
    int i = 0;
    for(i=0;i<H->tablesize;i++)//有表头
    {
        H->thelists[i] = (list)malloc(sizeof(struct listnode));
        H->thelists[i]->next = NULL;
    }
    return H;
}
void DeleteHashTable(hashtable H)
{
    position P,tem;
    int i = 0;
    for(i=0;i<H->tablesize;i++)
    {
        P = H->thelists[i]->next;
        while(P != NULL)
        {
            tem = P;
            free(tem);
            P=P->next;
        }
    }
    free(H->thelists);
    free(H);
}
position Find(ElementType key,hashtable H)
{
    position P;
    list L;
    L = H->thelists[ HashFunc( key, H->tablesize) ];
    P = L->next;
    while(P != NULL && P->data != key)
        P = P->next;

    return P;
}

bool Insert(ElementType key,hashtable H)
{
    position pos,newnode;
    list L;
    pos = Find(key,H);
    if(pos == NULL)
    {
        newnode = (position)malloc(sizeof(position));
        L = H->thelists[ HashFunc( key, H->tablesize) ];
        newnode->data = key;
        newnode->next = L->next;
        L->next = newnode;
    }
    else
        return true;
}
void PrintHashTable(hashtable H)
{
    position P;
    int i = 0;
    for(i=0;i<H->tablesize;i++)
    {
        P = H->thelists[i]->next;
        printf("H->thelists[%d] = ",i);
        while(P != NULL)
        {
            printf(" %d ->",P->data);
            P=P->next;
        }
        printf("NULL\n");   
    }
}

bool containsDuplicate(int* nums, int numsSize) {
    hashtable H;
    int tablesize  = numsSize * 2;
    H = InitializeHashTable(tablesize);
    int i = 0;
    for(i=0; i< numsSize; i++){
        position pos,newnode;
        list L;
        pos = Find(nums[i],H);
        if(pos == NULL)
        {
            newnode = (position)malloc(sizeof(position));
            L = H->thelists[ HashFunc( nums[i], H->tablesize) ];
            newnode->data = nums[i];
            newnode->next = L->next;
            L->next = newnode;
        }
        else
            return true;
    }
    return false;
}

int main()
{
    int nums[5] = {1,0,34,56,57};
    bool r = containsDuplicate(nums,5);
    printf("containsDuplicate is : %d \n",r);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode-面试算法经典-Java实现】【217-Contains Duplicate(包含重复元素)】

【217-Contains Duplicate(包含重复元素)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/Wang-Jun-C...

leetcode--Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return t...

[LeetCode]217-Contains Duplicate 几种解法

数组重复元素查找: 解法一 :sort 后相邻位比较  40ms class Solution { public: bool containsDuplicate(vector& nums)...

【leetcode题解】【再看一遍】【86】【M】Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices i and j in the array suc...
  • sscssz
  • sscssz
  • 2015年12月17日 23:08
  • 178

《leetCode》:Contains Duplicate III

题目Given an array of integers, find out whether there are two distinct indices i and j in the array s...

LeetCode------Contains Duplicate

题目简介 Given an array of integers, find if the array contains any duplicates. Your function should r...

LeetCode-217. Contains Duplicate (Java)

Given an array of integers, find if the array contains any duplicates. Your function should return t...
  • NOadu
  • NOadu
  • 2017年06月27日 11:17
  • 131

leetcode_[python/C++逐步深入]_217. Contains Duplicate(深度分析位操作算法)

题目链接 【题目】 Given an array of integers, find if the array contains any duplicates. Your function sho...

leetcode之Contains Duplicate II 问题

问题描述: Given an array of integers and an integer k, find out whether there are two distinct indicesi...

leetcode 217. Contains Duplicate

leetcode 217. Contains Duplicate题目 Given an array of integers, find if the array contains any dupl...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Leetcode]-Contains Duplicate HashTbale重解
举报原因:
原因补充:

(最多只允许输入30个字)