Leetcode刷題記錄_20181023

來源:互聯網
上載者:User

標籤:input   col   鏈表   http   problem   題記   sts   one   刷題   

160.Intersection of Two Linked Lists

尋找並返回AB鏈表中的交點,若無返回None

方法1:

計算A、B兩個鏈表長度,優先迴圈長度長的鏈表,長度差次迴圈後,依次對比AB

 1 class Solution(object): 2     def getIntersectionNode(self, headA, headB): 3         """ 4         :type head1, head1: ListNode 5         :rtype: ListNode 6         """ 7          8         a = headA 9         b = headB10         anum = 011         bnum = 012         if not a and not b: return None13         while a:14             anum +=115             a = a.next16         while b:17             bnum +=118             b = b.next19         flag = abs(anum-bnum)20         if anum>bnum:21             a = headA22             b = headB23             for i in range(flag):24                 a = a.next25             while a and b:26                 if a.val == b.val:27                     return a28                 a = a.next29                 b = b.next30             return None31         else:32             a = headA33             b = headB34             for i in range(flag):35                 b = b.next36             while a and b :37                 if a.val == b.val:38                     return b39                 a = a.next40                 b = b.next41             return None42         
View Code

方法2:

將AB組合,A迴圈到鏈表末尾轉至B,B迴圈到鏈表末尾轉至A,如有相同Node,返回

 1 class Solution(object): 2     def getIntersectionNode(self, headA, headB): 3         """ 4         :type head1, head1: ListNode 5         :rtype: ListNode 6         """ 7         8         if not headA or not headB: return None 9         a = headA10         b = headB11         flag = 012         while flag <=2:13             if a.val == b.val:14                 return a15             a = a.next16             b = b.next17             if a == None:18                 a = headB19                 flag +=120             if b == None:21                 b = headA22                 flag +=123         return None
View Code

167.Two Sum II - Input array is sorted

給定升序排列數組,尋找和為target的兩個數字下標+1(注意:數組中必然存在唯一解)

方法:

設定頭指標和尾指標,若和等於target,返回指標+1;若大於target,尾指標前移;否則頭指正後移

 1 class Solution(object): 2     def twoSum(self, numbers, target): 3         """ 4         :type numbers: List[int] 5         :type target: int 6         :rtype: List[int] 7         """ 8         a, b = 0,len(numbers)-1 9         while a < b:10             sum1 = numbers[a] + numbers[b]11             if  sum1 == target:12                 return [a+1,b+1]13             elif sum1 > target:14                 b -=115             else:16                 a +=117         
View Code

168.Excel Sheet Column Title

給定數字n,返回Excel中第n列列號

A-Z對應1-26,遞迴方法

class Solution(object):    def convertToTitle(self, n):        """        :type n: int        :rtype: str        """        base = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘        if n == 0:            return ‘‘        return self.convertToTitle((n-1)/26) + base[(n-1)%26]
View Code

 

Leetcode刷題記錄_20181023

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.