How to write a clean C?


  • 0
    P

    I use qsort, but I don't know how to write it clean

    #include<stdio.h>
    
    int fcmp(int *a, int *b) {
        return *a > *b;
    }
    bool containsDuplicate(int* nums, int numsSize) {
        qsort((void *)nums, numsSize, 4, fcmp);
        int tmp = *nums,i = 1;
        while(i < numsSize)
            if(tmp == nums[i]) return true;
            else{
                tmp = nums[i];
                i++;
            }
        return false;
    }

  • 2
    2
    int fcmp(int *a, int *b) {
        return *a > *b;
    }
    bool containsDuplicate(int* nums, int numsSize) {
        qsort((void *)nums, numsSize, 4, fcmp);
        for (int i = 0; i < numSize - 1; i++) {
           if (nums[i] == nums[i+1]) 
           {
              return true;
           }
        }
        
        return false;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.