Leetcode刷題記錄-20181022

來源:互聯網
上載者:User

標籤:asc   ict   opened   分享圖片   oat   one   pen   type   next   

136. Single Number

方法一:建立字典,依次迴圈;

 1 class Solution: 2     def singleNumber(self, nums): 3         """ 4         :type nums: List[int] 5         :rtype: int 6         """ 7         dict1 = {} 8         for i in nums: 9             if i not in dict1:10                 dict1[i]= 111             else:12                 dict1[i] +=113         for i in dict1:14             if dict1[i] ==1:15                 return i
View Code

方法二:使用集合set(),返回集合差

1 class Solution:2     def singleNumber(self, nums):3         """4         :type nums: List[int]5         :rtype: int6         """7         8         return 2*sum(set(nums))-sum(nums)
View Code

141. Linked List Cycle  

判斷是否鏈表中是否有環

思想:設定快慢指標,如快指標為Null 或者快指標.next 為null,那麼沒有環;

 1 class Solution(object): 2     def hasCycle(self, head): 3         """ 4         :type head: ListNode 5         :rtype: bool 6         """ 7          8         if not head: 9             return False10         fast = head.next11         slow = head12         while (slow != fast):13             if fast == None or fast.next == None:14                 return False15             fast = fast.next.next16             slow = slow.next17            18         return True
View Code

155. Min Stack

 1 class MinStack: 2  3     def __init__(self): 4         """ 5         initialize your data structure here. 6         """ 7         self.stack = [] 8         self.min = float(‘inf‘) 9         10 11     def push(self, x):12         """13         :type x: int14         :rtype: void15         """16         self.stack.append(x)17         self.min = min(self.min, x)18 19     def pop(self):20         """21         :rtype: void22         """23         if self.stack.pop() == self.min :24             self.min = min(self.stack) if self.stack else float(‘inf‘)25         26         27 28     def top(self):29         """30         :rtype: int31         """32         return self.stack[-1]33 34     def getMin(self):35         """36         :rtype: int37         """38         return self.min
View Code

 

Leetcode刷題記錄-20181022

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.