-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy path把数组排成最小的数.py
More file actions
34 lines (29 loc) · 983 Bytes
/
Copy path把数组排成最小的数.py
File metadata and controls
34 lines (29 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# coding: utf8
# @Author: 郭 璞
# @File: 把数组排成最小的数.py
# @Time: 2017/4/16
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 把数组排成最小的数
def fulllist(ls, result=[], position=0):
if position == len(ls)-1:
result.append(''.join(ls))
else:
for index in range(len(ls)):
ls[index], ls[position] = ls[position], ls[index]
fulllist(ls, result, position+1)
ls[position], ls[index] = ls[index], ls[position]
def method1(ls):
"""
借助“全排列”组装成数,再求最小的那个
:param ls:
:return:
"""
resultlist = []
fulllist(ls, resultlist, 0)
return min([int(item) for item in resultlist])
if __name__ == '__main__':
# ls = ['321', '32', '3']
ls = ['12', '1']
result = method1(ls)
print(result)