【leetcode】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.

思路:

又是一个不让排序,但是要得到跟排序有关信息的。想了半天,只能空间换时间,可是又只能用常量空间,这可难到我了。完全想不出来。放弃看答案。

发现,答案中其实是用了空间换时间的思想的,但是这里的空间直接就用了最开始的数组。

具体的思路是:从第一个数字开始,把正数换到按顺序排序的位置上。即1放到位置0,2放到位置1。直到当前位置的数字变成正确的数字。如果遇到小于0的数字或者大于元素个数的数字跳过,因为如果后面有对应位置的数字的话是会换过来的。

代码:

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        for(int i = 0; i < nums.size(); ++i)
        {
            while(nums[i] > 0 && nums[i] <= nums.size() && nums[nums[i] - 1] != nums[i]) //其实这里不要nums[i] > 0 也可以 就是会慢一点
                swap(nums[i], nums[nums[i] - 1]);  //交换数字 直到当前位置数字正确 或者 数字无法按序放在当前矩阵中
        }

        int i;
        for(i = 0; i < nums.size(); ++i)
        {
            if(nums[i] != i + 1)
                break;
        }
        return i + 1;
    }
};
时间: 2024-10-16 02:58:46

【leetcode】First Missing Positive(hard) ☆的相关文章

【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. 找到第一个没有出现的正整数 思路:

【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. [解析] 题意:给定一个数组,找出第一个缺失的正数,要求时间复杂度为O(n),空间复杂度为

【leetcode】First Missing Positive

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. 通过swap操作,把各个元素swap到相应的位置上,然后再

【LeetCode】First Missing Positive (2 solutions)

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. 解法一:O(nlogn) time and O(1) sp

【LeetCode】268. Missing Number

Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you i

【leetcode】1228.Missing Number In Arithmetic Progression

题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(object): def missingNumber(self, arr): """ :type arr: List[int] :rtype: int """ arr.sort() diff = (arr[-1] - arr[0])/(len(arr)) fo

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

【LeetCode】LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"