zoukankan      html  css  js  c++  java
  • <剑指offer>面试题

    题目1:二维数组的查找

    题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

    class Solution:
        # array 二维列表
        def Find(self, target, array):
            for i in range(len(array)):
                for j in range(len(array[0])):
                    if array[i][j] == target:
                        return True
            return False
    
    array = [[1,2,3],[4,5,6],[7,8,9]]
    solution = Solution()
    print(solution.Find(9,array))

    题目2:替换空格

    题目:请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

    class Solution:
        # s 源字符串
        def replaceSpace(self, s):
            s =s.replace(' ','%20')
            # s = '%20'.join(s.split(' '))
            return s
            # write code here
    
    
    str = "We Are Happy."
    solution = Solution()
    print(solution.replaceSpace(str))

    题目3:从尾到头打印链表 

    题目:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

    class ListNode:
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution:
        # 返回从尾部到头部的列表值序列,例如[1,2,3]
        def printListFromTailToHead(self, listNode):
            newlist = []
            while listNode:
                newlist.append(listNode.val)
                listNode = listNode.next
            return newlist[::-1]
    
    A1 = ListNode(1)
    A2 = ListNode(2)
    A3 = ListNode(3)
    A4 = ListNode(4)
    A5 = ListNode(5)
    
    A1.next = A2
    A2.next = A3
    A3.next = A4
    A4.next = A5
    
    solution = Solution()
    print(solution.printListFromTailToHead(A1))

    题目4:重建二叉树

    题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

    (前序:根-左-右 中序:左-根-右  后序:左-右-跟)

    class Solution:
        # 返回构造的TreeNode根节点
        def reConstructBinaryTree(self, pre, tin):
            # write code here
            if len(pre)==0:
                return None
            elif len(pre)==1:
                return TreeNode(pre[0])
            else:
                root=TreeNode(pre[0])
                #根节点的索引
                post = tin.index(pre[0])
                root.left=self.reConstructBinaryTree(pre[1:post+1],tin[:post])
                root.right=self.reConstructBinaryTree(pre[post+1:],tin[post+1:])
            return root

    题目5:用2个栈实现队列

    class Solution:
        def __init__(self):
            #A为入栈,B为出栈
            self.stackA=[]
            self.stackB=[]
        def push(self, node):
            #入栈直接压入就行
            self.stackA.append(node)
            # write code here
        def pop(self):
            # return xx
            #如果B不为空,先将B出栈
            if self.stackB:
                return self.stackB.pop()
            #如果A为空,返回None
            elif not self.stackA:
                return None
            else:
                while self.stackA:
                    #将栈A出栈的值反压入栈B中,出栈时就说先入先出
                    self.stackB.append(self.stackA.pop())
                return self.stackB.pop()
    

    题目6:旋转数组的最小数字 

    题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

    解法1:(懒)

    class Solution:
        def minNumberInRotateArray(self, rotateArray):
            
            if len(rotateArray)==0:
                return 0
            else:
                return min(rotateArray)

    解法2:(无脑)

    class Solution:
        def minNumberInRotateArray(self, rotateArray):
            
            if len(rotateArray)==0:
                return 0
            else:
                for i in range(1,len(rotateArray)):
                    if rotateArray[0]>rotateArray[i]:
                        rotateArray[0],rotateArray[i] = rotateArray[i],rotateArray[0]
                return rotateArray[0] 

    题目7:斐波拉契数列

    题目:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。n<=39

    解法1:递归(时间复杂度太大,可能编译不通过)

    class Solution:
        def Fibonacci(self, n):
            if n <=0:
                return 0
            elif n <= 2:
                return 1
            return self.Fibonacci(n-1)+self.Fibonacci(n-2)

    解法2:循环就完了

    class Solution:
        def Fibonacci(self, n):
            if n ==0:
                return 0
            elif n == 1:
                return 1
            else:
                big = 1
                small = 0
                for i in range(2,n+1):
                    big,small = big+small,big
                return big

    题目8:跳台阶

    题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

    倒着想.n级的跳法=n-1级的跳法+n-2级的跳法,实际上也是斐波拉契数列的问题

    class Solution:
        def jumpFloor(self, number):
            if number==0:
                return 0
            elif number<=2:
                return number
            else:
                f1,f2 = 1,2
                for i in range(3,number+1):
                    f1,f2 = f2,f1+f2
                return f2

    题目9:变态跳台阶

    题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

    同上一题

    f(n)=f(0)+f(1)+...+f(n-1)  f(n-1)=f(0)+f(1)+...+f(n-2)  第一个式子减第二个式子f(n)=2f(n-1)

    class Solution:
        def jumpFloor(self, number):
            if number==0:
                return 0
            elif number<=2:
                return number
            else:
                f1,f2 = 1,2
                for i in range(3,number+1):
                    f1,f2 = f2,2*f2
                return f2 

    题目10:矩形覆盖

    题目:我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

    也是斐波拉契数列的问题,第一次横着放f(n-1),竖着放(必须放2个)f(n-2)   f(n)=f(n-1)+f(n-2)

    class Solution:
        def rectCover(self, number):
            if number==0:
                return 0
            elif number<=2:
                return number
            else:
                f1,f2 = 1,2
                for i in range(3,number+1):
                    f1,f2 = f2,f1+f2
                return f2

    题目11:二进制中1的个数

    题目:输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

    负数先与0xffffffff做与运算

        def NumberOf1(self, n):
            # write code here
            return bin(n&0xffffffff).count('1')  

    题目12:数值的整数次方

    题目:给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

    emmm   硬来

    class Solution:
        def Power(self, base, exponent):
            if base==0:
                return 0
            elif base==1 or exponent==0:
                return 1
            elif exponent==1:
                return base
            else:
                result = 1.0
                for i in range(abs(exponent)):
                    result *= base
                if exponent < 0 :
                    return 1.0/result
                else:
                    return result
            # write code here

    题目13:调整数组顺序使奇数位于偶数前面

    题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

    class Solution:
        def reOrderArray(self, array):
            if len(array) <= 1:
                return array
            else:
                result = []
                list = []
                for i in range(len(array)):
                    if array[i]%2 != 0:
                        result.append(array[i])
                    else:
                        list.append(array[i])
                for j in list:
                    result.append(j)
                return result
            # write code here

    题目14:链表中倒数第K个节点

    题目:输入一个链表,输出该链表中倒数第k个结点。

    将链表转化成列表在求节点的值

    class Solution:
        def FindKthToTail(self, head, k):
            if head == None or k <= 0:
                return None
            l = []
            while head != None:
                l.append(head)
                head = head.next
            if k>len(l):
                return None
            else:
                result = l[-k]
                return result
            # write code here

    题目15:反转链表 

    题目:输入一个链表,反转链表后,输出新链表的表头。

    class Solution:
        # 返回ListNode
        def ReverseList(self, pHead):
            if pHead==None or pHead.next==None:
                return pHead
            result=None
            while pHead!=None:
                temp=pHead.next    
                pHead.next=result    
                result=pHead    
                pHead=temp   
            return result
            # write code here

    题目16:合并2个排序的链表

    题目:输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

    class Solution:
        # 返回合并后列表
        def Merge(self, pHead1, pHead2):
            if pHead1==None and pHead2==None:
                return None
            num1=[]
            num2=[]   
            while pHead1!=None:
                num1.append(pHead1.val)
                pHead1=pHead1.next
            while pHead2!=None:
                num2.append(pHead2.val)
                pHead2=pHead2.next
            num=num1+num2
            num.sort()
            pHead=ListNode(num[0])
            pre=pHead
            for i in range(1,len(num)):
                node=ListNode(num[i])
                pre.next=node
                pre=pre.next
            return pHead 

    题目17:树的子结构

    pass

    题目18:二叉镜像树

    pass

    题目19:顺时针打印矩阵

  • 相关阅读:
    Android Media Playback 中的MediaPlayer的用法及注意事项(二)
    Android Media Playback 中的MediaPlayer的用法及注意事项(一)
    34. Search for a Range
    33. Search in Rotated Sorted Array
    32. Longest Valid Parentheses
    31. Next Permutation下一个排列
    30. Substring with Concatenation of All Words找出串联所有词的子串
    29. Divide Two Integers
    28. Implement strStr()子串匹配
    27. Remove Element
  • 原文地址:https://www.cnblogs.com/shuimohei/p/10420523.html
Copyright © 2011-2022 走看看