剑指offer系列——表示数值的字符串,字符流中第一个不重复的数组,链表中环的入口结点

表示数值的字符串

题目描述

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+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

猜你喜欢

转载自blog.csdn.net/weixin_41813772/article/details/82876438