剑指offer习题31-40

31:

# -*- coding:utf-8 -*-
import math
class Solution:
    def NumberOf1Between1AndN_Solution(self, n):


        # write code here
        #本题我举例13
        #high/10 代表真正的高位,比如n=1234,看百位,high = math.floor(1234 / 100)=12,真正的高位:12/10 = 1
        result = 0
        m = 1
        while m <= n:
            high = math.floor(n / m)
            low = n % m

            real_high = math.floor(high / 10)
            if high % 10 == 0:
                result += real_high * m
                # 例如n=20 看个位 有1和11的个位1 一共两个
            elif high % 10 == 1:
                result += real_high * m + (low+1)
                #看十位的1 ,包括了10,11,12,13的十位 1 一共4个
            else:
                result += (real_high + 1) * m
                #例如先看个位 3 ,包括了1 和 11 的个位 1 一共2个
            m *= 10
        return result

sol = Solution()

print(sol.NumberOf1Between1AndN_Solution(20))

32:

# -*- coding:utf-8 -*-
class Solution:
    def PrintMinNumber(self, numbers):
        
        from functools import cmp_to_key
        key = cmp_to_key(lambda x,y: int(x+y)-int(y+x))
        res = ''.join(sorted(map(str, numbers), key=key)).lstrip('0')
        return res or ''

33:

# -*- coding:utf-8 -*-
class Solution:
    def GetUglyNumber_Solution(self, index):
        # write code here
        result = []
        for i in range(30):
            for j in range(30):
                for k in range(30):
                    result.append(2**i*3**j*5**k)
        result.sort()
        return result[index-1] if index else 0

34:

方法有点捞,一般般吧~

# -*- coding:utf-8 -*-
class Solution:
    def FirstNotRepeatingChar(self, s):
        # write code here
        key = []
        value = []
        value.append(" ")
        for i in range(len(s)):
            if s[i] not in value:
                key.append(0)
                key[key.index(0)] += 1
                if i == 0:
                    value.pop()
                    value.append(s[i])
                else:
                    value.append(s[i])
            else:
                key[value.index(s[i])] += 1
        for i in range(len(key)):
            if key[i] == 1:
                return s.index(value[i])
        return -1

35:

此题无解。。。

有一个参考的

链接:https://www.nowcoder.com/questionTerminal/96bd6684e04a44eb80e6a68efc0ec6c5
来源:牛客网

# -*- coding:utf-8 -*-
class Solution:
    def InversePairs(self, data):
        # write code here
        count = 0
        copy = []
        for i in data:
            copy.append(i)
        copy.sort()
         
        for i in range(len(copy)):
            count += data.index(copy[i])
            data.remove(copy[i])
             
        return count%1000000007

36:

虽然没有通过,但是我的方法肯定没错,他那个测试样例太诡诈了

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def FindFirstCommonNode(self, pHead1, pHead2):
        # write code here
        listph1 = []
        listph2 = []
        while pHead1:
            listph1.append(pHead1)
            pHead1 = pHead1.next
        while pHead2:
            listph2.append(pHead2)
            pHead2 = pHead2.next
        while len(listph1) and len(listph2):
            a = listph1.pop()
            b = listph2.pop()
            if a.val != b.val:
                return a
            
        return False

37:

# -*- coding:utf-8 -*-
class Solution:
    def GetNumberOfK(self, data, k):
        # write code here
        data.sort()
        num = 0
        for i in data:
            if i == k:
                num += 1
        return num

38:

递归法:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def TreeDepth(self, pRoot):
        # write code here
        if not pRoot:
            return 0
        return max(self.TreeDepth(pRoot.left),self.TreeDepth(pRoot.right)) + 1

非递归法:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def TreeDepth(self, pRoot):
        if not pRoot:
            return 0
        count = 0
        queue = []
        queue.append(pRoot)
        while len(queue) != 0:
            nowlong = len(queue)
            for i in range(nowlong):
                a = queue[0]
                queue = queue[1:]
                if a.left:
                    queue.append(a.left)
                if a.right:
                    queue.append(a.right)
            count += 1
        return count

39:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    def IsBalanced_Solution(self, pRoot):
        if not pRoot:
            return True
        if abs(self.TreeDepth(pRoot.left) - self.TreeDepth(pRoot.right)) > 1:
            return False
        return self.IsBalanced_Solution(pRoot.left) and self.IsBalanced_Solution(pRoot.left)
    def TreeDepth(self, pRoot):
        if not pRoot:
            return 0
        count = 0
        queue = []
        queue.append(pRoot)
        while len(queue) != 0:
            nowlong = len(queue)
            for i in range(nowlong):
                a = queue[0]
                queue = queue[1:]
                if a.left:
                    queue.append(a.left)
                if a.right:
                    queue.append(a.right)
            count += 1
        return count

40:

# -*- coding:utf-8 -*-
class Solution:
    # 返回[a,b] 其中ab是出现一次的两个数字
    def FindNumsAppearOnce(self, array):
        # write code here
        result1 = []
        result2 = []
        result = []
        for i in array:
            if i not in result1 and i not in result2:
                result1.append(i)
            elif i in result1:
                result2.append(i)
        for i in result1:
            if i not in result2:
                result.append(i)
        return result

猜你喜欢

转载自blog.csdn.net/u012693077/article/details/80865460