messiandzcy的专栏

题目地址?id=2163

RT,,很久以前的题,之前考虑的不对,后来偶然在《剑指offer》中看到了解法。

思路就是快排+自己写比较的规则

设有两个串x个y,分别连接成串xy和yx,再按字符串序按位进行比较即可。(之前直接比较了x和y,所以不对)

样例输入:

714 36 154 58 36 3 895534 5226 5636 99956 30 135 35 8 77

样例输出:

1415433636588135303552265534563677899956

python代码:

#str x,y special comparedef isBigger(x,y):str1 = x+ystr2 = y+x#print str1,str2#if str1==str2:return True_len=len(str1)for i in range(_len):if i==_len-1:return Trueif str1[i]==str2[i]:continueelif str1[i]<str2[i]:return Falseelse:return Truedef partition(array,low,high):pivot = array[low] #listwhile low < high:while low < high and isBigger(array[high],pivot):high -= 1array[low] = array[high]while low < high and isBigger(pivot,array[low]):low += 1array[high] = array[low]array[low] = pivotreturn lowdef quickSort(array,low,high):if low < high:#print low,highpivotpos = partition(array,low,high)quickSort(array,low,pivotpos-1)quickSort(array,pivotpos+1,high)while True:n = input()array = raw_input().split()quickSort(array,0,len(array)-1)print ''.join(array)

我躺下来,以一张报纸当枕头。高高在我上方的,

messiandzcy的专栏

相关文章:

你感兴趣的文章:

标签云: