python brush prove safety offer (update)

2019/07/28 6 questions began to brush every day, a total of 66 questions, brush three times, September 1 to complete.

1, a two-dimensional array to find:

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

The beginning of the idea:

# - * - Coding: UTF-8 - * - 
class Solution:
     # Array two-dimensional list, in fact, matrix 
    DEF the Find (Self, target, Array):
         # the Write code here Wallpaper 
        # rows of the matrix 
        rows = len (Array)
         # Matrix column 
        cols = len (Array [0])
         # traversing each matrix element 
        for I in Range (rows):
             for J in Range (cols):
                 # determines whether it contains the integer matrix 
                iF target == Array [I] [J]:
                     Print ( " find friends ")
                    return True
        return False

Write better:

class Solution:
     # Array two-dimensional list, in fact, matrix 
    DEF the Find (Self, target, Array):
         # the Write code here Wallpaper 
        # rows of the matrix 
        rows = len (Array) - 1 # column of the matrix 
        cols = len (array [0] ) --1 # matrices are ordered 
        I = rows   # last line of 
        J = 0   # a first column the while J <= cols and I> = 0:
             # If the integer less than X IF target < Array [I] [J]:
                 # OK reduced 
                I -. 1 = # If the integer less than X elif
        
        
        
            
            
            target> Array [I] [J]:
                 # column increases 
                J =. 1 + # exactly equal, to find the else :
                 return True
         # not found return False
            
            
        

 

2, implement a function, a string to replace each space to "20%." For example, when the string is We Are Happy. After the string is replaced after We% 20Are% 20Happy.

# -*- coding:utf-8 -*-
class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        # write code here
        return s.replace(" ", "%20")

No built-in functions:

DEF replaceSpace (S):
         # Write code here Wallpaper 
        # do not have built-in functions 
        # Create a new empty string 
        new_str = "" 
        # traverse the source string 
        for ELE in S:
             # Print (S) 
            # determines whether to traverse the character i spaces 
            # is not a space 
            IF ele.strip (): 
                new_str + = ELE
             # spaces 
            the else : 
                new_str + = " % 20 " 
        return new_str

 

3, an input list, the value returned by a ArrayList list sequentially from the tail to the head.

 Solution one:

class Solution:
     # return a list of values from the sequence of the tail to the head, for example, [l, 2,3] 
    DEF printListFromTailToHead (Self, listnode):
         # Write code here Wallpaper 
        # Note that this is a linked list is removed: listNode.next Value: listNode.val 
        new_list the = []
         # long list of non-null 
        the while listnode:
             # in the new sequence, every time the value 0 inserted position 
            new_list.insert (0, listNode.val) 
            listnode = listNode.next
         return new_list the

Solution two:

class Solution:
     # return a list of values from the sequence of the tail to the head, for example, [l, 2,3] 
    DEF printListFromTailToHead (Self, listnode):
         # Write code here Wallpaper 
        # Note that this is a linked list is removed: listNode.next Value: listNode.val 
        new_list the = []
         # long list of non-null 
        the while listnode:
             # new tail sequence added value 
            new_list.append (listNode.val) 
            listnode = listNode.next
         # finally reverse 
        return new_list the [:: -. 1]

 

Guess you like

Origin www.cnblogs.com/kongweisi/p/11261218.html