classSolution { public: intfirstMissingPositive(vector<int>& nums){ int n = nums.size(); for (int i = 0; i < n; ++i) { while (1 <= nums[i] && nums[i] <= n && nums[nums[i] - 1] != nums[i]) { swap(nums[nums[i] - 1], nums[i]); } } for (int i = 0; i < n; ++i) { if (nums[i] != i + 1) { return i + 1; } } return n + 1; } };
publicclassSolution { publicintfirstMissingPositive(int[] nums) { intn= nums.length; for (inti=0; i < n; ++i) { while (1 <= nums[i] && nums[i] <= n && nums[nums[i] - 1] != nums[i]) { swap(nums, nums[i] - 1, i); } } for (inti=0; i < n; ++i) { if (nums[i] != i + 1) { return i + 1; } } return n + 1; } privatevoidswap(int[] nums, int i, int j) { inttemp= nums[i]; nums[i] = nums[j]; nums[j] = temp; } }
结果
执行用时 : 1 ms, 击败 98.51% 使用 Java 的用户
内存消耗 : 53.84 MB, 击败 68.75% 使用 Java 的用户
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution(object): deffirstMissingPositive(self, nums): """ :type nums: List[int] :rtype: int """ n = len(nums) for i inrange(n): while1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]: nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1] for i inrange(n): if nums[i] != i + 1: return i + 1 return n + 1
结果
执行用时 : 69 ms, 击败 72.86% 使用 Python 的用户
内存消耗 : 19.07 MB, 击败 90.86% 使用 Python 的用户
Python3
1 2 3 4 5 6 7 8 9 10
classSolution: deffirstMissingPositive(self, nums: List[int]) -> int: n = len(nums) for i inrange(n): while1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]: nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1] for i inrange(n): if nums[i] != i + 1: return i + 1 return n + 1
结果
执行用时 : 74 ms, 击败 % 使用 Python3 的用户
内存消耗 : 27.45 MB, 击败 % 使用 Python3 的用户
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
voidswap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } intfirstMissingPositive(int* nums, int numsSize) { for (int i = 0; i < numsSize; ++i) { while (1 <= nums[i] && nums[i] <= numsSize && nums[nums[i] - 1] != nums[i]) { swap(&nums[nums[i] - 1], &nums[i]); } } for (int i = 0; i < numsSize; ++i) { if (nums[i] != i + 1) { return i + 1; } } return numsSize + 1; }
publicclassSolution { publicintFirstMissingPositive(int[] nums) { int n = nums.Length; for (int i = 0; i < n; ++i) { while (1 <= nums[i] && nums[i] <= n && nums[nums[i] - 1] != nums[i]) { Swap(nums, nums[i] - 1, i); } } for (int i = 0; i < n; ++i) { if (nums[i] != i + 1) { return i + 1; } } return n + 1; } privatevoidSwap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } }
结果
执行用时 : 142 ms, 击败 66.90% 使用 C# 的用户
内存消耗 : 57.28 MB, 击败 27.47% 使用 C# 的用户
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** * @param {number[]} nums * @return {number} */ var firstMissingPositive = function(nums) { const n = nums.length; for (let i = 0; i < n; ++i) { while (1 <= nums[i] && nums[i] <= n && nums[nums[i] - 1] !== nums[i]) { [nums[nums[i] - 1], nums[i]] = [nums[i], nums[nums[i] - 1]]; } } for (let i = 0; i < n; ++i) { if (nums[i] !== i + 1) { return i + 1; } } return n + 1; };
结果
执行用时 : 74 ms, 击败 70.49% 使用 JavaScript 的用户
内存消耗 : 55.44 MB, 击败 36.87% 使用 JavaScript 的用户
TypeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
functionfirstMissingPositive(nums: number[]): number { const n = nums.length; for (let i = 0; i < n; ++i) { while (1 <= nums[i] && nums[i] <= n && nums[nums[i] - 1] !== nums[i]) { [nums[nums[i] - 1], nums[i]] = [nums[i], nums[nums[i] - 1]]; } } for (let i = 0; i < n; ++i) { if (nums[i] !== i + 1) { return i + 1; } } return n + 1; }
classSolution { funcfirstMissingPositive(_nums: [Int]) -> Int { var nums = nums let n = nums.count for i in0..<n { while1<= nums[i] && nums[i] <= n && nums[nums[i] -1] != nums[i] { nums.swapAt(nums[i] -1, i) } } for i in0..<n { if nums[i] != i +1 { return i +1 } } return n +1 } }
结果
执行用时 : 151 ms, 击败 100.00% 使用 Swift 的用户
内存消耗 : 19.63 MB, 击败 14.14% 使用 Swift 的用户
Kotlin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { funfirstMissingPositive(nums: IntArray): Int { val n = nums.size for (i in0 until n) { while (1 <= nums[i] && nums[i] <= n && nums[nums[i] - 1] != nums[i]) { nums[nums[i] - 1] = nums[i].also { nums[i] = nums[nums[i] - 1] } } } for (i in0 until n) { if (nums[i] != i + 1) { return i + 1 } } return n + 1 } }
结果
执行用时 : 318 ms, 击败 93.88% 使用 Kotlin 的用户
内存消耗 : 49.80 MB, 击败 89.80% 使用 Kotlin 的用户
Dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution{ int firstMissingPositive(List<int> nums) { final n = nums.length; for (var i = 0; i < n; ++i) { while (1 <= nums[i] && nums[i] <= n && nums[nums[i] - 1] != nums[i]) { final temp = nums[i]; nums[i] = nums[temp - 1]; nums[temp - 1] = temp; } } for (var i = 0; i < n; ++i) { if (nums[i] != i + 1) { return i + 1; } } return n + 1; } }
结果
执行用时 : 312 ms, 击败 100.00% 使用 Dart 的用户
内存消耗 : 160.04 MB, 击败 100.00% 使用 Dart 的用户
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funcfirstMissingPositive(nums []int)int { n := len(nums) for i := 0; i < n; i++ { for1 <= nums[i] && nums[i] <= n && nums[nums[i]-1] != nums[i] { nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1] } } for i := 0; i < n; i++ { if nums[i] != i+1 { return i + 1 } } return n + 1 }
结果
执行用时 : 38 ms, 击败 92.71% 使用 Go 的用户
内存消耗 : 7.73 MB, 击败 97.26% 使用 Go 的用户
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# @param {Integer[]} nums # @return {Integer} deffirst_missing_positive(nums) n = nums.length (0...n).each do |i| while1 <= nums[i] && nums[i] <= n && nums[nums[i] - 1] != nums[i] nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1] end end (0...n).each do |i| return i + 1if nums[i] != i + 1 end n + 1 end
objectSolution{ deffirstMissingPositive(nums: Array[Int]): Int = { val n = nums.length defswap(i: Int, j: Int): Unit = { val temp = nums(i) nums(i) = nums(j) nums(j) = temp } for (i <- 0 until n) { while (1 <= nums(i) && nums(i) <= n && nums(nums(i) - 1) != nums(i)) { swap(i, nums(i) - 1) } } for (i <- 0 until n) { if (nums(i) != i + 1) { return i + 1 } } n + 1 } }