劍指offer系列——表示數值的字串,字元流中第一個不重複的陣列,連結串列中環的入口結點
阿新 • • 發佈:2018-11-11
表示數值的字串
題目描述
請實現一個函式用來判斷字串是否表示數值(包括整數和小數)。例如,字串"+100","5e2","-123","3.1416"和"-1E-16"都表示數值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。
解題思路:
法一:用float進行轉換
法二:
正則表示式匹配規律
法三:
判斷s中所有字串,以e為界,e後面不能出現.或空,否則直接返回False,然後把e前後兩部分全部放到
一個判斷函式裡面,考慮所有出現的字串,+-不能出現在首位,字串裡面.出現次數不能超過1。
程式碼:
法一:
# -*- coding:utf-8 -*- class Solution: # s字串 def isNumeric(self, s): # write code here try: return float(s) except: return 0
法二:
# -*- coding:utf-8 -*-
class Solution:
# s字串
def isNumeric(self, s):
# write code here
if not s:
return 0
import re
return re.match(r'^[\+\-]?[0-9]*(\.[0-9]*)?([eE][\+\-]?[0-9]+)?$',s)
法三:
# -*- coding:utf-8 -*- class Solution: # s字串 def isNumeric(self, s): # write code here if not s or len(s)<0: return 0 alist = [i.lower() for i in s] if 'e' in alist: index = alist.index('e') front = alist[:index] behind = alist[index+1:] if '.' in behind or len(behind) == 0: return False isfront = self.Digit(front) isbehind = self.Digit(behind) return isfront and isbehind else: isNum = self.Digit(alist) return isNum def Digit(self, alist): dotNum = 0 allowNum = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.', 'e'] for i in range(len(alist)): if alist[i] not in allowNum: return False if alist[i] == '.': dotNum +=1 if alist[i] in '+-'and i !=0: return False if dotNum >1: return False return True
字元流中第一個不重複的陣列
題目描述
請實現一個函式用來找出字元流中第一個只出現一次的字元。例如,當從字元流中只讀出前兩個字元"go"時,第一個只出現一次的字元是"g"。當從該字元流中讀出前六個字元“google"時,第一個只出現一次的字元是"l"。
輸出描述:
如果當前字元流沒有存在出現一次的字元,返回#字元。
解題思路:
引入兩個輔助儲存空間。一個Dict儲存當前出現的字元以及字元出現的次數,一個List儲存當前出現字元。
然後每次比較List的第一個字元在Dict中對應的次數,如果為1則輸出這個字元,如果不為1則彈出這個字元比較下一個字元。
程式碼:
# -*- coding:utf-8 -*- class Solution: # 返回對應char def __init__(self): self.alist=[] self.adict={} def FirstAppearingOnce(self): # write code here while len(self.alist)>0 and self.adict[self.alist[0]]==2: self.alist.pop(0)#刪除陣列中第一個元素 if len(self.alist)==0: return "#" else: return self.alist[0] def Insert(self, char): # write code here if char not in self.adict.keys(): self.adict[char] =1 self.alist.append(char) else: self.adict[char] =2
連結串列中環的入口結點
題目描述
給一個連結串列,若其中包含環,請找出該連結串列的環的入口結點,否則,輸出null。
解題思路:
法一:兩個指標一個fast,一個slow同時從一個連結串列頭部出發,一快一慢,如果連結串列中有環,則環內相遇。此時把其中一個指標重新指向連結串列頭部,另一個不變(環內),則兩個指標一次走一步 ,再相遇則是入口結點。
法二:遍歷連結串列,環的存在,遍歷遇見的第一個重複的即為入口節點
程式碼:
法一:
# -*- 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 pHead ==None or pHead.next == None or pHead.next.next==None:
return None
slow, fast = pHead.next,pHead.next.next
while slow != fast:
slow = slow.next
fast = fast.next.next
fast = pHead
while fast!=slow:
fast = fast.next
slow = slow.next
return fast
法二:
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
tempList = []
p = pHead
while p:
if p in tempList:
return p
else:
tempList.append(p)
p = p.next