Simple algorithm interview questions (with their answers)

This is what I recently saw the interview questions, would like to try to write about myself, really learn Java. I just put a simple function to achieve, I hope you can teach me how to standardize the structure and naming them.

1. Given two strings a and b (longer than 100W), on the character type is a subset of b, b seeking in a complementary set of character types; (I believe that this problem should be ignored for the input Analyzing )

= a ' abcdefgcdeh ' 
b = ' CDE ' 
c = []
 # value of ab as defined above, c is defined sequence to match the correct initial position stored values, mainly to prevent a plurality of sequences of b 
for I in Range (len (a) -len (B) + 1'd): # define the starting position of the matching 
    index = I
     Print ( " *** " + STR (I))
     for J in Range (len (B)): 
        
        IF B [J] =! A [index]:
             BREAK 
        the else : 
            index = index +. 1
     IF J == len (B) -1 :
        c.append (I) # successfully matched position c is added to 
D = []
 for K in c: 
    D + = [K + m for m in Range (len (B))] # D sub successful match sequence set positions 
Print (D)
 for I in Range (len (a)):
     IF I not  in D:     # matching string within the sequence is not D 
        Print (a [I])

2. Given a positive integer N, is less than or equal to N, and returns the number of positive integers with at least one repeating numbers;

N=int(input('N:'))
num=0
if N<11:
    print(num)
else:
    for i in range(N+1):
        a={}
        i=str(i)
        for j in range(len(i)):
            if i[j] in a:
                num+=1
                break
            else:
                a.update({i[j]:0})
    print(num)

3. Telephone number combination. Below is a sample of the phone keys, each comprising a number of letter. For example the letter "A" by pressing a "2" is obtained, the letter "B" may be "2" obtained through twice, and so on. When given a string of numbers, we can obtain the corresponding maps, such as "22", the possibility represents a letter [ "AA", "B"]. Required: enter a numeric string, such as "2321241499844211." All output is likely to represent the letter combinations.

import numpy as np

ALPHABETS = 'abcdefghijklmnopqrstuvwxyz'
KEY_N = np.array([1,1,3,3,3,3,3,4,3,4])

def get_ch(ch, count):
    if ch < 2:
        return ''
    offset = -3
    for i in range(ch):
        offset += KEY_N[i]
    offset += count
    return ALPHABETS[offset]

def search(pre_res, inputs):
    len_inputs = len(inputs)
    # print('len_inputs:', len_inputs)
    if len_inputs == 0:
        print(pre_res)
        return

    ch = int(inputs[0])
    # print('ch:', ch)
    # print('KEY_N[ch]:', KEY_N[ch])
    for i in range(KEY_N[ch]):
        if i < len_inputs and int(inputs[i]) == ch:
            # print('1111111')
            # print(inputs[i+1:])
            # print(pre_res + get_ch(ch, i+1))
            search(pre_res + get_ch(ch, i+1), inputs[i+1:])
        else:
            # print('2222222')
            break

if __name__ == "__main__":
    test_case = '12156662227777'
    search('', test_case)

Guess you like

Origin www.cnblogs.com/superSaiyan/p/12168969.html