# 二维数组中的查找
题目描述:给定一个数组,里面全部是数字,每个数字不可分割,请拼接成一个最大的数字(以字符串形式展示),如 [4, 5, 21, 90],拼接为 “905421”
# 方法
def compare_num(x, y): | |
max_length = max([len(str(x)), len(str(y))]) | |
for i in range(max_length): | |
x_bit = None | |
y_bit = None | |
if len(str(x)) > i: | |
x_bit = str(x)[i] | |
if len(str(y)) > i: | |
y_bit = str(y)[i] | |
if x_bit and y_bit: | |
if x_bit > y_bit: | |
return 1 | |
elif x_bit < y_bit: | |
return -1 | |
return 0 | |
if __name__ == '__main__': | |
# 核心思想是冒泡排序 和 按位比较 | |
numbers = [499, 55, 54, 540, 6, 99, 9966] | |
numbers = [str(n) for n in numbers] | |
for i in range(len(numbers)): | |
print(f'第{i+1}轮') | |
for j in range(0, len(numbers)-i-1): | |
first = numbers[j] | |
second = numbers[j+1] | |
if compare_num(first, second) == -1: | |
numbers[j], numbers[j+1] = numbers[j+1], numbers[j] | |
print(numbers) | |
print(''.join(numbers)) |