2018-10-22

  • reverse list
def reverse_list(node)
  previous_node = None
  while node:
    node_next = node.next
    node.next = previous_node
    previous_node = node
    node = node_next
  return previous_node
  • find mid
 def find_mid(head)
  if not head or not head.next:
    return head
  slow = head
  fast = head
  while fast.next and fast.next.next:
    fast = fast.next.next
    slow = slow.next
  return slow
#4個member return 2nd
  • remove all nodes in a head
def remove_vowel(head)
  fake_node = ListNode(None)
  fake_next = head
  curr = fake_node
  while curr.next:
    if curr.next.val in ['a', 'e']:
      curr.next = curr.next.next
    else:
      curr.next = curr.next.next
  return fake_node.next
  • add two linkedlist 大數(shù)相加
    64位: 64個二進(jìn)制,CPU可進(jìn)行的最大位數(shù)的運算
def add(head1, head2)
  a = reverse_list(head1)
  b = reverse_list(head2)
  fake_node = ListNode('fake_head')
  cur_node = fake_head
  carry = 0
  while a and b
    temp = a.val + b.val +carry
    carry = temp / 10
    cur_node.next = ListNode(temp_sum % 10)
    cur_node = cur_node.next
    a = a.next
    b = b.next
  while a
    temp = a.val + carry
    carry = temp / 10
    curr_node.next = ListNode(carry % 10)
    curr_node = curr_node.next
    a = a.next
  while b
    temp = b.val + carry
    carry = temp / 10
    curr_node.next = ListNode(carry % 10)
    curr_node = curr_node.next
    b = b.next
  if carry > 0:
    cur_node.next = ListNode(carry)
  return reverse_list(fake_head.next)
#time O(n) space O(n)
  • determine if a list is palindrome(回文)
def is_palindrome(head):
  fake_head = ListNode('fake_head')
  fake_head.next = head
  mid_node_prev = find_mid(fake_node)
  mid_node = mid_node_prev.next
  
  mid_node_prev.next = None
  head1 = head
  head2 = reverse_list(mid_node)
  while head1 and head2:
    if head1.val != head2.val:
      return False
    head1 = head1.next
    head2 = head2.next
  return True
#time O(n) space O(1)
  • takes a nonnegative integer and returns the largest integer whose square is less than or equal to the given integer
#binary search in [1, n/2]
def square_root(n):
  if n<=0:
    return n
  left, right = 0, n/2
  while left < right - 1: #建議所有binary search都這么寫媳瞪,一定會返回[left, right]兩個元素的list
    mid = left + (right - left)/2 #avoid overflow
    mid_sq = mid*mid
    if mid == n:
      return mid
    if mid < n:
      left = mid
    else:
      right = mid
  return right if right*right <= n else left
  • what if the input is a real number?
def square_root_real(n, eps):
  left, right = 1.0, n*1.0/2
  if n < 1.0:
    left, right = n*1.0, 1.0
  while True:
    mid = left + (right - left) / 2
    midsq = mid*mid
    if abs(midsq - n)/n < eps:
      return mid
    elif midsq > n:
      right = mid
    else:
      left = mid
  return left
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末隧期,一起剝皮案震驚了整個濱河市扯夭,隨后出現(xiàn)的幾起案子缓醋,更是在濱河造成了極大的恐慌该默,老刑警劉巖住练,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件地啰,死亡現(xiàn)場離奇詭異,居然都是意外死亡讲逛,警方通過查閱死者的電腦和手機(jī)亏吝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盏混,“玉大人蔚鸥,你說我怎么就攤上這事⌒碓撸” “怎么了止喷?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長混聊。 經(jīng)常有香客問我弹谁,道長,這世上最難降的妖魔是什么句喜? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任预愤,我火速辦了婚禮,結(jié)果婚禮上咳胃,老公的妹妹穿的比我還像新娘植康。我一直安慰自己,他們只是感情好展懈,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布销睁。 她就那樣靜靜地躺著,像睡著了一般存崖。 火紅的嫁衣襯著肌膚如雪冻记。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天金句,我揣著相機(jī)與錄音檩赢,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛贞瞒,可吹牛的內(nèi)容都是我干的偶房。 我是一名探鬼主播,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼军浆,長吁一口氣:“原來是場噩夢啊……” “哼棕洋!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起乒融,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤掰盘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后赞季,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體愧捕,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年申钩,在試婚紗的時候發(fā)現(xiàn)自己被綠了次绘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡撒遣,死狀恐怖邮偎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情义黎,我是刑警寧澤禾进,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站廉涕,受9級特大地震影響泻云,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜火的,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一壶愤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧馏鹤,春花似錦征椒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至治力,卻和暖如春蒙秒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宵统。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工晕讲, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓瓢省,卻偏偏與公主長得像弄息,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子勤婚,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

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