code:
func firstMissingPositive(nums []int) int {
nLen := len(nums)
if 0 == nLen {
return 1
}
for i, _ := range nums {
for nums[i] != i + 1 && nums[i] > 0 && nums[i] <= nLen && nums[i] != nums[nums[i] - 1] {
nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i]
}//for nums[i] != i + 1 && nums[i] > 0 && nums[i] <= nLen
}//for i, _ := range nums
for i, _ := range nums {
if nums[i] != i + 1 {
return i + 1
}
}//for i, _ := range nums
return nLen + 1
}
result:
personal opinion:
in golang, it is easy to write function of swap