2022.08.19微软笔试
第三题图不太会,但感觉做过类似的。在这放下前两题,请大佬们指教。
一
def solution(X, Y, W): # write your code in Python (Python 3.6) l = len(X) if l == 0: return 0 X.sort() count = 0 start = X[0] for i in range(l): if i == 0: count += 1 else: if start + W < X[i]: count += 1 strart = X[i] return count二
def solution(S): # write your code in Python (Python 3.6) hashmap = dict() res_l = [] # res_r = [] res_sigle = [] for c in S: if int(c) in hashmap: hashmap[int(c)] += 1 else: hashmap[int(c)] = 1 for i in range(9,0,-1): if i in hashmap: if hashmap[i]>=2: l = hashmap[i]//2 for j in range(l): res_l.append(str(i)) if hashmap[i]%2: res_sigle.append(str(i)) else: res_sigle.append(str(i)) if 0 in hashmap: if hashmap[0]>=2: if len(res_l): l_0 = hashmap[i] // 2 for k in range(l_0): res_l.append(str(0)) else: res_sigle.append(str(0)) else: res_sigle.append(str(0)) if len(res_sigle): res = res_l+[res_sigle[0]]+res_l[::-1] else: res = res_l+res_l[::-1] return "".join(res)