41. First Missing Positive(C++)

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.

Solution:

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        for(int i=0; i<nums.size(); i++){
            if(i+1==nums[i]) continue;
            int x = nums[i];
            while(x>=1 && x<=nums.size() && x!=nums[x-1]){
                swap(x, nums[x-1]);
            }
        }
        for(int i=0; i<nums.size(); i++){
            if(i+1!=nums[i])    return i+1;
        }
        return nums.size()+1;
    }
};

  

时间: 2024-08-08 22:08:27

41. First Missing Positive(C++)的相关文章

41. First Missing Positive【leetcode】寻找第一个丢失的整数,java,算法

41. 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. 题目:寻找第一个丢失的整数意思为[1,2,4,5,

LeetCode - 41. First Missing Positive

41. First Missing Positive Problem's Link ---------------------------------------------------------------------------- Mean: 给你一组整数,找出第一个空缺的正整数. 要求:时间O(n),空间O(n). analyse: 这题时间O(n)想了半天没想到,用O(n*logn)过的. 然后看了discuss,想法非常巧妙,自愧不如. Time complexity: O(N) v

[array] leetcode - 41. First Missing Positive - Hard

leetcode - 41. First Missing Positive - Hard descrition 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 s

41. First Missing Positive(js)

41. First Missing Positive 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] Output: 1题意:给定一个无序数组,保持数组不重复找出最小的正整数代码如

刷题41. First Missing Positive

一.题目说明 题目是41. First Missing Positive,求一个未排序队列中缺失的最小正整数.时间复杂度要求是O(n).难度是Hard,确实难. 二.我的解答 不考虑时间复杂度,首先对队列进行排序,然后从第一个正数开始,如果不是1就返回1,否则继续查找2....找不到就返回,找到就继续. 代码如下: #include<iostream> #include<vector> #include<algorithm> #include<unordered_

[Leetcode][Python]41: First Missing Positive

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 41: First Missing Positivehttps://oj.leetcode.com/problems/first-missing-positive/ Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,a

41. 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. 解题思路:本题的核心思想就是将x放置到nums[x-1]的位置上去,然后从头开始遍历数组即可.但是把数交

41. First Missing Positive (Map)

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. 思路:n个数,可能的最大正整数是n,所以可以将这n个数作为哈希值.但是这样要有O(n)的额外空间. 解决

41. First Missing Positive *HARD*

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. int firstMissingPositive(vector<int>& nums) { i