_ To prove safety offer_ arrays arranged in an array smallest number

The array arranged in the smallest number

Title Description
enter a positive integer array, the array of all the numbers arranged in a number spliced together, the splice can print out all numbers smallest one. 3,32,321 input array} {e.g., print the minimum number of three numbers can be arranged to 321,323.
Problem-solving ideas :
Here Insert Picture Description
my bubbling program:

# -*- coding:utf-8 -*-
class Solution:
    def PrintMinNumber(self, numbers):
        # write code here
        res = ""
        if len(numbers)==0:
            return res
        for i in range(len(numbers)-1):
            for j in range(i+1,len(numbers)):
                if int(str(numbers[i])+str(numbers[j])) >= int(str(numbers[j]) + str(numbers[i])):
                    numbers[i], numbers[j] = numbers[j], numbers[i]
            res += str(numbers[i])
        res += str(numbers[-1])
        return int(res)

Quick drain Great God (though not through, but the idea is to quickly exhaust the idea):

def strSort(self, list):
    if len(list) <= 1:
        return list
    left = self.strSort([i for i in list[1:] if (i+list[0]) < (list[0]+i)])
    right = self.strSort([i for i in list[1:] if i+list[0] >= list[0] + i])
    return left+[list[0]]+right

And a fast-programmed serious if:

def quick_sort(self, list):
    if len(list) < 2:
        return list[:]
    left = (self.quick_sort([i for i in list[1:] if i <= list[0]]))
    right = (self.quick_sort([i for i in list[1:] if i > list[0]]))
    return left + [list[0]] + right
Published 31 original articles · won praise 0 · Views 708

Guess you like

Origin blog.csdn.net/freedomUSTB/article/details/105231657