-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path179.largest-number.cpp
More file actions
63 lines (51 loc) · 1.34 KB
/
179.largest-number.cpp
File metadata and controls
63 lines (51 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "testharness.h"
#include <map>
#include <string>
#include <string.h>
#include <vector>
using namespace std;
class Solution {
public:
struct IntStr;
string largestNumber(vector<int> &num) {
map<IntStr, int> numSet;
for (size_t i = 0; i < num.size(); ++i) {
numSet[IntStr(num[i])]++;
}
string result;
for (auto iter = numSet.rbegin(); iter != numSet.rend(); ++iter) {
for (int i = 0; i < (*iter).second; ++i) {
result += (*iter).first.value;
}
}
if (result[0] == '0')
return "0";
else
return result;
}
struct IntStr {
IntStr(int n) {
num = n;
sprintf(value, "%d", n);
}
bool operator<(const IntStr& rhs) const {
int i = 0;
int j = 0;
while (value[i] != '\0' || rhs.value[j] != '\0') {
if (value[i] == '\0') i = 0;
if (rhs.value[j] == '\0') j = 0;
if (value[i] != rhs.value[j]) {
return value[i] < rhs.value[j];
} else {
i++; j++;
}
}
return num < rhs.num;
}
int num;
char value[16];
};
};
TEST(Solution, test) {
ASSERT_EQ(2, 1+1);
}