剑指Offer(33)求数组排列出来的最小数
2015-08-01 17:06
423 查看
求数组排列的最小数,一来可以通过求出各种排列组合,然后进行比较,得出结果,但这样会带来很多问题,一是计算量很大,时间复杂度高;二是排列出来的数可能是大数,不在int的比较范围之内。
还有一种方法,也是借鉴网上的方法,就是将排列好的大数转换成string类型,然后用泛型函数里的sort进行比较排序。这样就得出想要的答案了。当然首先要将int型的各个数组转换成string类型,这里借助了sstream(详情请查看这篇文章:http://blog.163.com/zhuandi_h/blog/static/180270288201291710222975/)。
下面直接贴出代码:
文章参考:http://blog.163.com/zhuandi_h/blog/static/180270288201291710222975/
http://blog.csdn.net/cxllyg/article/details/7659525 http://www.cnblogs.com/xinsheng/p/3585331.html
还有一种方法,也是借鉴网上的方法,就是将排列好的大数转换成string类型,然后用泛型函数里的sort进行比较排序。这样就得出想要的答案了。当然首先要将int型的各个数组转换成string类型,这里借助了sstream(详情请查看这篇文章:http://blog.163.com/zhuandi_h/blog/static/180270288201291710222975/)。
下面直接贴出代码:
#include "stdafx.h" #include <iostream> #include <stdio h=""> #include <string> #include <sstream> #include <algorithm> using namespace std; bool compare(string& str1, string& str2 ) { string s1 = str1; string s2 = str2; s1.append(str2); s2.append(s1); return s1 < s2; } void ComArrayMin(int* pArray, int num) { //int i; string* pStrArray = new string[num]; for(int i = 0; i < num; ++i) { stringstream stream; stream<<parray i="" stream="">>pStrArray[i]; } sort(pStrArray,pStrArray + num,compare); for(int i =0; i < num; ++i) { cout<<pStrArray[i]; } cout<<endl; delete[] pStrArray; } int _tmain(int argc, _TCHAR* argv[]) { int Array[] = {3,32,321}; int len = sizeof(Array)/sizeof(int); ComArrayMin(Array, len); getchar(); return 0; } </parray></algorithm></sstream></string></stdio></iostream>
文章参考:http://blog.163.com/zhuandi_h/blog/static/180270288201291710222975/
http://blog.csdn.net/cxllyg/article/details/7659525 http://www.cnblogs.com/xinsheng/p/3585331.html
相关文章推荐
- 编程算法 - 把数组排成最小的数 代码(C)
- 把数组排成最小的数
- 剑指offer:把数组排成最小的数(java)
- 【剑指offer】面试题33-把数组排成最小的数
- j剑指offer面试题[33]-把数组排成最小的数
- 把数组排成最小的数java实现
- 把数组排成最小的数
- 剑指offer-面试题33-把数组排成最小的数
- CSS基础-17CSS盒子模型-外边距合并
- JavaScript学习要点(十三)
- CSS基础-16CSS盒子模型-外边距
- CSS基础-15CSS盒子模型-边框
- CSS基础-14CSS盒子模型-内边距
- js预加载/延迟加载
- CSS基础-13CSS盒子模型-概述
- django css
- 浅谈JavaScript中setInterval和setTimeout的使用问题
- POJ3253Fence Repair【贪心+优先队列 】
- 访问WEB-INF下的jsp页面以及利用session访问webroot下的页面
- 两个不同HTML页面之间参数传递