GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode-3/python/041_First_Missing_Positive.py at master · vermuz/leetcode-3 Expand file tree/ Copy path041_First_Missing_Positive.pyMore file actions 21 lines (21 loc) · 788 Bytes / Copy path041_First_Missing_Positive.pyFile metadata and controls21 lines (21 loc) · 788 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution(object): def firstMissingPositive(self, nums): """ :type nums: List[int] :rtype: int """ # https://leetcode.com/discuss/86025/java-clean-o-n-solution-with-explanation ls = len(nums) index = 0 while index < ls: # nums[nums[index] - 1] == nums[index] means that the num is in right position if nums[index] <= 0 or nums[index] > ls or nums[nums[index] - 1] == nums[index]: index += 1 else: # swap current num to correct position pos = nums[index] - 1 nums[index], nums[pos] = nums[pos], nums[index] res = 0 while res < ls and nums[res] == res + 1: res += 1 return res + 1 |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |