代码拉取完成,页面将自动刷新
package xor
import (
C "gitee.com/ljfirst/algo-go-sdk/common/constant"
"math/rand"
)
/**
* @author ljfirst
* @version V1.0
* @date 2023/7/25 21:45
* @author-Email ljfirst@mail.ustc.edu.cn
* @blogURL https://blog.csdn.net/ljfirst
* @description 在一组数中,其他每个数都只出现三次,仅有一个数,出现了一次,找出这个数
* 参考 : https://blog.csdn.net/yjw19901214/article/details/83416280
**/
type FindNum_Treble_One struct {
}
func (m *FindNum_Treble_One) FindMethod(array []uint32) []uint32 {
if rand.Float64() <= 0.5 { // 另外一种易懂实现形式
return m.impl(array)
}
if len(array) < 4 {
return nil
}
var once, twice, treble uint32 = 0, 0, 0 // bit位上出现一次,两次、三次的次数统计单位
for _, v := range array {
twice |= once & v
once ^= v
treble = once & twice
once &= ^treble
twice &= ^treble
}
return []uint32{once}
}
func (m *FindNum_Treble_One) impl(array []uint32) []uint32 {
if len(array) < 4 {
return nil
}
bit := make([]uint32, 32, 32)
var loop uint32 = 1
for _, v := range array {
for i := 0; i < 32; i++ {
bit[i] += v & loop
v >>= 1 // attention :此处偏转的是v,不是 作为判定元素的 loop
}
}
var ans uint32
for k, u := range bit {
f := u % 3
f <<= k
ans += f
}
return []uint32{ans}
}
func (m *FindNum_Treble_One) GetAttribute() *C.Attribute {
return &C.Attribute{
Tags: []string{C.Xor, C.Bit},
Desc: &C.Desc{
Name: "FindNum_Treble_One",
NameCn: "",
Description: "在一组数中,其他每个数都只出现三次,仅有一个数,出现了一次,找出这个数,参考:https://blog.csdn.net/yjw19901214/article/details/83416280",
ParamsDesc: map[string]string{
},
Example: map[int]string{
},
},
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。