题解 | #缺失的第一个正整数#

缺失的第一个正整数

https://www.nowcoder.com/practice/50ec6a5b0e4e45348544348278cdcee5

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param nums int整型一维数组 
 * @param numsLen int nums数组长度
 * @return int整型
 */
#include <stdlib.h>

int  compar(const void* e1,const void* e2)
{
    return  *(int*)e1 - *(int*)e2;
}

int minNumberDisappeared(int* nums, int numsLen )
 {
    // write code here
    qsort(nums, numsLen, sizeof(nums[0]),  compar);//qsort快排函数,从小到大排列
    
      ///从1开始和数组中大于0的数字作比较
    int i=0, x=1;
    for(i=0;i<numsLen;++i)
    {
        if(nums[i]>0) //判断数组中的数字是否大于0,大于0才开始作比较      
        {
             if(x<nums[i])//从1开始与数字中第一个大于0的数比较
             {
                break;
             }
             else {
             {
                x++;
             }
             }
        }
   }
   return x;
 }
    

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务