1. 程式人生 > 其它 >資料結構-陣列

資料結構-陣列

217、存在重複數字

給你一個整數陣列 nums 。如果任一值在陣列中出現至少兩次 ,返回 true ;如果陣列中每個元素互不相同,返回 false 。

輸入:nums = [1,2,3,1]
輸出:true

方法1:

bool containsDuplicate(int* nums, int numsSize){
    int i,j;
    int sign=0;
    for(i=0;i<numsSize;i++)
    {
        for(j=i+1;j<numsSize;j++)
            if(nums[i]==nums[j])
            {
                sign=1;
                break; 
            }
        if(sign==1)
            break;
    }
    if(sign==1)
        return true;
    else 
        return false;
}

超出時間限制了,時間複雜度 n^2

方法2:
c庫函式qsort()<stdlib.h>

void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
base:要排序的陣列第一個元素的指標
nitems: 陣列的元素的個數
size:一個元素的大小
compar:比較陣列中的兩個元素。

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

等於

int p=*(int*)a;
int q=*(int*)b;
return p-q;
int cmp(const void* _a, const void* _b) {
    int a = *(int*)_a, b = *(int*)_b;
    return a - b;
}

bool containsDuplicate(int* nums, int numsSize) {
    qsort(nums, numsSize, sizeof(int), cmp);
    for (int i = 0; i < numsSize - 1; i++) {
        if (nums[i] == nums[i + 1]) {
            return true;
        }
    }
    return false;
}

方法3:
雜湊表

#define HASHCAPACITY 769
int repeatnumber=0;

struct List{
    long int value;
    struct List *next;
};
typedef struct{
    struct List* data;
}Hash;

int hashFunc(int key){
    if(key<0)
        return -key%HASHCAPACITY;//you need care about "negative number"
    else
        return key%HASHCAPACITY;
}
void ListPush(struct List *head,int key){
    struct List* temp=(struct List*)malloc(sizeof(struct List));
    temp->value=key;
    temp->next=head->next;
    head->next=temp;
}
bool ListContains(struct List* head,int key){
    struct List* temp=head;
    for(;temp->next!=NULL;temp=temp->next)
    {
        if(temp->next->value==key)
            return true;
    }
    return false;
}

Hash* HashInitilize()
{
    Hash *h=(Hash*)malloc(sizeof(Hash));
    h->data=(struct List*)malloc(HASHCAPACITY *sizeof(struct List)); 
    for(int i=0;i<HASHCAPACITY;i++)
    {
        h->data[i].next=NULL;
        h->data[i].value=10000000001;
    }  
    return h;
}
bool HashPush(Hash* object,int key)
{
    int keyvalue=hashFunc(key);
    if(object!=NULL &&!ListContains(&(object->data[keyvalue]),key))
    {
         ListPush(&(object->data[keyvalue]),key);
         
         return 1;
    }
    if(object==NULL)
        printf("object id NULL\n");
    return 0;
   
}
void HashFree(Hash*object)
{
    if(object==NULL)
        return;
    for(int i=0;i<HASHCAPACITY;i++)
    {
        while(object->data[i].next)
        {
            struct List* temp=object->data[i].next;
            object->data[i].next=temp->next;
            free(temp);
        }
        free(object->data);
    }
}

bool containsDuplicate(int* nums, int numsSize){
    int i;
    int m=0;
    Hash* hashtable=HashInitilize();
   // hashtable=(Hash*)malloc(sizeof(Hash));   // !!!!cause memory leaks
    
    for(i=0;i<numsSize;i++)
    {
        if(HashPush(hashtable,nums[i]))
            m++;
    }
    printf("%d\n",m);
   
    if(m==numsSize)
        return false;
    else
        return true;

}

use after free:訪問堆上被釋放的記憶體
heap buffer overflow:堆上緩衝區訪問溢位
stack buffer overflow:棧上緩衝區訪問溢位
global buffer overflow:全域性緩衝區訪問溢位
use after return:訪問棧上已被釋放的記憶體