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.
解题思路:先按照一定的规则对数字进行排序,排序规则是:对两个变量从最高位开始进行逐位比较,当只有其中一个变量还有未比较位数时,从该位数字开始,与另一变量进行逐位比较。当该变量只有一位时,则看该位是否不小于另一变量中的最大数字。
实现代码:
/***************************************************************************** * @COPYRIGHT NOTICE * @Copyright (c) 2015, 楚兴 * @All rights reserved * @Version : 1.0 * @Author : 楚兴 * @Date : 2015/2/6 13:23 * @Status : Accepted * @Runtime : 9 ms *****************************************************************************/ #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution { public: string largestNumber(vector<int> &num) { Solution s; string str = ""; sort(num.begin(),num.end(),s); char ch[11]; int sum = 0; for (auto it = num.begin(); it != num.end(); it++) { sum += *it; itoa(*it,ch,10); str += ch; } return sum == 0 ? "0" : str; } bool operator()(int m, int n) { if (m == n) { return false; } char ch1[11]; char ch2[12]; itoa(m,ch1,10); itoa(n,ch2,10); char* t1 = ch1; char* t2 = ch2; while (*t1 && *t2) { if (*t1 != *t2) { return *t1 > *t2; } t1++; t2++; } while (*t1) { return strcmp(t1,ch2); } while(*t2) { return !strcmp(t2,ch1); } } private: char maxchar(char* ch) { char* temp = ch; char max = '\0'; while(*temp) { max = max > *temp ? max : *temp; temp++; } return max; } //如果ch1应在前,返回true,否则返回false bool strcmp(char* ch1, char* ch2) { char* t1 = ch1; char* t2 = ch2; // ch2位数较短 while (*t1) { if (*t2 == '\0') { t2 = ch2; //指向首字符 } if (*t1 != *t2) { return *t1 > *t2; } else { if (*(t1 + 1) == '\0') { char c = maxchar(ch2); bool b = *t1 >= c; return b; } } t1++; t2++; } return false; } void itoa(int n, char ch[11], int R = 10) { char* temp = ch; if (n == 0) { strcpy(ch,"0"); return; } while(n) { *temp++ = n % R + '0'; n = n / R; } *temp = '\0'; reverse(ch); } void reverse(char ch[11]) { char* p = ch; char* q = ch + strlen(ch) - 1; char c; while(p < q) { c = *p; *p++ = *q; *q-- = c; } } }; int main() { vector<int> nums; nums.push_back(8308); nums.push_back(830); for (int i = 9; i >= 0; i--) { nums.push_back(i); } nums.push_back(2281); nums.push_back(121); nums.push_back(12); Solution s; string str = s.largestNumber(nums); cout<<str.c_str()<<endl; system("pause"); }第二种思路:省去前面找排序思路的复杂过程,直接先排序,再检验。
/***************************************************************************** * @COPYRIGHT NOTICE * @Copyright (c) 2015, 楚兴 * @All rights reserved * @Version : 2.0 * @Author : 楚兴 * @Date : 2015/2/6 14:06 * @Status : Accepted * @Runtime : 12 ms *****************************************************************************/ #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution { public: string largestNumber(vector<int> &num) { Solution s; string str = ""; sort(num.begin(),num.end(),s); char ch[11]; int sum = 0; for (auto it = num.begin(); it != num.end(); it++) { sum += *it; itoa(*it,ch,10); str += ch; } return sum == 0 ? "0" : str; } bool operator()(int m, int n) { if (m == 0) //等于0时不能放前面 { return false; } char ch1[11]; char ch2[11]; itoa(m,ch1); itoa(n,ch2); if (strlen(ch1) == strlen(ch2)) { return m >= n; } char ch3[22]; char ch4[22]; memcpy(ch3,ch1,sizeof(ch1)); strcat(ch3,ch2); memcpy(ch4,ch2,sizeof(ch2)); strcat(ch4,ch1); char* t1 = ch3; char* t2 = ch4; while(*t1) { if (*t1 != *t2) { return *t1 > *t2; } t1++; t2++; } return true; } private: void strcat(char* input, char* output) { char* t1 = input; char* t2 = output; while (*t1) { t1++; } while(*t2) { *t1++ = *t2++; } *t1 = '\0'; } void itoa(int n, char ch[11], int R = 10) { char* temp = ch; if (n == 0) { strcpy(ch,"0"); return; } while(n) { *temp++ = n % R + '0'; n = n / R; } *temp = '\0'; reverse(ch); } void reverse(char ch[11]) { char* p = ch; char* q = ch + strlen(ch) - 1; char c; while(p < q) { c = *p; *p++ = *q; *q-- = c; } } }; int main() { vector<int> nums; nums.push_back(8308); nums.push_back(830); for (int i = 9; i >= 0; i--) { nums.push_back(0); } nums.push_back(2281); nums.push_back(121); nums.push_back(12); Solution s; string str = s.largestNumber(nums); cout<<str.c_str()<<endl; char ch[10]; memset(ch,0,2); cout<<strlen(ch)<<endl; system("pause"); }
Java代码:
// Runtime: 129 ms public class Solution { public String largestNumber(int[] nums) { String[] words = new String[nums.length]; int sum = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; words[i] = String.valueOf(nums[i]); } if (sum == 0) { return "0"; } Arrays.sort(words, new Comparator<String>() { public int compare(String o1, String o2) { String s1 = o1 + o2; String s2 = o2 + o1; return s2.compareTo(s1); } } ); StringBuilder sb = new StringBuilder(); for (String word : words) { sb.append(word); } return sb.toString(); } }