classSolution {public:intfirstMissingPositive(intA[],intn) {if(n ==0)return1;//if A[i] is negative, i+1 exists in original A//partition, non-negative onlyintlow =0;inthigh = n-1;intend = n-1;while(low <=high) {while(low <= high && A[low] >0) low++;//to here,//case...
遍历数组,若找到了这个missingnum,就把它+1再继续找,遍历结束后返回missingnum即可。代码class Solution: def firstMissingPositive(self, nums: List[int]) -> int: nums.sort() missingnum = 1 for i in range(len(nums)): if nums[i] == missingnum: missingnum += 1 return missingnum分类: LeetC...
class Solution { public: int firstMissingPositive(vector<int>& nums) { bucket_sort(nums); for(int i = 0; i < nums.size(); ++i) { if(nums[i] != i + 1) { return i + 1; } } return nums.size() + 1; } private: void bucket_sort(vector<int>& nums) { for(int i = 0...
参考代码2: package leetcode_50; /*** * * @author pengfei_zheng * 找到第一个丢失的正数 */ public class Solution41 { public static int firstMissingPositive(int[] nums) { int i = 0; while(i < nums.length){ if(nums[i] == i+1 || nums[i] <= 0 || nums[i] > nums.length) ...
Given an unsorted integer array, find the smallest missing positive integer. 给定一个未排序的整数数组,找出其中缺少的最小的正整数。 Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1] Output: 2 Example 3: Input: [7,8,9,11,12] ...
Given an unsorted integer arraynums, return the smallest missing positive integer. You must implement an algorithm that runs inO(n)time and uses constant extra space. Example 1: Input: nums = [1,2,0] Output: 3 Example 2: Input: nums = [3,4,-1,1] ...
Leetcode: First Missing Positive Given an unsortedintegerarray,find the first missing positiveinteger.Forexample,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithmshouldruninO(n)timeanduses constant space. 1. 2. 3. 4. 5. 6....
缺失的第一个正数 题目描述: 给定一个未排序的整数数组,找出其中没有出现的最小的正整数。 示例1 示例2 说明: 你的算法的时间复杂度应为O(n),并且只能使用常数级别的空间...
LeetCode:First Missing Positive LeetCode 题目链接:First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space....
public int firstMissingPositive(int[] nums) { for (int i = 0; i < nums.length; ) { int n = nums[i]; if (n >= 1 && n <= nums.length + 1 && nums[n - 1] != n) {//这个数在答案区间 int tmp = nums[n - 1]; ...