<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 查找未排序數組中缺失的最小正數| 系列 1 > 原文: [https://www.geeksforgeeks.org/find-the-smallest-positive-number-missing-from-an-unsorted-array/](https://www.geeksforgeeks.org/find-the-smallest-positive-number-missing-from-an-unsorted-array/) 您會得到一個包含正負元素的未排序數組。 您必須使用恒定的額外空間在`O(n)`時間內找到數組中丟失的最小正數。 您可以修改原始數組。 例子 ``` Input: {2, 3, 7, 6, 8, -1, -10, 15} Output: 1 Input: { 2, 3, -7, 6, 8, 1, -10, 15 } Output: 4 Input: {1, 1, 0, -1, -2} Output: 2 ``` 解決此問題的**樸素的方法**是搜索給定數組中從 1 開始的所有正整數。 我們可能必須在給定數組中搜索最多`n + 1`個數字。 因此,在最壞的情況下,此解決方案需要`O(n ^ 2)`。 我們可以使用**排序**來解決,時間復雜度更低。 我們可以在`O(nLogn)`時間對數組進行排序。 數組排序后,我們要做的就是對數組進行線性掃描。 因此,此方法需要`O(nLogn + n)`時間,即`O(nLogn)`。 我們還可以**使用哈希**。 我們可以建立給定數組中所有正元素的哈希表。 一旦建立哈希表。 我們可以在哈希表中查找從 1 開始的所有正整數。一旦我們發現哈希表中不存在的數字,我們就將其返回。 這種方法平均可能需要`O(n)`時間,但需要`O(n)`額外空間。 **`O(n)`時間和`O(1)`額外空間解決方案**: 這個想法類似于這個帖子。 我們使用數組元素作為索引。 要標記元素`x`的存在,我們將索引`x`處的值更改為負。 但是,如果存在非正數(`-ve`和 0),則此方法無效。 因此,我們首先將正數與負數分開,然后應用該方法。 以下是兩步算法。 1)將正數與其他正數分開,即,將所有非正數移到左側。 在下面的代碼中,`segregate()`函數完成了這一部分。 2)現在我們可以忽略非正元素,而只考慮包含所有正元素的數組部分。 我們遍歷包含所有正數的數組,并標記元素`x`的存在,我們將索引`x`處的值符號更改為負數。 我們再次遍歷數組,然后打印具有正值的第一個索引。 在下面的代碼中,`findMissingPositive()`函數完成了這一部分。 請注意,在`findMissingPositive`中,由于 C 中的索引從 0 開始,所以我們從值中減去了 1。 ## C++ ```cpp /* C++ program to find the smallest positive missing number */ #include <bits/stdc++.h> using namespace std; /* Utility to swap to integers */ void swap(int* a, int* b) { ????int temp; ????temp = *a; ????*a = *b; ????*b = temp; } /* Utility function that puts all? non-positive (0 and negative) numbers on left? side of arr[] and return count of such numbers */ int segregate(int arr[], int size) { ????int j = 0, i; ????for (i = 0; i < size; i++) { ????????if (arr[i] <= 0) { ????????????swap(&arr[i], &arr[j]); ????????????j++; // increment count of non-positive integers ????????} ????} ????return j; } /* Find the smallest positive missing number? in an array that contains all positive integers */ int findMissingPositive(int arr[], int size) { ????int i; ????// Mark arr[i] as visited by making arr[arr[i] - 1] negative. ????// Note that 1 is subtracted because index start ????// from 0 and positive numbers start from 1 ????for (i = 0; i < size; i++) { ????????if (abs(arr[i]) - 1 < size && arr[abs(arr[i]) - 1] > 0) ????????????arr[abs(arr[i]) - 1] = -arr[abs(arr[i]) - 1]; ????} ????// Return the first index value at which is positive ????for (i = 0; i < size; i++) ????????if (arr[i] > 0) ????????????// 1 is added because indexes start from 0 ????????????return i + 1; ????return size + 1; } /* Find the smallest positive missing? number in an array that contains? both positive and negative integers */ int findMissing(int arr[], int size) { ????// First separate positive and negative numbers ????int shift = segregate(arr, size); ????// Shift the array and call findMissingPositive for ????// positive part ????return findMissingPositive(arr + shift, size - shift); } // Driver code int main() { ????int arr[] = { 0, 10, 2, -10, -20 }; ????int arr_size = sizeof(arr) / sizeof(arr[0]); ????int missing = findMissing(arr, arr_size); ????cout << "The smallest positive missing number is " << missing; ????return 0; } // This is code is contributed by rathbhupendra ``` ## C ``` /* C program to find the smallest positive missing number */ #include <stdio.h> #include <stdlib.h> /* Utility to swap to integers */ void swap(int* a, int* b) { ????int temp; ????temp = *a; ????*a = *b; ????*b = temp; } /* Utility function that puts all non-positive (0 and negative) numbers on left? side of arr[] and return count of such numbers */ int segregate(int arr[], int size) { ????int j = 0, i; ????for (i = 0; i < size; i++) { ????????if (arr[i] <= 0) { ????????????swap(&arr[i], &arr[j]); ????????????j++; // increment count of non-positive integers ????????} ????} ????return j; } /* Find the smallest positive missing number? in an array that contains all positive integers */ int findMissingPositive(int arr[], int size) { ????int i; ????// Mark arr[i] as visited by making arr[arr[i] - 1] negative. ????// Note that 1 is subtracted because index start ????// from 0 and positive numbers start from 1 ????for (i = 0; i < size; i++) { ????????if (abs(arr[i]) - 1 < size && arr[abs(arr[i]) - 1] > 0) ????????????arr[abs(arr[i]) - 1] = -arr[abs(arr[i]) - 1]; ????} ????// Return the first index value at which is positive ????for (i = 0; i < size; i++) ????????if (arr[i] > 0) ????????????// 1 is added because indexes start from 0 ????????????return i + 1; ????return size + 1; } /* Find the smallest positive missing? number in an array that contains both positive and negative integers */ int findMissing(int arr[], int size) { ????// First separate positive and negative numbers ????int shift = segregate(arr, size); ????// Shift the array and call findMissingPositive for ????// positive part ????return findMissingPositive(arr + shift, size - shift); } int main() { ????int arr[] = { 0, 10, 2, -10, -20 }; ????int arr_size = sizeof(arr) / sizeof(arr[0]); ????int missing = findMissing(arr, arr_size); ????printf("The smallest positive missing number is %d ", missing); ????getchar(); ????return 0; } ``` ## Java ```java // Java program to find the smallest // positive missing number import java.util.*; class Main { ????/* Utility function that puts all non-positive? ???????(0 and negative) numbers on left side of? ???????arr[] and return count of such numbers */ ????static int segregate(int arr[], int size) ????{ ????????int j = 0, i; ????????for (i = 0; i < size; i++) { ????????????if (arr[i] <= 0) { ????????????????int temp; ????????????????temp = arr[i]; ????????????????arr[i] = arr[j]; ????????????????arr[j] = temp; ????????????????// increment count of non-positive ????????????????// integers ????????????????j++; ????????????} ????????} ????????return j; ????} ????/* Find the smallest positive missing? ???????number in an array that contains ???????all positive integers */ ????static int findMissingPositive(int arr[], int size) ????{ ????????int i; ????????// Mark arr[i] as visited by making ????????// arr[arr[i] - 1] negative. Note that ????????// 1 is subtracted because index start ????????// from 0 and positive numbers start from 1 ????????for (i = 0; i < size; i++) { ????????????int x = Math.abs(arr[i]); ????????????if (x - 1 < size && arr[x - 1] > 0) ????????????????arr[x - 1] = -arr[x - 1]; ????????} ????????// Return the first index value at which ????????// is positive ????????for (i = 0; i < size; i++) ????????????if (arr[i] > 0) ????????????????return i + 1; // 1 is added becuase indexes ????????// start from 0 ????????return size + 1; ????} ????/* Find the smallest positive missing? ???????number in an array that contains ???????both positive and negative integers */ ????static int findMissing(int arr[], int size) ????{ ????????// First separate positive and ????????// negative numbers ????????int shift = segregate(arr, size); ????????int arr2[] = new int[size - shift]; ????????int j = 0; ????????for (int i = shift; i < size; i++) { ????????????arr2[j] = arr[i]; ????????????j++; ????????} ????????// Shift the array and call ????????// findMissingPositive for ????????// positive part ????????return findMissingPositive(arr2, j); ????} ????// main function ????public static void main(String[] args) ????{ ????????int arr[] = { 0, 10, 2, -10, -20 }; ????????int arr_size = arr.length; ????????int missing = findMissing(arr, arr_size); ????????System.out.println("The smallest positive missing number is " + missing); ????} } ``` ## Python ``` ''' Python program to find the? smallest positive missing number ''' ''' Utility function that puts all? non-positive (0 and negative) numbers on left? side of arr[] and return count of such numbers ''' def segregate(arr, size): ????j = 0 ????for i in range(size): ????????if (arr[i] <= 0): ????????????arr[i], arr[j] = arr[j], arr[i] ????????????j += 1 # increment count of non-positive integers? ????return j? ''' Find the smallest positive missing number? in an array that contains all positive integers ''' def findMissingPositive(arr, size): ????# Mark arr[i] as visited by making arr[arr[i] - 1] negative.? ????# Note that 1 is subtracted because index start? ????# from 0 and positive numbers start from 1? ????for i in range(size): ????????if (abs(arr[i]) - 1 < size and arr[abs(arr[i]) - 1] > 0): ????????????arr[abs(arr[i]) - 1] = -arr[abs(arr[i]) - 1] ????# Return the first index value at which is positive? ????for i in range(size): ????????if (arr[i] > 0): ????????????# 1 is added because indexes start from 0? ????????????return i + 1 ????return size + 1 ''' Find the smallest positive missing? number in an array that contains? both positive and negative integers ''' def findMissing(arr, size): ????# First separate positive and negative numbers? ????shift = segregate(arr, size) ????# Shift the array and call findMissingPositive for? ????# positive part? ????return findMissingPositive(arr[shift:], size - shift)? # Driver code? arr = [ 0, 10, 2, -10, -20 ] arr_size = len(arr)? missing = findMissing(arr, arr_size)? print("The smallest positive missing number is ", missing) # This code is contributed by Shubhamsingh10 ``` ## C# ```cs // C# program to find the smallest // positive missing number using System; class main { ????// Utility function that puts all ????// non-positive (0 and negative) ????// numbers on left side of arr[] ????// and return count of such numbers ????static int segregate(int[] arr, int size) ????{ ????????int j = 0, i; ????????for (i = 0; i < size; i++) { ????????????if (arr[i] <= 0) { ????????????????int temp; ????????????????temp = arr[i]; ????????????????arr[i] = arr[j]; ????????????????arr[j] = temp; ????????????????// increment count of non-positive ????????????????// integers ????????????????j++; ????????????} ????????} ????????return j; ????} ????// Find the smallest positive missing ????// number in an array that contains ????// all positive integers ????static int findMissingPositive(int[] arr, int size) ????{ ????????int i; ????????// Mark arr[i] as visited by making ????????// arr[arr[i] - 1] negative. Note that ????????// 1 is subtracted as index start from ????????// 0 and positive numbers start from 1 ????????for (i = 0; i < size; i++) { ????????????if (Math.Abs(arr[i]) - 1 < size && arr[ Math.Abs(arr[i]) - 1] > 0) ????????????????arr[ Math.Abs(arr[i]) - 1] = -arr[ Math.Abs(arr[i]) - 1]; ????????} ????????// Return the first index value at ????????// which is positive ????????for (i = 0; i < size; i++) ????????????if (arr[i] > 0) ????????????????return i + 1; ????????// 1 is added becuase indexes ????????// start from 0 ????????return size + 1; ????} ????// Find the smallest positive ????// missing number in array that ????// contains both positive and ????// negative integers ????static int findMissing(int[] arr, int size) ????{ ????????// First separate positive and ????????// negative numbers ????????int shift = segregate(arr, size); ????????int[] arr2 = new int[size - shift]; ????????int j = 0; ????????for (int i = shift; i < size; i++) { ????????????arr2[j] = arr[i]; ????????????j++; ????????} ????????// Shift the array and call ????????// findMissingPositive for ????????// positive part ????????return findMissingPositive(arr2, j); ????} ????// main function ????public static void Main() ????{ ????????int[] arr = { 0, 10, 2, -10, -20 }; ????????int arr_size = arr.Length; ????????int missing = findMissing(arr, arr_size); ????????Console.WriteLine("The smallest positive missing number is " + missing); ????} } // This code is contributed by Anant Agarwal. ``` **輸出**: ``` The smallest positive missing number is 1 ``` 請注意,此方法會修改原始數組。 我們可以更改分隔數組中元素的符號,以獲取相同的元素集。 但是我們仍然放開元素的順序。 如果我們想保持原始數組不變,則可以創建該數組的副本,然后在`temp`數組上運行此方法。 **另一種方法**:在此問題中,我們創建了一個全為 0 的列表,其大小為給定數組的`max()`值。 現在,只要我們在原始數組中遇到任何正值,就將列表的索引值更改為 1。因此,完成后,我們簡單地遍歷修改后的列表,即遇到的第一個 0,即(索引值`+1`)應該是我們的答案,因為 python 中的索引從 0 開始。 下面是上述方法的實現: ## C++ ``` // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the first missing positive // number from the given unsorted array int firstMissingPos(int A[], int n) { ????// To mark the occurrence of elements ????bool present[n + 1] = { false }; ????// Mark the occurrences ????for (int i = 0; i < n; i++) { ????????// Only mark the required elements ????????// All non-positive elements and ????????// the elements greater n + 1 will never ????????// be the answer ????????// For example, the array will be {1, 2, 3} ????????// in the worst case and the result ????????// will be 4 which is n + 1 ????????if (A[i] > 0 && A[i] <= n) ????????????present[A[i]] = true; ????} ????// Find the first element which didn't ????// appear in the original array ????for (int i = 1; i <= n; i++) ????????if (!present[i]) ????????????return i; ????// If the original array was of the ????// type {1, 2, 3} in its sorted form ????return n + 1; } // Driver code int main() { ????int A[] = { 0, 10, 2, -10, -20 }; ????int size = sizeof(A) / sizeof(A[0]); ????cout << firstMissingPos(A, size); } // This code is contributed by gp6 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看