We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 107732a commit 2e003abCopy full SHA for 2e003ab
animation-simulation/数组篇/leetcode41缺失的第一个正数.md
@@ -83,6 +83,8 @@ class Solution {
83
84
题目代码:
85
86
+Java Code:
87
+
88
```java
89
class Solution {
90
public int firstMissingPositive(int[] nums) {
@@ -115,3 +117,29 @@ class Solution {
115
117
}
116
118
```
119
120
+Python3 Code:
121
122
+```py
123
+class Solution:
124
+ def firstMissingPositive(self, nums: List[int]) -> int:
125
+ n = len(nums)
126
127
+ def swap(nums, a, b):
128
+ temp = nums[a]
129
+ nums[a] = nums[b]
130
+ nums[b] = temp
131
+ i = 0
132
+ while i < n:
133
+ num = nums[i]
134
+ # 已经就位
135
+ if num <= 0 or num >= n or num == i + 1 or nums[num - 1] == num:
136
+ i += 1
137
+ # 可以交换
138
+ else:
139
+ swap(nums, i, num - 1)
140
+ for i in range(n):
141
+ if nums[i] != i + 1:
142
+ return i + 1
143
+ return n + 1
144
+```
145
0 commit comments