力扣 存在重复元素 C语言实现

原题地址

https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/24/

题目描述

给定一个整数数组,判断是否存在重复元素。

如果任意一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。

示例 1:
输入: [1,2,3,1]
输出: true

示例 2:
输入: [1,2,3,4]
输出: false

示例 3:
输入: [1,1,1,3,3,4,3,2,4,2]
输出: true

1. 暴力解法

最简单的方法,直接对每个元素遍历整个数组,判断是否存在相同元素,代码如下:

/* Traverse the entire array for each element 
 * to find if there is duplicate. */
bool
containsDuplicate_v(int* nums, int numsSize){
    int i, j;
    
    if (numsSize <= 1)
        return false;
    
    for (i = 0; i < numsSize; i++)
    {
        for (j = i + 1; j < numsSize; j++)
        {
            if (nums[i] == nums[j])
                return true;
        }
    }
    return false;
}

时间复杂度计算方法如下:
对于一个下标为i的元素,需要向后查找n - i个元素判断其是否存在重复者,因此总的操作数量为: ( n − 1 ) + ( n − 2 ) + ( n − 3 ) + . . . + 2 + 1 = ∑ i = 1 n i = n ( n + 1 ) n (n - 1) + (n - 2) + (n - 3) + ... + 2 + 1 = \sum_{i = 1}^n{i} = \frac{n(n+1)}{n} (n1)+(n2)+(n3)+...+2+1=i=1ni=nn(n+1)
因此,时间复杂度为 O ( n 2 ) O(n^2) O(n2)

2. 排序解法

想到是整数数组,所以可以先将数组进行排序,那么如果存在相同的元素,一定是紧挨着的。
那么将数组先进行排序,在逐个判断当前元素是否与其后继元素是否相等即可。代码如下:

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

/* Sort the arrya first, and then determine for each element whethre its 
 * successor is the same */
bool
containsDuplicate_s(int *nums, int numsSize)
{
    int i;

    qsort(nums, numsSize, sizeof(int), cmp);

    for (i = 0; i < numsSize - 1; i++)
    {
        if (nums[i] == nums[i + 1])
            return true;
    }
    return false;
}

时间复杂度计算过程如下:
C语言中的qsort()函数采用的是快速排序,因此,其时间复杂度为 O ( n l o g n ) O(nlogn) O(nlogn)。而后一部分的遍历时间复杂度为 n n n。所以整个算法的时间复杂度为 O ( n l o g n ) O(nlogn) O(nlogn)

哈希表

判断元素是否重复,这很容易就联想到了哈希表。直接将数组遍历一遍,对于某个元素,如果哈希表中已经存在,则表示重复,返回true。如果不存在,标记为存在即可。
这里需要注意:

  1. 给的数组中可能有负数,所以要根据最大值和最小值一起来设置哈希表的大小。
  2. 要将数组中的数向哈希表中进行映射
    代码如下:
/* Use hash table to find duplicate value. */
bool
containsDuplicate_h(int *nums, int numsSize)
{
    int i, min, max;

    if (numsSize <= 1)
        return false;

    /* Min value and max value will decide the size of hash table. */
    min = max = nums[0];
    for (i = 1; i < numsSize; i++)
    {
        min = (nums[i] < min ? nums[i] : min);
        max = (nums[i] > max ? nums[i] : max);
    }

    /* Initial the hash table. */
    int table_size = max - min + 1;
    int hash_table[table_size];
    for (i = 0; i < max - min; i++)
        hash_table[i] = 0;

    /* Fill in the hash table and determin
     * if there are duplicate values. */
    int hash_val;
    for (i = 0; i < numsSize; i++)
    {
        hash_val = nums[i] - min;
        if (hash_table[hash_val] == 1)
            return true;
        hash_table[hash_val] = 1;
    }

    return false;

算法时间复杂度为一次遍历的时间, O ( n ) O(n) O(n)
算法空间复杂度为数组最大值与最小值的差值,无法确定。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
题目描述: 给出两个非空链表来表示两个非负整数。其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字。 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 您可以假设除了数字 0 之外,这两个数都不会以零开头。 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 解题思路: 题目中要求我们按照逆序的方式存储两个非负整数,也就是说链表的头节点表示该数的个位,第二个节点表示该数的十位,以此类推。 因此,我们可以从两个链表的头节点开始,对应位相加,并记录进位,将结果添加到新的链表中。如果有进位,需要在下一位相加时加上进位。 需要注意的是,当两个链表的长度不一致时,可以将较短的链表的缺失位看作是 0。 最后,如果最高位有进位,还需要在新链表的最高位添加一个值为 1 的节点。 C 语言代码实现: /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){ struct ListNode *head = NULL, *tail = NULL; int carry = 0; while (l1 || l2) { int n1 = l1 ? l1->val : 0; int n2 = l2 ? l2->val : 0; int sum = n1 + n2 + carry; if (!head) { head = tail = malloc(sizeof(struct ListNode)); tail->val = sum % 10; tail->next = NULL; } else { tail->next = malloc(sizeof(struct ListNode)); tail = tail->next; tail->val = sum % 10; tail->next = NULL; } carry = sum / 10; if (l1) l1 = l1->next; if (l2) l2 = l2->next; } if (carry > 0) { tail->next = malloc(sizeof(struct ListNode)); tail = tail->next; tail->val = carry; tail->next = NULL; } return head; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值