Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6d3f3c5

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent e9af039 commit 6d3f3c5

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

‎179_largest_number/largest_number.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ struct object {
88

99
static int compare(const void *o1, const void *o2)
1010
{
11-
char p1[32];
12-
char p2[32];
13-
p1[0] = '0円';
14-
p2[0] = '0円';
11+
char p1[32] = { '0円' };
12+
char p2[32] = { '0円' };
1513
strcat(p1, ((struct object *) o1)->buf);
1614
strcat(p1, ((struct object *) o2)->buf);
1715
strcat(p2, ((struct object *) o2)->buf);

‎179_largest_number/largest_number.cc‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
string largestNumber(vector<int>& nums) {
8+
vector<string> strs;
9+
for (const auto i : nums) {
10+
strs.push_back(to_string(i));
11+
}
12+
13+
auto cmp = [](string s1, string s2) { return s1 + s2 > s2 + s1; };
14+
sort(strs.begin(), strs.end(), cmp);
15+
16+
if (strs[0] == "0") {
17+
return "0";
18+
}
19+
20+
string res;
21+
for (const auto& s : strs) {
22+
res += s;
23+
}
24+
return res;
25+
}
26+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int findKthLargest(vector<int>& nums, int k) {
8+
priority_queue<int, vector<int>, greater<int>> queue;
9+
for (const auto i : nums) {
10+
if (queue.size() < k) {
11+
queue.push(i);
12+
} else if (i > queue.top()) {
13+
queue.pop();
14+
queue.push(i);
15+
}
16+
}
17+
return queue.top();
18+
}
19+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /