2018-10-25 First Unique Number In Stream [M]

  1. First Unique Number In Stream

LC:387

Given a continuous stream of numbers, write a function that returns the first unique number whenever terminating number is reached(include terminating number). If there no unique number before terminating number or you can't find this terminating number, return -1.

Example
Given a stream [1, 2, 2, 1, 3, 4, 4, 5, 6] and a number 5
return 3

Given a stream [1, 2, 2, 1, 3, 4, 4, 5, 6] and a number 7
return -1

class LinkedNode:
    def __init__(self, val=None, next=None):
        self.val = val
        self.next = next
        
class Solution:
    def __init__(self,):
        self.hash = {}
        self.head = LinkedNode()
        self.tail = self.head
        
    """
    @param nums: a continuous stream of numbers
    @param number: a number
    @return: returns the first unique number
    """
    def firstUniqueNumber(self, nums, number):
        if not nums:
            return -1
        
        for n in nums:
            if n in self.hash:
                if self.hash[n]:
                    self.remove(self.hash[n])
            else:
                self.push_back(LinkedNode(n))
            if n == number:
                return self.head.next.val
        return -1
    
    def push_back(self, node):
        self.hash[node.val] = self.tail
        self.tail.next = node
        self.tail = node
    
    # different from LRU, need to totally reomove
    # use self.hash[node] to denote that node is deleted
    def remove(self, prev):
        node = prev.next
        prev.next = node.next
        
        if node == self.tail:
            self.tail = prev
        else:
            self.hash[node.next.val] = prev
        
        self.hash[node.val] = None

Note:

  1. Almost the same to 2018-10-23/24 LRU Cache [H]

    1. need LinkedNode()
    2. same push_back()
    3. same __init__(), need hash(value, prev node)
  2. But different in:

    1. remove() instead of move_to_back()
    2. set hash[val] to be None if deleted, because hash still works as "visited"
  3. A really good way of practicing linked list

    1. Learn how to add, remove etc.
    2. Consider edge cases.
      1. tail of linked list inremove()
      2. what if a node is deleted
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子仰坦,更是在濱河造成了極大的恐慌,老刑警劉巖烫堤,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡鸽斟,警方通過查閱死者的電腦和手機拔创,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來富蓄,“玉大人剩燥,你說我怎么就攤上這事×⒈叮” “怎么了灭红?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長帐萎。 經常有香客問我比伏,道長胜卤,這世上最難降的妖魔是什么疆导? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮葛躏,結果婚禮上澈段,老公的妹妹穿的比我還像新娘。我一直安慰自己舰攒,他們只是感情好败富,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著摩窃,像睡著了一般兽叮。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上猾愿,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天鹦聪,我揣著相機與錄音,去河邊找鬼蒂秘。 笑死泽本,一個胖子當著我的面吹牛,可吹牛的內容都是我干的姻僧。 我是一名探鬼主播规丽,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼撇贺!你這毒婦竟也來了赌莺?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤松嘶,失蹤者是張志新(化名)和其女友劉穎雄嚣,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡缓升,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年鼓鲁,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片港谊。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡骇吭,死狀恐怖,靈堂內的尸體忽然破棺而出歧寺,到底是詐尸還是另有隱情燥狰,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布斜筐,位于F島的核電站龙致,受9級特大地震影響,放射性物質發(fā)生泄漏顷链。R本人自食惡果不足惜目代,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望嗤练。 院中可真熱鬧榛了,春花似錦、人聲如沸煞抬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽革答。三九已至战坤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間残拐,已是汗流浹背途茫。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蹦骑,地道東北人慈省。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像眠菇,于是被迫代替她去往敵國和親边败。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內容

  • perl: warning: Falling back to a fallback locale ("en_US....
    keaidelele閱讀 967評論 0 50
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • 每天下班經過那個十字路口,總是好多的電動車急急忙忙的穿梭著登疗,不管紅綠燈排截,他都敢過嫌蚤,也不打鈴。好幾次都差點撞到我断傲。我...
    鐵木真真閱讀 234評論 0 0
  • 有一種一塊大石頭終于落地的感覺脱吱。 雖然我知道其實我們這邊是個小石頭。 不過還是好開心啊认罩,可以放松...
    _aqu閱讀 328評論 2 0
  • 出了村子箱蝠,那幾個官兵就把我關進了一架馬車里。 馬車中還有四五個妙齡少女垦垂,人人面上都是悲哀絕望的神情宦搬,因著不敢惹怒了...
    冷清持閱讀 798評論 6 16