1. 程式人生 > >6.6—排序—First Missing Positive

6.6—排序—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.

#include<iostream>
using namespace std;
void swap(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}
void BucketSort(int a[], int n)
{
	for (int i = 0; i < n;i++)
	{
		while (a[i] != i + 1)
		{
			if (a[i]<=0 || a[i]>n || a[i] == a[a[i] - 1])
				break;
			swap(a[i], a[a[i] - 1]);
		}
	}
}
int FirstMissingPositive(int a[], int n)
{
	BucketSort(a, n);
	for (int i = 0; i < n; i++)
		if (a[i] != i + 1)
			return i + 1;
	return n + 1;
}
int main()
{
	const int n = 6;
	int a[n] = { 2, 0, 6, 3, 4, 1 };
	BucketSort(a, n);
	
	int first = FirstMissingPositive(a, n);
	cout << first << endl;
}

相關推薦

6.6排序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

Leetcode 041 First Missing Positive(桶排序

解題思路:桶排序的變種,每次把碰到把nums[i] 和 nums[nums[i]] 互換,保證 nums[i] = nums[nums[i]]。當然nums[i]小於零,或者是大於陣列的可以忽略。最後遍歷一遍陣列,第一個不匹配的位置即為答案。class Solution {

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.

leetcode-First Missing Positive

href algorithm missing 排序 lan problem right ive 數組 https://leetcode.com/problems/first-missing-positive/#/description Given an unsorted

[leetcode-41-First Missing Positive]

should pla gpo miss discuss com leet algo solution Given an unsorted integer array, find the first missing positive integer. For example,

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

put 基本原理 理解 log 開始 blog ons i+1 right leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the f

41. First Missing Positive

self sin 常數 數組 const fin map from algorithm Given an unsorted integer array, find the first missing positive integer. For example, Give

leetcode41. First Missing Positive

int markdown python pri cnblogs 最小 正整數 leetcode first 給定一個數組,找出數組中不曾出現的最小正整數。 關鍵在於需要對原數組進行操作。 class Solution: def firstMissingPositiv

First Missing Positive

you pos missing body OS range amp num markdown 題目: Given an unsorted integer array, find the first missing positive integer. For example,

LeetCode 41. 缺失的第一個正數(First Missing Positive

style 第一個 交換 ret etc 沒有 解題思路 ssi != 題目描述 給定一個未排序的整數數組,找出其中沒有出現的最小的正整數。 示例 1: 輸入: [1,2,0] 輸出: 3 示例 2: 輸入: [3,4,-1,1] 輸出: 2 示例 3: 輸入: [

leetcode41 - First Missing Positive - hard

posit while pos ssi mis 個數 一個數 emp ger Given an unsorted integer array, find the smallest missing positive integer.Example 1:Input: [1,2,

[Swift]LeetCode41. 缺失的第一個正數 | 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]

LeetCode(41)-First Missing Positive

41-First Missing Positive Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3

41. first-missing-positive

      這道題呢技巧性比較強,對於像我這樣的新手第一次一般是想不出來這種方法的。       題目的大意是找出一個int陣列中第一個缺失的正整數,比如:0,1,3,5. 這組int陣列中,第一個缺失的正整數是2,當然最直觀的思路是我

LeetCode41. 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,-

Google面試題專題4 - leetcode41. First Missing Positive/25. Reverse Nodes in k-Group

41. First Missing Positive 題目描述 給定一無序整數陣列,找到丟失的最小正整數。 要求時間複雜度O(n),空間複雜度O(1)。 例子 Example 1: Input: [1,2,0] Output: 3

First Missing Positive解題報告

其實看到這種hard型別的題目,還是有點緊張的,但有驚無險,一次就過了。 題目描述 Given an unsorted integer array, find the smallest missing positive integer. 大意就是說,給你一堆毫

python leetcode 41. First Missing Positive

考察在陣列上的操作,nums[0]=1,nums[1]=2…按照這個順序在陣列上移動元素。然後在遍歷如果nums[index]!=index+1 那麼就找到了最小的丟失數。 這裡我們對陣列中數進行判斷,而不是下標,不然會死迴圈(例如[2,2,2,2]) class Solution:

LeetCode #41 First Missing Positive

(Week 2 演算法作業) 題目 分析 演算法1 演算法2 其他演算法 總結 題目 Given an unsorted integer array, find the smallest missing positive in

【leetcode】41.(Hard)First Missing Positive

題目連結 解題思路: 歸併排序——剔重——找到最小非負數——折半查詢 這道題就是需要考慮的邊界比較多 另外就是對重複數字的處理 提交程式碼: class Solution { public int firstMissingPositive(int[]