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 307b2bc

Browse files
cpp
Change-Id: I90b2c24989f146480df491d55865d71b3b762505
1 parent 54454b7 commit 307b2bc

8 files changed

+840
-0
lines changed

‎cpp/leetcode/1.两数之和.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* @lc app=leetcode.cn id=1 lang=cpp
3+
*
4+
* [1] 两数之和
5+
*
6+
* https://leetcode.cn/problems/two-sum/description/
7+
*
8+
* algorithms
9+
* Easy (52.84%)
10+
* Likes: 16011
11+
* Dislikes: 0
12+
* Total Accepted: 4.1M
13+
* Total Submissions: 7.7M
14+
* Testcase Example: '[2,7,11,15]\n9'
15+
*
16+
* 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值
17+
* target 的那 两个 整数,并返回它们的数组下标。
18+
*
19+
* 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
20+
*
21+
* 你可以按任意顺序返回答案。
22+
*
23+
*
24+
*
25+
* 示例 1:
26+
*
27+
*
28+
* 输入:nums = [2,7,11,15], target = 9
29+
* 输出:[0,1]
30+
* 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
31+
*
32+
*
33+
* 示例 2:
34+
*
35+
*
36+
* 输入:nums = [3,2,4], target = 6
37+
* 输出:[1,2]
38+
*
39+
*
40+
* 示例 3:
41+
*
42+
*
43+
* 输入:nums = [3,3], target = 6
44+
* 输出:[0,1]
45+
*
46+
*
47+
*
48+
*
49+
* 提示:
50+
*
51+
*
52+
* 2 <= nums.length <= 10^4
53+
* -10^9 <= nums[i] <= 10^9
54+
* -10^9 <= target <= 10^9
55+
* 只会存在一个有效答案
56+
*
57+
*
58+
*
59+
*
60+
* 进阶:你可以想出一个时间复杂度小于 O(n^2) 的算法吗?
61+
*
62+
*/
63+
64+
// @lc code=start
65+
#include <iostream>
66+
#include <unordered_map>
67+
#include <vector>
68+
class Solution {
69+
public:
70+
std::vector<int> twoSum(std::vector<int> &nums, int target) {
71+
std::unordered_map<int, int> store;
72+
for (int i = 0; i < nums.size(); i++) {
73+
int rest = target - nums[i];
74+
auto it = store.find(rest);
75+
if (it != store.end()) {
76+
return {it->second, i};
77+
}
78+
store[nums[i]] = i;
79+
}
80+
return {};
81+
}
82+
};
83+
// @lc code=end
84+
85+
int main(int argc, const char **argv) {
86+
Solution s;
87+
std::vector<int> arr = {2, 3, 4, 5};
88+
auto ret = s.twoSum(arr, 6);
89+
std::cout << "ret: ";
90+
for (size_t i = 0; i < ret.size(); i++) {
91+
std::cout << ret[i] << ", ";
92+
}
93+
std::cout << std::endl;
94+
return 0;
95+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* @lc app=leetcode.cn id=11 lang=cpp
3+
*
4+
* [11] 盛最多水的容器
5+
*
6+
* https://leetcode-cn.com/problems/container-with-most-water/description/
7+
*
8+
* algorithms
9+
* Medium (63.82%)
10+
* Likes: 2531
11+
* Dislikes: 0
12+
* Total Accepted: 454.7K
13+
* Total Submissions: 712.4K
14+
* Testcase Example: '[1,8,6,2,5,4,8,3,7]'
15+
*
16+
* 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai)
17+
* 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)
18+
* 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
19+
*
20+
* 说明:你不能倾斜容器。
21+
*
22+
*
23+
*
24+
* 示例 1:
25+
*
26+
*
27+
*
28+
*
29+
* 输入:[1,8,6,2,5,4,8,3,7]
30+
* 输出:49
31+
* 解释:图中垂直线代表输入数组
32+
* [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
33+
*
34+
* 示例 2:
35+
*
36+
*
37+
* 输入:height = [1,1]
38+
* 输出:1
39+
*
40+
*
41+
* 示例 3:
42+
*
43+
*
44+
* 输入:height = [4,3,2,1,4]
45+
* 输出:16
46+
*
47+
*
48+
* 示例 4:
49+
*
50+
*
51+
* 输入:height = [1,2,1]
52+
* 输出:2
53+
*
54+
*
55+
*
56+
*
57+
* 提示:
58+
*
59+
*
60+
* n = height.length
61+
* 2
62+
* 0
63+
*
64+
*
65+
*/
66+
67+
// @lc code=start
68+
#include <algorithm>
69+
#include <iostream>
70+
#include <vector>
71+
class Solution {
72+
public:
73+
int maxArea(std::vector<int> &height) {
74+
int l = 0, r = height.size() - 1, ma = 0;
75+
while (l < r) {
76+
if (height.at(l) < height.at(r)) {
77+
ma = std::max(ma, (r - l) * height.at(l));
78+
l++;
79+
} else {
80+
ma = std::max(ma, (r - l) * height.at(r));
81+
r--;
82+
}
83+
}
84+
return ma;
85+
}
86+
};
87+
// @lc code=end
88+
89+
void print_ret(int ret) {
90+
std::cout << "ret: ";
91+
std::cout << ret << " ";
92+
std::cout << std::endl;
93+
}
94+
95+
int main(int argc, const char **argv) {
96+
Solution s;
97+
std::vector<int> arr = {1,8,6,2,5,4,8,3,7};
98+
auto ret = s.maxArea(arr);
99+
print_ret(ret);
100+
return 0;
101+
}

‎cpp/leetcode/15.三数之和.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* @lc app=leetcode.cn id=15 lang=cpp
3+
*
4+
* [15] 三数之和
5+
*
6+
* https://leetcode-cn.com/problems/3sum/description/
7+
*
8+
* algorithms
9+
* Medium (32.32%)
10+
* Likes: 3410
11+
* Dislikes: 0
12+
* Total Accepted: 532.8K
13+
* Total Submissions: 1.6M
14+
* Testcase Example: '[-1,0,1,2,-1,-4]'
15+
*
16+
* 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c
17+
* ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
18+
*
19+
* 注意:答案中不可以包含重复的三元组。
20+
*
21+
*
22+
*
23+
* 示例 1:
24+
*
25+
*
26+
* 输入:nums = [-1,0,1,2,-1,-4]
27+
* 输出:[[-1,-1,2],[-1,0,1]]
28+
*
29+
*
30+
* 示例 2:
31+
*
32+
*
33+
* 输入:nums = []
34+
* 输出:[]
35+
*
36+
*
37+
* 示例 3:
38+
*
39+
*
40+
* 输入:nums = [0]
41+
* 输出:[]
42+
*
43+
*
44+
*
45+
*
46+
* 提示:
47+
*
48+
*
49+
* 0
50+
* -10^5
51+
*
52+
*
53+
*/
54+
55+
// @lc code=start
56+
#include <algorithm>
57+
#include <iostream>
58+
#include <ostream>
59+
#include <vector>
60+
class Solution {
61+
public:
62+
std::vector<std::vector<int>> threeSum(std::vector<int> &nums) {
63+
std::vector<std::vector<int>> ret;
64+
if (nums.size() < 3) {
65+
return ret;
66+
}
67+
std::sort(nums.begin(), nums.end());
68+
if (nums.at(nums.size() - 1) < 0) {
69+
return ret;
70+
}
71+
for (size_t i = 0; i < nums.size() - 2; i++) {
72+
if (nums.at(0) > 0) {
73+
break;
74+
}
75+
if (i > 0 && nums.at(i) == nums.at(i - 1)) {
76+
continue;
77+
}
78+
int rest = 0 - nums.at(i);
79+
int l = i + 1, r = nums.size() - 1;
80+
while (l < r) {
81+
int tmp = nums.at(l) + nums.at(r);
82+
if (tmp == rest) {
83+
ret.push_back({nums.at(i), nums.at(l), nums.at(r)});
84+
while (l < r && nums.at(l) == nums.at(l + 1)) {
85+
l++;
86+
}
87+
while (l < r && nums.at(r) == nums.at(r - 1)) {
88+
r--;
89+
}
90+
l++;
91+
r--;
92+
} else if (tmp < rest) {
93+
l++;
94+
} else {
95+
r--;
96+
}
97+
}
98+
}
99+
return ret;
100+
}
101+
};
102+
// @lc code=end
103+
104+
void print_ret(const std::vector<std::vector<int>> &ret) {
105+
std::cout << "ret: [" << std::endl;
106+
for (size_t i = 0; i < ret.size(); i++) {
107+
std::cout << "[";
108+
for (size_t j = 0; j < ret.at(i).size(); j++) {
109+
std::cout << ret.at(i).at(j) << ",";
110+
}
111+
std::cout << "]" << std::endl;
112+
}
113+
std::cout << "]" << std::endl;
114+
}
115+
116+
int main(int argc, const char **argv) {
117+
Solution s;
118+
std::vector<int> arr = {-1, 0, 1, 2, -1, -4};
119+
auto ret = s.threeSum(arr);
120+
print_ret(ret);
121+
return 0;
122+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* @lc app=leetcode.cn id=16 lang=cpp
3+
*
4+
* [16] 最接近的三数之和
5+
*
6+
* https://leetcode-cn.com/problems/3sum-closest/description/
7+
*
8+
* algorithms
9+
* Medium (40.62%)
10+
* Likes: 187
11+
* Dislikes: 0
12+
* Total Accepted: 26.7K
13+
* Total Submissions: 65.8K
14+
* Testcase Example: '[-1,2,1,-4]\n1'
15+
*
16+
* 给定一个包括 n 个整数的数组 nums 和
17+
* 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target
18+
* 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
19+
*
20+
* 例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.
21+
*
22+
* 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).
23+
*
24+
*
25+
*/
26+
27+
// @lc code=start
28+
#include <algorithm>
29+
#include <iostream>
30+
#include <vector>
31+
32+
class Solution {
33+
public:
34+
int threeSumClosest(std::vector<int> &nums, int target) {
35+
int minDiff = INT_MAX;
36+
int closest = 0;
37+
sort(nums.begin(), nums.end());
38+
for (int i = 0; i < nums.size(); i++) {
39+
int j = i + 1, k = nums.size() - 1;
40+
while (j < k) {
41+
int threeSum = nums[i] + nums[j] + nums[k];
42+
int diff = abs(target - threeSum);
43+
if (diff == 0) {
44+
return 0;
45+
}
46+
if (diff < minDiff) {
47+
minDiff = diff;
48+
closest = threeSum;
49+
}
50+
if (threeSum > target) {
51+
k--;
52+
} else {
53+
j++;
54+
}
55+
}
56+
}
57+
return closest;
58+
}
59+
};
60+
// @lc code=end
61+
62+
int main(int argc, const char **argv) {
63+
Solution s;
64+
std::vector<int> nums = {2,3,4,5};
65+
auto str = s.threeSumClosest(nums, 17);
66+
std::cout << str << std::endl;
67+
return 0;
68+
}

0 commit comments

Comments
(0)

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