Skip to content

Commit 455ef26

Browse files
committed
2
1 parent ff8773e commit 455ef26

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
254254
#### [354. Russian Doll Envelopes](https://github.com/hitzzc/go-leetcode/tree/master/russian_doll_envelopes)
255255
#### [367. Valid Perfect Square](https://github.com/hitzzc/go-leetcode/tree/master/valid_perfect_square)
256256
#### [368. Largest Divisible Subset](https://github.com/hitzzc/go-leetcode/tree/master/largest_divisible_subset)
257+
#### [371. Sum of Two Integers](https://github.com/hitzzc/go-leetcode/tree/master/sum_of_two_integers)
258+
#### [373. Find K Pairs with Smallest Sums](https://github.com/hitzzc/go-leetcode/tree/master/find_k_pairs_with_smallest_sums)
257259

258260

259261

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
4+
priority_queue<pair<int, int>, vector<pair<int, int>>, CMP> q;
5+
vector<pair<int, int>> result;
6+
for (int i = 0; i < min(k, (int)nums1.size()); ++i) {
7+
for (int j = 0; j < min(k, (int)nums2.size()); ++j) {
8+
auto node = make_pair(nums1[i], nums2[j]);
9+
if (q.size() == k && node.first + node.second < q.top().first + q.top().second) {
10+
q.pop();
11+
}
12+
if (q.size() < k) q.push(node);
13+
}
14+
}
15+
while (!q.empty()) {
16+
result.push_back(q.top());
17+
q.pop();
18+
}
19+
return result;
20+
}
21+
22+
struct CMP {
23+
bool operator () (const pair<int, int>& a, const pair<int, int>& b) {
24+
return a.first + a.second < b.first + b.second;
25+
}
26+
};
27+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int getSum(int a, int b) {
4+
int carry = 0;
5+
while (b != 0) {
6+
carry = a & b;
7+
a ^= b;
8+
b = carry << 1;
9+
}
10+
return a;
11+
}
12+
};

0 commit comments

Comments
 (0)