golang練手小項目系列(1)-位向量

本系列整理了10個工作量和難度適中的Golang小項目,適合已經(jīng)掌握Go語法的工程師進(jìn)一步熟練語法和常用庫的用法碴裙。

問題描述

有一組非負(fù)整數(shù)钢悲,實(shí)現(xiàn)一個位向量類型,能在O(1)時間內(nèi)完成插入青团、刪除和查找等操作譬巫。

要點(diǎn):

實(shí)現(xiàn)Has(uint)、Add(uint)督笆、Remove(uint)芦昔、Clear()、Copy()娃肿、String()咕缎、AddAll(…uint)、UnionWith()料扰、IntersectWith()凭豪、DifferenceWith()、SymmetricDifference()方法晒杈。

拓展:

使用uint存儲而不是uint32或uint64這樣限定字長的類型嫂伞。

代碼實(shí)現(xiàn):

import (

? "bytes"

? "fmt"

)

func (s *IntSet)countBit(n uint)int{

? count := 0

? for n != 0{

? ? ? n = n & ( n - 1 )

? ? ? count += 1

? }

? return count

}

func (s *IntSet)calWordBit(x int)(word int, bit uint)? {

? word, bit = x / wordSize, uint(x%wordSize)

? return

}

const wordSize = 32 << (^uint(0) >> 63)

// An IntSet is a set of small non-negative integers.

// Its zero value represents the empty set.

type IntSet struct {

? words []uint

}

// Has reports whether the set contains the non-negative value x.

func (s *IntSet) Has(x int) bool {

? word, bit := s.calWordBit(x)

? return word < len(s.words) && s.words[word]&(1<<bit) != 0

}

// Add adds the non-negative value x to the set.

func (s *IntSet) Add(x int) {

? word, bit := s.calWordBit(x)

? for word >= len(s.words) {

? ? ? s.words = append(s.words, 0)

? }

? s.words[word] |= 1 << bit

}

func (s *IntSet) AddAll(nums ...int) {

? for _, n := range nums {

? ? ? s.Add(n)

? }

}

// 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)

? ? ? }

? }

}

// Set s to the intersection of s and t.

func (s *IntSet) IntersectWith(t *IntSet) {

? for i, tword := range t.words {

? ? ? if i < len(s.words) {

? ? ? ? s.words[i] &= tword

? ? ? } else {

? ? ? ? s.words = append(s.words, tword)

? ? ? }

? }

}

// Set s to the difference of s and t.

func (s *IntSet) DifferenceWith(t *IntSet) {

? for i, tword := range t.words {

? ? ? if i < len(s.words) {

? ? ? ? s.words[i] &^= tword

? ? ? } else {

? ? ? ? s.words = append(s.words, tword)

? ? ? }

? }

}

// Set s to the symmetric difference of s and t.

func (s *IntSet) SymmetricDifference(t *IntSet) {

? for i, tword := range t.words {

? ? ? if i < len(s.words) {

? ? ? ? s.words[i] ^= tword

? ? ? } else {

? ? ? ? s.words = append(s.words, tword)

? ? ? }

? }

}

// return the number of elements

func (s *IntSet) Len() int {

? count := 0

? for _, word := range s.words {

? ? ? count += s.countBit(word)

? }

? return count

}

// remove x from the set

func (s *IntSet) Remove(x int) {

? word, bit := s.calWordBit(x)

? s.words[word] &^= 1 << bit

}

// remove all elements from the set

func (s *IntSet) Clear() {

? for i := range s.words {

? ? ? s.words[i] = 0

? }

}

// return a copy of the set

func (s *IntSet) Copy() *IntSet {

? new := &IntSet{}

? new.words = make([]uint, len(s.words))

? copy(new.words, s.words)

? return new

}

// 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 < wordSize; j++ {

? ? ? ? if word&(1<<uint(j)) != 0 {

? ? ? ? ? ? if buf.Len() > len("{") {

? ? ? ? ? ? ? buf.WriteByte(' ')

? ? ? ? ? ? }

? ? ? ? ? ? fmt.Fprintf(&buf, "%d", wordSize*i+j)

? ? ? ? }

? ? ? }

? }

? buf.WriteByte('}')

? return buf.String()

}

// Return set elements.

func (s *IntSet) Elems() []int {

? e := make([]int, 0)

? for i, word := range s.words {

? ? ? for j := 0; j < wordSize; j++ {

? ? ? ? if word&(1<<uint(j)) != 0 {

? ? ? ? ? ? e = append(e, i*wordSize+j)

? ? ? ? }

? ? ? }

? }

? return e

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市拯钻,隨后出現(xiàn)的幾起案子帖努,更是在濱河造成了極大的恐慌,老刑警劉巖粪般,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拼余,死亡現(xiàn)場離奇詭異,居然都是意外死亡亩歹,警方通過查閱死者的電腦和手機(jī)匙监,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來小作,“玉大人亭姥,你說我怎么就攤上這事《愣瑁” “怎么了致份?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長础拨。 經(jīng)常有香客問我氮块,道長绍载,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任滔蝉,我火速辦了婚禮击儡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蝠引。我一直安慰自己阳谍,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布螃概。 她就那樣靜靜地躺著矫夯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪吊洼。 梳的紋絲不亂的頭發(fā)上训貌,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天,我揣著相機(jī)與錄音冒窍,去河邊找鬼递沪。 笑死,一個胖子當(dāng)著我的面吹牛综液,可吹牛的內(nèi)容都是我干的款慨。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼谬莹,長吁一口氣:“原來是場噩夢啊……” “哼檩奠!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起附帽,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤笆凌,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后士葫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡送悔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年慢显,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片欠啤。...
    茶點(diǎn)故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡荚藻,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出洁段,到底是詐尸還是另有隱情应狱,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布祠丝,位于F島的核電站疾呻,受9級特大地震影響除嘹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜岸蜗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一尉咕、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧璃岳,春花似錦年缎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至犁柜,卻和暖如春洲鸠,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背赁温。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工坛怪, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人股囊。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓袜匿,卻偏偏與公主長得像,于是被迫代替她去往敵國和親稚疹。 傳聞我的和親對象是個殘疾皇子居灯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,350評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,585評論 0 23
  • 寫完這個題目的時候,我看的電影已經(jīng)開演了内狗,害得我讓電腦白白浪費(fèi)了兩個小時的電怪嫌,《七月與安生》一個關(guān)于兩個朋友的故事...
    甜菜的眼淚閱讀 355評論 0 0
  • 我只是很煩 我需要發(fā)泄一下 我不知道原來異地戀這么不容易的 我和他在現(xiàn)實(shí)生活中明明那么親近 可為什么一到網(wǎng)上 就什...
    MrMuller閱讀 155評論 0 0
  • 莫急,莫慌柳沙,你且靜下心來岩灭,你當(dāng)舍棄些雜事
    咫尺無究閱讀 295評論 0 0