牛客網刷題2--python

鏈表中環的入口節點

給一個鏈表,若其中包含環,請找出該鏈表的環的入口結點,否則,輸出null。
解法
參考:https://blog.csdn.net/wszll_Alex/article/details/86741909
在這裏插入圖片描述

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        if not pHead:
            return None
        fast = slow = pHead
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            
            if fast == slow:
                fast = pHead
                while fast != slow:
                    fast = fast.next
                    slow = slow.next
                return fast
            
        return None

包含min函數的棧

定義棧的數據結構,請在該類型中實現一個能夠得到棧中所含最小元素的min函數(時間複雜度應爲O(1))。
注意:保證測試中不會當棧爲空的時候,對棧調用pop()或者min()或者top()方法。

解法
其實就是提前存下一個min值,隨時更新它就可以。

# -*- coding:utf-8 -*-
class Solution:
    def __init__(self):
        self.stack = []
        self.mi = 100000
        
    def push(self, node):
        # write code here
        self.stack.append(node)
        if node < self.mi:
            self.mi = node
            
    def pop(self):
        # write code here
        t = self.stack[-1]
        self.stack = self.stack[:-1]
        if t == self.mi:
            self.mi = 100000
            for x in self.stack:
                if x < self.mi:
                    self.mi = x
                
        return t
    
    def top(self):
        # write code here
        t = self.stack[-1]
        return t
    
    def min(self):
        # write code here
        return self.mi
        

字符流中第一個不重複的字符

請實現一個函數用來找出字符流中第一個只出現一次的字符。例如,當從字符流中只讀出前兩個字符"go"時,第一個只出現一次的字符是"g"。當從該字符流中讀出前六個字符“google"時,第一個只出現一次的字符是"l"。
輸出描述:
如果當前字符流沒有存在出現一次的字符,返回#字符。

解法
想到了哈希,用一個列表存儲所有進來的字符,隨時判斷每個字符是否重複,並且隨時更新當前指向第一個不重複字符的指針。
但是後來看了解析,發現並不需要這麼複雜,可以使用隊列的思想,不需要保存下所有字符,只需要保存那些不重複的字符。

  • 入隊:獲取字符流中的一個字符時,當我們判斷它是不重複時,將它加入隊列;
  • 輸出/出隊:注意,因爲隊列中存儲的 “不重複字符” 在一系列的流讀取操作後,隨時有可能改變狀態(變重複),所以,隊列中的字符不能直接輸出,要先進行一次重複判斷,如果發現隊頭字符已經重複了,就將它移出隊列並判斷新的隊頭,否則,輸出隊頭的值;
# -*- coding:utf-8 -*-
class Solution:
    def __init__(self):
        self.s = []
        self.dic = {}
        self.idx = -1
    # 返回對應char
    def FirstAppearingOnce(self):
        # write code here
        if self.idx == -1:
            return "#"
        return self.s[self.idx]
    
    def Insert(self, char):
        # write code here
        self.s.append(char)
        if char not in self.dic:
            self.dic[char] = 1
            if self.idx == -1:
                self.idx = len(self.s) - 1
        else:
            self.dic[char] += 1
            
        if self.idx != -1 and self.dic[self.s[self.idx]] > 1:
            flag = 0
            for i in range(self.idx + 1, len(self.s)):
                if self.dic[self.s[i]] == 1:
                    self.idx = i
                    flag = 1
                    break
            if flag == 0:
                self.idx = -1
                    

把二叉樹打印出多行

從上到下按層打印二叉樹,同一層結點從左至右輸出。每一層輸出一行。

解法
二叉樹的層次遍歷用的就是BFS,爲了確定當前是哪一行,所以需要在隊列中再保存一個當前的層數。

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回二維列表[[1,2],[4,5]]
    def Print(self, pRoot):
        # write code here
        if not pRoot:
            return []
        res = [[]]
        q = []
        q.append((pRoot, 0))
        
        while q:
            cur, n = q.pop(0)
            if n >= len(res):
                res.append([])
            res[n].append(cur.val)
            if cur.left:
                q.append((cur.left, n + 1))
            if cur.right:
                q.append((cur.right, n + 1))
                
        return res
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章