File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed
algorithms/python/firstMissingPositive Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -378,7 +378,7 @@ LeetCode
378378| 44| [ Wildcard Matching] ( https://leetcode.com/problems/wildcard-matching/ ) | [ C++] ( ./algorithms/cpp/wildcardMatching/wildcardMatching.cpp ) | Hard|
379379| 43| [ Multiply Strings] ( https://leetcode.com/problems/multiply-strings/ ) | [ C++] ( ./algorithms/cpp/multiplyStrings/multiplyStrings.cpp ) | Medium|
380380| 42| [ Trapping Rain Water] ( https://leetcode.com/problems/trapping-rain-water/ ) | [ C++] ( ./algorithms/cpp/trappingRainWater/trappingRainWater.cpp ) | Hard|
381- | 41| [ First Missing Positive] ( https://leetcode.com/problems/first-missing-positive/ ) | [ C++] ( ./algorithms/cpp/firstMissingPositive/firstMissingPositive.cpp ) | Hard|
381+ | 41| [ First Missing Positive] ( https://leetcode.com/problems/first-missing-positive/ ) | [ C++] ( ./algorithms/cpp/firstMissingPositive/firstMissingPositive.cpp ) , [ Python ] ( ././algorithms/python/firstMissingPositive/firstMissingPositive.py ) | Hard|
382382| 40| [ Combination Sum II] ( https://leetcode.com/problems/combination-sum-ii/ ) | [ C++] ( ./algorithms/cpp/combinationSum/combinationSum.II.cpp ) | Medium|
383383| 39| [ Combination Sum] ( https://leetcode.com/problems/combination-sum/ ) | [ C++] ( ./algorithms/cpp/combinationSum/combinationSum.cpp ) | Medium|
384384| 38| [ Count and Say] ( https://leetcode.com/problems/count-and-say/ ) | [ C++] ( ./algorithms/cpp/countAndSay/countAndSay.cpp ) , [ Java] ( ./algorithms/java/src/countAndSay/CountAndSay.java ) | Easy|
Original file line number Diff line number Diff line change 1+ # question : https://leetcode.com/problems/first-missing-positive/
2+ # Author : Samir Rajesh Prajapati
3+ # Date : 2020-10-01
4+
5+ # *********************************************************************************
6+ # *
7+ # * Given an unsorted integer array, find the first missing positive integer.
8+ # *
9+ # * For example,
10+ # * Given [1,2,0] return 3,
11+ # * and [3,4,-1,1] return 2.
12+ # *
13+ # * Your algorithm should run in O(n) time and uses constant space.
14+ # *
15+ # *
16+ # **********************************************************************************
17+
18+ class Solution :
19+ def firstMissingPositive (self , nums : [int ]) -> int :
20+ for i in range (len (nums )):
21+ while nums [i ]> 0 and nums [i ]< len (nums ) and nums [i ]- 1 != i and nums [i ]!= nums [nums [i ]- 1 ]:
22+ nums [nums [i ]- 1 ], nums [i ] = nums [i ], nums [nums [i ]- 1 ]
23+ for i in range (len (nums )):
24+ if i + 1 != nums [i ]:
25+ return i + 1
26+ return len (nums ) + 1
27+
28+ if __name__ == '__main__' :
29+ number = Solution ()
30+ print (number .firstMissingPositive ([3 ,4 ,- 1 ,1 ]))
You can’t perform that action at this time.
0 commit comments