把数组排成最小的数
来源:互联网 发布:淘宝宝贝上架技巧视频 编辑:程序博客网 时间:2024/05/19 14:16
题目地址http://codeup.cn/problem.php?id=2163
RT,很久以前的题,之前考虑的不对,后来偶然在《剑指offer》中看到了解法。
思路就是快排+自己写比较的规则
设有两个串x个y,分别连接成串xy和yx,再按字符串序按位进行比较即可。(之前直接比较了x和y,所以不对)
样例输入:
7
14 36 154 58 36 3 8
9
5534 5226 5636 99956 30 135 35 8 77
样例输出:
1415433636588
135303552265534563677899956
python代码:
#str x,y special comparedef isBigger(x,y): str1 = x+y str2 = y+x #print str1,str2 #if str1==str2:return True _len=len(str1) for i in range(_len): if i==_len-1:return True if str1[i]==str2[i]:continue elif str1[i]<str2[i]:return False else:return True def partition(array,low,high): pivot = array[low] #list while low < high: while low < high and isBigger(array[high],pivot):high -= 1 array[low] = array[high] while low < high and isBigger(pivot,array[low]):low += 1 array[high] = array[low] array[low] = pivot return low def quickSort(array,low,high): if low < high: #print low,high pivotpos = 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)
0 0
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数--总结
- 把数组排成最小的数--总结
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 把数组排成最小的数
- 博客搬家
- Android调用支付宝快捷支付时提示"系统繁忙,请稍后再试(ALI64)"错误
- thinkphp基本操作
- A点出发到各地的路径及其距离
- Merge Intervals 这个应该叫合并间隙?
- 把数组排成最小的数
- 使用phpmailer发送邮件,且需要激活完整代码
- iOS开发:获取矩形渐变色的UIImage
- 《APP开发》APP规范实例-详细的UI设计方法
- 每天学习一算法【3】单链表翻转
- TCP与UDP的区别,以及它们各自的定义
- java中“集合”与“数组”转换
- 我的安卓路(一)之”第一天好好玩呦“
- UVa 1597 - Searching the Web(模拟)