题解 | #缺失的第一个正整数#
缺失的第一个正整数
https://www.nowcoder.com/practice/50ec6a5b0e4e45348544348278cdcee5?tpId=295&tqId=2188893&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
C语言
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @return int整型 */ int minNumberDisappeared(int* nums, int numsLen ) { // write code here int* hash = (int*)calloc(500001, sizeof(int)); memset(hash, 0, 500001*sizeof(int)); for(int i=0; i<numsLen; i++){ if(nums[i] >= 0) //对于正整数,赋值hash对应表项 hash[nums[i]] = 1; } for(int i=1; i<500001; i++){ //从1开始遍历hash表,找出最小未出现的正整数 if(hash[i] == 0){ return i; } } return NULL; }