mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-06 07:31:57 +00:00
添加Go语言题解
This commit is contained in:
@@ -273,3 +273,28 @@ public:
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func firstMissingPositive(nums []int) int {
|
||||
length := len(nums)
|
||||
if length == 0 { return 1 }
|
||||
for i := 0; i < length; i++ {
|
||||
// 将不在正确位置的元素放在正确的位置上。
|
||||
for nums[i] > 0 && nums[i] < length + 1 && nums[i] != i + 1 && nums[i] != nums[nums[i] - 1] {
|
||||
j := nums[i] - 1
|
||||
nums[i], nums[j] = nums[j], nums[i]
|
||||
}
|
||||
}
|
||||
// 第一个不在正确位置上的元素就是结果。
|
||||
for i := 0; i < length; i++ {
|
||||
if nums[i] != i + 1 {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
return length + 1
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user