feat(Go-Tool):2021/03/18:新增超过半数数字系列算法题,新增出现次数不同的数字系列算法题

This commit is contained in:
Huangzj
2021-03-18 15:55:40 +08:00
parent 8dbfebe5be
commit 00ada130fa
14 changed files with 441 additions and 0 deletions
@@ -0,0 +1,58 @@
/*
* @Author : huangzj
* @Time : 2021/3/16 18:01
* @Description
*/
package NumberCount
type OnlyTwoNumberShowOnceObj struct {
}
func (*OnlyTwoNumberShowOnceObj) Doc() string {
return `
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度为O(n),空间复杂度为O(1)。
`
}
func OnlyTwoNumberShowOnce(numList []int) (int, int) {
if len(numList) < 2 {
panic("数组长度不能小于两个")
}
result := 0
//先进行异或,得到异或的结果
for _, num := range numList {
result = result ^ num
}
//找到异或后为1的位置
pos := 0
for ; pos < 32; pos++ {
if ((result >> pos) & 1) == 1 {
break
}
}
firstList := make([]int, 0)
secondList := make([]int, 0)
//根据异或位的结果,把数组分成两组
for _, num := range numList {
if ((num >> pos) & 1) == 1 {
firstList = append(firstList, num)
} else {
secondList = append(secondList, num)
}
}
first := 0
for _, num := range firstList {
first ^= num
}
second := 0
for _, num := range secondList {
second ^= num
}
return first, second
}