To prove safety offer-- array

Description Title
(same as the length of each one-dimensional array) in a two-dimensional array, each row from left to right in order of ascending sort, to sort each column from top to bottom in increasing order. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

import numpy as np
class Solution:
    # array 二维列表
    def Find(self, target, array):
        # write code here
        a = np.array(array)
        rows = len(a)
        cols = len(a[0])
        x = 0
        y = cols-1
        while x < rows and y >= 0:
            if a[x][y] > target:
                y -= 1
            elif a[x][y] < target:
                x += 1
            else:
                return True
        return False

Title Description
All numbers in an array of length n's are in the range 0 to n-1. Some digital array is duplicated, but do not know how many numbers are duplicated. Do not know each digit is repeated several times. Please find an array of any one of the duplicate numbers. For example, if the length of the input array 7 {2,3,1,0,2,5,3}, then the corresponding output of the first 2 repeating digits.

# -*- coding:utf-8 -*-
class Solution:
    # 这里要特别注意~找到任意重复的一个值并赋值到duplication[0]
    # 函数返回True/False
    def __init__(self):
        self.dict = {}

    def duplicate(self, numbers, duplication):
        # write code here
        for i in range(len(numbers)):
            if not self.dict.get(numbers[i]):
                self.dict[numbers[i]] = 1
            else:
                duplication[0] = numbers[i]
                self.dict[numbers[i]] += 1
                return True
        return False

Title Description
Given an array to A [0,1, ..., n-please construct an array B [0,1, ..., n-where B is the element B [i] = A [0 ] A [. 1] ... A * [. 1-I] A [I +. 1] ... * A [n--. 1]. You can not use the division. (Note: The predetermined B [0] = A [1-2];)

class Solution:
    def multiply(self, A):
        # write code here
        length = len(A)
        B = [1] * length
        for i in range(1, length):
            B[i] = B[i-1] * A[i-1]
        tmp = 1
        for j in range(length-2, -1, -1):
            tmp *= A[j+1]
            B[j] = B[j] * tmp
        return B
Released nine original articles · won praise 0 · Views 757

Guess you like

Origin blog.csdn.net/weixin_42707571/article/details/105248548