劍指offer - 題49~55(字符串數字轉換,數組中重複的數字,乘積數組,正則匹配,字符串是否含數值,不重複字符,鏈表環入口結點)

把字符串轉換成整數
題目描述
將一個字符串轉換成一個整數,要求不能使用字符串轉換整數的庫函數。 數值爲0或者字符串不是一個合法的數值則返回0
輸入描述:
輸入一個字符串,包括數字字母符號,可以爲空
輸出描述:
如果是合法的數值表達則返回該數字,否則返回0
示例1

輸入
+2147483647
1a33
輸出
2147483647
0
# -*- coding:utf-8 -*-
class Solution:
    def StrToInt(self, s):
        # write code here
        char_arr=['0','1','2','3','4','5','6','7','8','9','+','-']
        result=0
        sign=1
        if not s:
            return 0
        for char in s:
            if char in char_arr:
                if char=="+":
                    sign=1
                    continue
                elif char=="-":
                    sign=-1
                    continue
                else:
                    result=result*10+char_arr.index(char)
            else:
                result=0
                break
        return result*sign

數組中重複的數字
在一個長度爲n的數組裏的所有數字都在0到n-1的範圍內。 數組中某些數字是重複的,但不知道有幾個數字是重複的。也不知道每個數字重複幾次。請找出數組中任意一個重複的數字。 例如,如果輸入長度爲7的數組{2,3,1,0,2,5,3},那麼對應的輸出是第一個重複的數字2。

# -*- coding:utf-8 -*-
class Solution:
    # 這裏要特別注意~找到任意重複的一個值並賦值到duplication[0]
    # 函數返回True/False
    def duplicate(self, numbers, duplication):
        # write code here
        dic={}
        for i in numbers:
            if i not in dic:
                dic[i]=1
            else:
                dic[i]+=1
        # 此處必須另起一個循環,因爲要找的是第一個有重複的數字
        for i in numbers:
            if dic[i]!=1:
                duplication[0]=i
                return True
        return False

構建乘積數組
給定一個數組A[0,1,…,n-1],請構建一個數組B[0,1,…,n-1],其中B中的元素B[i]=A[0] * A[1] * … * A[i-1] * A[i+1] * … * A[n-1]。不能使用除法。(注意:規定B[0] = A[1] * A[2] * … * A[n-1],B[n-1] = A[0] * A[1] * … * A[n-2];)

# -*- coding:utf-8 -*-
class Solution:
    def multiply(self, A):
        # write code here
        # 以[i]爲分割線,分別計算前半部分和後半部分的成績,然後再將兩部分乘起來
        front=[1]
        back=[1]
        for i in range(len(A)-1):
            front.append(front[i]*A[i])
            back.append(back[i]*A[-1-i])
        result=[front[j]*back[-j-1] for j in range(len(A))]
        return result

正則表達式匹配
請實現一個函數用來匹配包括’.‘和’‘的正則表達式。模式中的字符’.‘表示任意一個字符,而’'表示它前面的字符可以出現任意次(包含0次)。 在本題中,匹配是指字符串的所有字符匹配整個模式。例如,字符串"aaa"與模式"a.a"和"abaca"匹配,但是與"aa.a"和"ab*a"均不匹配

# -*- coding:utf-8 -*-
class Solution:
    # s, pattern都是字符串
    def match(self, s, pattern):
        # write code here
        if not s and not pattern:
            return True
        elif not pattern:
            return False
        elif not s:
            # *:代表前一個字符出現0次或n次
            if len(pattern)>1 and pattern[1]=='*':
                return self.match(s,pattern[2:])
            else:
                return False
        else:
            if len(pattern)>1 and pattern[1]=='*':
                if s[0]!=pattern[0] and pattern[0]!='.':
                    return self.match(s,pattern[2:])
                else:
                    return self.match(s[1:],pattern) or self.match(s,pattern[2:]) or self.match(s[1:],pattern[2:])
            else:
                if s[0]==pattern[0] or pattern[0]=='.':
                    return self.match(s[1:],pattern[1:])
                else:
                    return False

表示數值的字符串
請實現一個函數用來判斷字符串是否表示數值(包括整數和小數)。例如,字符串"+100",“5e2”,"-123",“3.1416"和”-1E-16"都表示數值。 但是"12e",“1a3.14”,“1.2.3”,"±5"和"12e+4.3"都不是。

# -*- coding:utf-8 -*-
class Solution:
    # s字符串
    def isNumeric(self, s):
        # write code here
        import re
        return re.match(r"^[\+\-]?[0-9]*(\.[0-9]*)?([eE][\+\-]?[0-9]+)?$",s)

字符流中第一個不重複的字符
請實現一個函數用來找出字符流中第一個只出現一次的字符。例如,當從字符流中只讀出前兩個字符"go"時,第一個只出現一次的字符是"g"。當從該字符流中讀出前六個字符“google"時,第一個只出現一次的字符是"l"。
輸出描述:

如果當前字符流沒有存在出現一次的字符,返回#字符。
# -*- coding:utf-8 -*-
class Solution:
    # 返回對應char
    def __init__(self):
        self.s=""
    def FirstAppearingOnce(self):
        # write code here
        res=list(filter(lambda c:self.s.count(c)==1,self.s))
        return res[0] if res else "#"
    def Insert(self, char):
        # write code here
        self.s+=char

鏈表中環的入口結點
給一個鏈表,若其中包含環,請找出該鏈表的環的入口結點,否則,輸出null。

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        # l_fast = l_slow + ring = 2 x l_slow,l_slow = ring 
        slow,fast=pHead,pHead
        while fast and fast.next:
            slow=slow.next
            fast=fast.next.next
            if slow==fast:
                slow2=pHead
                while slow!=slow2:
                    slow=slow.next
                    slow2=slow2.next
                return slow
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章