Leetcode brush title records to prove safety offer

Interview questions 3: Repeat number array

# Use set, the time complexity of O (n), the spatial complexity of O (n-) 
class
Solution (Object): DEF findRepeatNumber (Self, the nums): "" " :type nums: List[int] :rtype: int """ a = set([]) for num in nums: if num in a: return num a.add(num)
# Barrel idea, the time complexity of O (n), space complexity O (1) 
class Solution (Object):
     DEF findRepeatNumber (Self, nums):
         "" "
        :type nums: List[int]
        :rtype: int
        """
        for i in range(len(nums)):
            val = nums[i]
            if val != i and val == nums[val]:
                return val
            nums[i], nums[val] = nums[val], nums[i]

 

Interview questions 4: a two-dimensional array to find

# From right to left to push down on the 
class Solution (Object):
     DEF findNumberIn2DArray (Self, the Matrix, target):
         "" "
        :type matrix: List[List[int]]
        :type target: int
        :rtype: bool
        """
        if matrix == [] or matrix == [[]]:
            return False
        c = len(matrix[0])-1
        l = 0
        while True:
            if matrix[l][c] == target:
                return True
            if matrix[l][c] > target:
                c -= 1
            else:
                l += 1
            if l > len(matrix)-1 or c < 0:
                return False

 

5 face questions: replace spaces

 

6 is inscribed: list print head from the tail

 

7 interview questions: rebuild binary tree

class Solution(object):
    def buildTree(self, preorder, inorder):
        """
        :type preorder: List[int]
        :type inorder: List[int]
        :rtype: TreeNode
        """
        def helper(inc_start, inc_end):
            if inc_start == inc_end:
                return None
            inc_value = preorder[self.pre_index]
            root = TreeNode(inc_value)
            self.pre_index += 1
            root.left = helper(inc_start, inc_value_map[inc_value])
            root.right = helper(inc_value_map[inc_value] + 1, inc_end)
            return root
        self.pre_index = 0
        inc_value_map = {v: k for k, v in enumerate(inorder)}
        return helper(0, len(inorder))

 

9 is inscribed: two queue stacks

 

Interview questions 10-I: Fibonacci number

 

Interview questions 10-II: frog jump problem step

Guess you like

Origin www.cnblogs.com/weswes/p/12306094.html