Leetcode Largest Number, 本题的主要内容是找出一个比较两数“大小”的方法,本算法提供一种递补归比较的方法,即把数拆从长度相等段(长的依赖短的拆分),这个不断的比下去,就会得到相应的“大小”关系。相关代码如下:
#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<algorithm>
/**
* The core of this algorithm is the compare function, we should find that if
* two number with the same length, we can compare it directly; if the tow with
* different length, we need to split the longer one, it comes out tow
* condition:
* 1: num1 > num2 or num1 < num2, we can result directly
* 2: num1 == num2, we need to split the longer one and compare recursively.
*/
using namespace std;
class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<vector<int> > info(nums.size(), vector<int>(2, 0));
string re;
int base = 1;
int flag = 0;
// get the length of the numbers
for (int i = 0; i < nums.size(); i ++) {
// make sure all of the number is not zero.
if (nums[i] != 0) {
flag = 1;
}
base = 1;
info[i][0] = nums[i];
while (nums[i] != 0) {
nums[i] /= 10;
base *= 10;
}
base = base == 1? 10 : base;
info[i][1] = base;
}
if (flag == 0) {
re = "0";
return re;
}
// use quik sort algorithm sort the info
sort(info, 0, nums.size() - 1);
// compute the result
for (int i = nums.size() - 1; i >= 0; i --) {
re.append(numToString(info[i][0], info[i][1]));
}
return re;
}
// convert a number to string
string numToString(int num, int base) {
string str;
base /= 10;
while (base != 0) {
str.push_back(num / base + '0');
num %= base;
base /= 10;
}
return str;
}
// use the quik sort algorithm to sort the arr
void sort(vector<vector<int> >& info, int start, int end) {
if (start >= end) {
return;
}
int low = start + 1;
int high = end;
int cur = start + 1;
int re;
while (cur <= high) {
re = compare(info[start][0], info[start][1], info[cur][0], \
info[cur][1]);
if (re <= 0) {
swap(info, cur, high);
high --;
} else {
cur ++;
}
}
swap(info, start, high);
sort(info, start, high - 1);
sort(info, high + 1, end);
}
// swap the two element
void swap(vector<vector<int> >& info, int pos1, int pos2) {
int tmp = info[pos1][0];
info[pos1][0] = info[pos2][0];
info[pos2][0] = tmp;
tmp = info[pos1][1];
info[pos1][1] = info[pos2][1];
info[pos2][1] = tmp;
}
// the rule of compare the two element
int compare(int num1, int base1, int num2, int base2) {
int tmp;
if (base1 == base2) {
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
} else if (num1 == num2) {
return 0;
}
} else if (base1 > base2) {
tmp = num1 / (base1 / base2);
if (tmp > num2) {
return 1;
} else if (tmp < num2) {
return -1;
} else if (tmp == num2) {
return compare(num1 % (base1 / base2), base1 / base2, num2, \
base2);
}
} else if (base1 < base2) {
tmp = num2 / (base2 / base1);
if (num1 > tmp) {
return 1;
} else if (num1 < tmp) {
return -1;
} else if (num1 == tmp) {
return compare(num1, base1, num2 % (base2 / base1), \
base2 / base1);
}
}
return 2;
}
};
// swap the two element
// // swap the two element
// Sample input: ./a.out argv1 argv2....
int main(int argc, char* argv[]) {
Solution so;
vector<int> vec;
for (int i = 1; i < argc; i ++) {
vec.push_back(atoi(argv[i]));
}
string re = so.largestNumber(vec);
cout<<"result: "<<re<<endl;
return 0;
}
相关测试用例:
test1:
23 3 3 4 5 231
result: 543323231
test2:
0 22 0 20
result: 222000