LeetCode:Missing Number And First Missing Positive
1、
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
First Missing Positive
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
.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
思路:
1:求出1,2......n的數列之和total。
2:計算陣列所有元素的和sum。
3:total - sum的值,就是所求缺失的數。
程式碼:
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int total = n * (n + 1) / 2;
int sum = 0;
for(int i = 0; i < n; i ++)
{
sum += nums[i];
}
return total - sum;
}
};
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
.
思路:
雖然不能再另外開闢非常數級的額外空間,但是可以在輸入陣列上就地進行swap操作。
思路:交換陣列元素,使得陣列中第i位存放數值(i+1)。最後遍歷陣列,尋找第一個不符合此要求的元素,返回其下標。整個過程需要遍歷兩次陣列,複雜度為O(n)。
下圖以題目中給出的第二個例子為例,講解操作過程。
程式碼:
class Solution {
public:
void swap(vector<int>& nums, int a, int b)
{
int tmp = nums[a];
nums[a] = nums[b];
nums[b] = tmp;
}
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
int pos = 0;
while(pos < n)
{
int num = nums[pos];
if(num < n && num > 0 && num != nums[num - 1])
{
swap(nums, pos, num - 1);
}
else
pos ++;
}
for(int i = 0; i < n; i ++)
{
int num = nums[i];
if(num != i + 1)
return i + 1;
}
return n + 1;
}
};
相關文章
- Leetcode First Missing PositiveLeetCode
- Leetcode 41 First Missing PositiveLeetCode
- Leetcode-First Missing PositiveLeetCode
- First Missing Positive leetcode javaLeetCodeJava
- First Missing Positive【hard】
- [LeetCode Python3] 41. First Missing Positive 一步步優化LeetCodePython優化
- 41. First Missing Positive(找到陣列中未出現的最小正整數)陣列
- Leetcode-Missing RangesLeetCode
- 【Leetcode】163. Missing RangesLeetCode
- Number.POSITIVE_INFINITY
- [LeetCode] 2028. Find Missing ObservationsLeetCode
- Missing Subsequence Sum
- ntldr is missing怎麼解決? ntldr is missing怎麼回事?
- gerrit "missing Change-Id"
- your Android sdk is missingAndroid
- NILDR is Missing解決方案
- B. Missing Subsequence Sum
- PB協議報錯 it is missing required fields: (cannot determine missing fields for lite message)協議UI
- shell指令碼報錯:[: missing `]‘指令碼
- 【Python】 Missing parentheses in call to 'print'Python
- Laravel 8 路由模組新增 missing 方法Laravel路由
- ntldr is missing問題如何解決?
- GAMES001&mit missing semesterGAMMIT
- Dynamic Web Project option missing in Eclipse KeplerWebProjectEclipse
- Missing Microsoft.CompactFramework.CSharp.targetsROSFrameworkCSharp
- Missing com.sun.jdmk:jmxtools:jar:1.2.1JAR
- keil error:#8:missing closing quote 處理Error
- DQACP is missing Press Ctrl+Alt+Del to Restart(圖)REST
- [ARC186E] Missing Subsequence 題解
- Java報錯:Missing ServletWebServerFactory bean,如何解決JavaServletWebServerBean
- Your PHP installation appears to be missing the MySQL extension which is requirePHPAPPMySqlUI
- [CareerCup] 5.7 Find Missing Integer 查詢丟失的數
- GK659 Missing item assignment for 'Minority interest in the balance sheet'REST
- OGG-01028 Recovery record is missing ERRORError
- Oracle OCP IZ0-053(Recovery Missing Active Redo Log)Oracle
- SAP How to maintain missing enrties in TJ07 via customizingAI
- 【問題解決】property [elasticsearch.version] is missing for plugin [head]ElasticsearchPlugin
- 【Android】Permission denied (missing INTERNET permission?)異常踩坑Android