-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestNumberFromVector.cpp
More file actions
74 lines (57 loc) · 1.8 KB
/
largestNumberFromVector.cpp
File metadata and controls
74 lines (57 loc) · 1.8 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
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
string convertInt(int i) {
if (i==0) return "0";
string str;
while (i) {
str += i%10 + '0';
i = i/10;
}
reverse(str.begin(), str.end());
return str;
}
bool compare(string s1, string s2) {
return strcmp((s2+s1).c_str(), (s1+s2).c_str()) < 0;
/*
//cout<<s1<<" "<<s2<<endl;
int i, j;
for (i =0, j=0; i < s1.length() || j < s2.length(); i++, j++) {
if (s1[i] < s2[j]) return false;
if (s1[i] > s2[j]) return true;
}
while (i < s1.length()){
if (s1[i] < s2[j-1]) return true;
i++;
}
while (j < s2.length()){
if (s1[i-1] < s2[j]) return false;
j++;
}
return true;
*/
}
string largestNumber(const vector<int> &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
vector<string> b;
for (int i = 0; i < A.size(); ++i) {
b.push_back(convertInt(A[i]));
}
sort(b.begin(), b.end(), compare);
string str;
for (int i = 0; i< b.size(); ++i){
str += b[i];
}
return str;
}
int main() {
//vector<int> v = { 3, 30, 34, 5, 9 };
//vector<int> v = { 980, 674, 250, 359, 98, 969, 143, 379, 363, 106, 838, 923, 969, 880, 997, 664, 152, 329, 975, 377, 995, 943, 369, 515, 722, 302, 496, 124, 692, 993, 341, 785, 400, 113, 302, 563, 121, 230, 358, 911, 437, 438, 494, 599, 168, 866, 689, 444, 684, 365, 470, 176, 910, 204, 324, 657, 161, 884, 623, 814, 231, 694, 399, 126, 426};
vector<int> v = {0};
cout<<largestNumber(v);
}