[日常] Go语言圣经-指针对象的方法-bit数组习题
练习6.1: 为bit数组实现下面这些方法
func (*IntSet) Len() int // return the number of elements
func (*IntSet) Remove(x int) // remove x from the set
func (*IntSet) Clear() // remove all elements from the set
func (*IntSet) Copy() *IntSet // return a copy of the set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | package main import ( "bytes" "fmt" ) func main() { var x, y IntSet x.Add(1) x.Add(144) x.Add(9) fmt.Println(x.String()) // "{1 9 144}" y.Add(9) y.Add(42) fmt.Println(y.String()) // "{9 42}" x.UnionWith(&y) fmt.Println(x.String()) // "{1 9 42 144}" fmt.Println(x.Len()) // 返回4 //x.Remove(9) //"{1 42 144}" z := x.Copy() x.Clear() fmt.Println(x.String()) //返回{} fmt.Println(z.String()) //"{1 9 42 144}" fmt.Println(x.Has(9), x.Has(123)) // "true false" } // An IntSet is a set of small non-negative integers. // Its zero value represents the empty set. type IntSet struct { words []uint64 } // Has reports whether the set contains the non-negative value x. func (s *IntSet) Has(x int) bool { word, bit := x/64, uint(x%64) return word < len(s.words) && s.words[word]&(1<<bit) != 0 } // UnionWith sets s to the union of s and t. func (s *IntSet) UnionWith(t *IntSet) { for i, tword := range t.words { if i < len(s.words) { s.words[i] |= tword } else { s.words = append(s.words, tword) } } } // String returns the set as a string of the form "{1 2 3}". func (s *IntSet) String() string { var buf bytes.Buffer buf.WriteByte( '{' ) for i, word := range s.words { if word == 0 { continue } for j := 0; j < 64; j++ { if word&(1<<uint(j)) != 0 { if buf.Len() > len( "{" ) { buf.WriteByte( ' ' ) } fmt.Fprintf(&buf, "%d" , 64*i+j) } } } buf.WriteByte( '}' ) return buf.String() } /* 练习6.1: 为bit数组实现下面这些方法 */ func (s *IntSet) Len() int { sum := 0 for _, word := range s.words { for j := 0; j < 64; j++ { if word&(1<<uint(j)) != 0 { sum++ } } } return sum } //往集合中添加元素 //1. 或|;两个值其中之一为1,结果为1 //2. 1 << bit 1左移到指定位 //3. a |= b ==> a= a|b 最终实现设置指定位为1 func (s *IntSet) Add(x int) { word, bit := x/64, uint(x%64) for word >= len(s.words) { s.words = append(s.words, 0) } s.words[word] |= 1 << bit } //删除集合中的元素 //1.异或^ :两个值相同,结果为0;两个值不同结果为1; //2.与&:两个值都是1,结果为1;其他结果为0 //3. s.words[word] ^ (1 << bit) 把我指定位的1改成了0 //4. a &= b ==> a=a&b 最终实现设置指定位为0 func (s *IntSet) Remove(x int) { word, bit := x/64, uint(x%64) s.words[word] &= s.words[word] ^ (1 << bit) } //清空集合 //1. 设置每个位都为0 //2. 使用异或,把位是1的改成0 func (s *IntSet) Clear() { for i, word := range s.words { for j := 0; j < 64; j++ { if word&(1<<uint(j)) != 0 { s.words[i] ^= 1 << uint(j) } } } } //copy一个set //编译器判断变量的生命期会超出作用域后,自动在堆上分配 func (s *IntSet) Copy() (r *IntSet) { var result IntSet for _, word := range s.words { result.words = append(result.words, word) } return &result } |
十年开发经验程序员,离职全心创业中,历时三年开发出的产品《唯一客服系统》
一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的网站在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的全渠道在线客服系统,致力于帮助广大开发者/公司快速部署整合私有化客服功能。
开源地址:唯一客服(开源学习版)
官网地址:唯一客服官网
标签:
Go
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2016-04-20 [android] 帧动画和补间动画
2016-04-20 [MongoDB] mongodb与php