依图面试题

算法题

  • 螺旋矩阵
  • 寻找两个有序数组的中位数

合并排序:

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        new_nums = sorted(nums1 + nums2)
        new_len = new_nums.__len__()
        if new_len % 2 == 0:
            return (new_nums[new_len // 2] + new_nums[new_len // 2 - 1]) / 2
        else:
            return new_nums[new_len // 2]
  • 基本计算器(输入的是只含有括号数字加减号的字符串)

  • 计算器(加减乘除)

  • 大数相加

  • 大数相减(考虑正负)

思路:按照我们手写除法时的方法,两个数字末位对齐,从后开始,按位相减,不够减时向前位借一。 
最终结果需要去除首端的0.如果所有位都是0,则返回0。

def max_sub(line):
    a,b = line.strip().split()
    Minus_flag = 0
    if int(a) < int(b):
        a,b = b,a
        Minus_flag = 1
    a = [int(item) for item in a]
    b = [int(item) for item in b]

    res = ''
    for i in range(len(b)):
        flag_a = len(a)-1-i
        flag_b = len(b)-1-i
        if a[flag_a]>= b[flag_b]:
            res = str(a[flag_a]-b[flag_b])+res
        else:
            res = str(10+a[flag_a]-b[flag_b])+res
            while a[flag_a-1]==0:
                a[flag_a-1]=9
                flag_a -= 1
            a[flag_a-1] -= 1
    for j in range(len(a)-1-i-1,-1,-1):
        res = str(a[j])+res
    zero_flag=0
    for i in range(len(res)):
        if res[i]!='0':
            zero_flag=1
            break
    if zero_flag==0:
        return 0
    if Minus_flag:
        return '-'+res[i:]
    return res[i:]

print(max_sub('4321 4421'))
  • 八皇后

  • N皇后

  • 【LeetCode】304. Range Sum Query 2D:给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。 要求求和复杂度为O(1)。

  • 二维有序矩阵查找特定target是否存在

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

# -*- coding:utf-8 -*-


class Solution:
    # array 二维列表
    def Find(self, target, array):
        # write code here
        row = len(array)-1
        col = 0
        res = False
        while row >=0 and col <= len(array[0])-1:
            if array[row][col] > target:
                row -= 1
            elif array[row][col] < target:
                col += 1
            else:
                res = True
                break
        return res

if __name__ == '__main__':
    target = 8
    array = [[1,3,7,9],
             [2,4,8,10],
             [5,8,9,11],
             [7,9,10,12]]
    res = Solution().Find(target, array)
    print(res)
  • 最长01子串的问题
  • LeetCode560:和为K的子数组

  • LeetCode 4.寻找两个有序数组的中位数

解法0:合并+排序

class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        nums = sorted(nums1+nums2)
        if len(nums)%2 ==0:
            answer = (nums[int(len(nums)/2-1)]+nums[int(len(nums)/2)])/2
        else:
            answer = nums[int((len(nums)+1)/2)-1]
        return answer

解法一:时间复杂度O(m+n) 空间复杂度O(m+n)

思路

  1. 给定nums1给nums2,则中位数的位置一定能确定,len(nums1) + len(nums2) / 2的取整值,记为median_idx

  2. 需要区分是len(nums1) + len(nums2)是奇数还是偶数

  3. 两个指针,只需要从nums1和nums2头部开始往后移动,将两者中较小值放入新的数组

  4. 当新的数组的长度达到了median_idx,即可求中位数

  5. 过程中需要注意两个数组长度不等的情况,防止下标越界 

 

def findMedianSortedArrays(nums1, nums2):
    """
    :type nums1: List[int]
    :type nums2: List[int]
    :rtype: float
    """
    median_idx = int((len(nums1) + len(nums2)) / 2)
    index1, index2 = 0, len(nums1)
    combine_list = nums1 + nums2
    new_list = []
    i = 0

    while i <= median_idx:
        if index1 < len(nums1) and index2 - len(nums1) < len(nums2):
            new_list.append(min(combine_list[index1], combine_list[index2]))
            if combine_list[index1] < combine_list[index2]:
                index1 += 1
            else:
                index2 += 1
        elif index1 >= len(nums1):
            # nums1 is out of range, append nums2
            new_list.append(combine_list[index2])
            index2 += 1
        elif index2 >= len(nums2):
            # nums2 is out of range, append nums1
            new_list.append(combine_list[index1])
            index1 += 1
        i += 1

    if (len(nums1) + len(nums2)) % 2 == 0:
        median = float(new_list[median_idx] + new_list[median_idx - 1]) / 2
    else:
        median = new_list[median_idx]

    return median

思路三:https://www.jianshu.com/p/6eab8e87a9de

你可能感兴趣的:(面经)