#include <iostream>#include <stdio.h>#include <vector>#include <string>#include <sstream>#include <algorithm>using namespace std;/*问题:Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be very large, so you need to return a string instead of an integer.分析:给定一个非负整数数组,将他们以一定次序排放,使其变成最大的数字。肯定是想把最大的数字放在最前面。这道题目好像是算法分析与设计的。如果用回溯,每次确定一个摆放位置,将摆放结果和最大结果比较即可。注意数字0不能作为头部。如果用dp,设dp[i]表示前i个数所能组成的最大数字,那么dp[i+1]要么是需要插入到一个合适的位置形成最大数字,每次插入时间复杂度为O(n),总的时间复杂度为O(n^2)5,53,如果当前的数和另一个数位数相同,把较大的放在前面,如果位数不同逐一比较,将某位上较小的数放在前面 如果前面都相同,比较位数较少的一位数最后一位不断和位数较多的数的最后一位一次比较,若较多的位数中某位值>位数较少最后一位, 则将较多位数的数放在前面5,5654,5355,55353,553输入:2(数组元素个数)5 5325 562 54 53255 553253 55353 30 34 5 9112121 12100 9 8 7 6 5 4 3 2 120 0输出:55356554535555355353953433011212198765432100报错:Runtime Error Message:terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::at: __n (which is 2) >= this->size() (which is 2)Last executed input:[121,12]Input:[0,9,8,7,6,5,4,3,2,1]Output:"9887654321"Expected:"9876543210"1】//这里移动的位置计算错了for(int k = result.size() - 2 ; k >= j + 1 ; k--)2】没有考虑到输入两个0:结果只需要返回一个03】未考虑824,8247等组合情况Input:[824,938,1399,5607,6973,5703,9609,4398,8247]Output:"9609938824782469735703560743981399"Expected:"9609938824824769735703560743981399"关键:1 leecode解法:https://leetcode.com/PRoblems/largest-number/?tab=Solutions对于任意两个整数m和n,确定谁的顺序放在前面,是由mn和nm谁的大小决定的,而不是n和m各自比较因此只需要用字符串比较函数,比较的时候把字符串拼接后再比较2 strcmp(str1,str2),str1 > str2,返回大于0*///比较的时候:如果str1+str2 > str1+str2,返回为返回truebool compare(string& str1 , string& str2){ string s1 = str1 + str2; string s2 = str2 + str1; //比较这两个字符串,strcmp //如果s1 < s1,返回 int result = strcmp(s1.c_str() , s2.c_str()); //s1 > s2,返回真 if(result > 0) { return true; } else { return false; }}class Solution {public: string largestNumber(vector<int>& nums) { if(nums.empty()) { return ""; } int size = nums.size(); vector<string> result; string num1; string num2; string value; for(int i = 0 ; i < size ; i++) { stringstream stream; stream << nums.at(i); value = stream.str(); result.push_back(value); } //得到整个字符串数组,自定义比较函数 sort(result.begin() , result.end() , compare); if(result.empty()) { return ""; } stringstream resultStream; for(int i = 0 ; i < size ; i++) { resultStream << result.at(i); } string sResult = resultStream.str(); if('0' == sResult.at(0))//防止"00" { return "0"; } return sResult; }};void print(vector<int>& result){ if(result.empty()) { cout << "no result" << endl; return; } int size = result.size(); for(int i = 0 ; i < size ; i++) { cout << result.at(i) << " " ; } cout << endl;}void process(){ vector<int> nums; int value; int num; Solution solution; string result; while(cin >> num ) { nums.clear(); for(int i = 0 ; i < num ; i++) { cin >> value; nums.push_back(value); } result = solution.largestNumber(nums); cout << result << endl; }}int main(int argc , char* argv[]){ process(); getchar(); return 0;}/* //如果num1 > num2,返回1,num1 < num2,返回-1,否则,返回0 int compare(string& num1 , string& num2) { if(num1.empty() && num2.empty()) { return 0; } else if(num1.empty()) { return -1; } else if(num2.empty()) { return 1; } int len1 = num1.length(); int len2 = num2.length(); int minLen = min(len1 , len2); for(int i = 0 ; i < minLen; i++) { if(num1.at(i) > num2.at(i)) { return 1; } else if(num1.at(i) < num2.at(i)) { return -1; } } if(len1 == len2) { return 0; } //说明前面的位数相同 char value; if(len1 > len2) { value = num2.at(minLen - 1); int i = minLen; while(i < len1) { if(num1.at(i) > value) { return 1; } else if(num1.at(i) < value) { return -1; } i++; } return 0; } else { value = num1.at(minLen - 1); int i = minLen; while(i < len2) { if(num2.at(i) > value) { return -1; } else if(num2.at(i) < value) { return 1; } i++; } return 0; } } string largestNumber(vector<int>& nums) { if(nums.empty()) { return ""; } int size = nums.size(); vector<string> result; string num1; string num2; string value; stringstream resultstream; resultstream << nums.at(0); result.push_back(resultstream.str()); for(int i = 1 ; i < size ; i++) { //寻找 stringstream stream; stream << nums.at(i); value = stream.str(); bool isFind = false; for(int j = i - 1; j >= 0 ; j--) { //如果当前的数,大于它,则向前继续寻找,直到找到一个数大于当前数,就插入在当前数的后面 //插入在元素i的位置处,其余位置后移 if(-1 == compare(value , result.at(j)) ) { isFind = true; result.push_back(value); //这里移动的位置计算错了 for(int k = result.size() - 2 ; k >= j + 1 ; k--) { result.at(k+1) = result.at(k); } result.at(j+1) = value; break; } } //如果没有找到,说明当前元素是最大的,插入在头部即可 if(!isFind) { result.insert(result.begin() , value); } } stringstream resStream; if(result.empty()) { return ""; } size = result.size(); for(int i = 0 ; i < size ; i++) { resStream << result.at(i); } string sResult = resStream.str(); //全部都是0则返回一个0 int pos = sResult.find_first_not_of('0'); if(string::npos == pos) { return "0"; } return sResult; }*/
新闻热点
疑难解答