1 Star 0 Fork 0

catyMap / AlgorithmNote

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
findErrorNums.go 587 Bytes
一键复制 编辑 原始数据 按行查看 历史
dogeMap 提交于 2021-07-06 08:12 . 更新题库
package main
import (
"fmt"
)
// 这道题先排序再找是行不通的,因为排序之后能找到丢失的,但是重复的会找错
// 这道题用二分也是不行的,因为一开始就是无序数组
// 简单题就直接暴力!
func findErrorNums(nums []int) (res []int) {
imap := make([]int, 10001, 10001)
res = make([]int, 2, 2)
for _, v := range nums {
imap[v]++
if imap[v] == 2 {
res[0] = v
}
}
for i := 1; i <= len(nums); i++ {
if imap[i] == 0 {
res[1] = i
}
}
return res
}
func main() {
fmt.Println(findErrorNums([]int{1, 2, 3, 3, 5}))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dogemap/algorithm-note.git
git@gitee.com:dogemap/algorithm-note.git
dogemap
algorithm-note
AlgorithmNote
dc486f96f6c1

搜索帮助