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 c56d345

Browse files
增加部分题目
1 parent be52127 commit c56d345

8 files changed

+284
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* @lc app=leetcode id=416 lang=cpp
3+
*
4+
* [416] Partition Equal Subset Sum
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
// 回溯法 超时
11+
// bool canPartition(vector<int>& nums) {
12+
// int pack1 = nums[0];
13+
// int pack2 = 0;
14+
// return traceback(nums, pack1, pack2, 1);
15+
// }
16+
17+
18+
// bool traceback(const vector<int>& nums, int pack1, int pack2, int i) {
19+
// if (i >= nums.size())
20+
// return pack1 == pack2;
21+
// bool left = traceback(nums, pack1 + nums[i], pack2, i+1);
22+
// if (left)
23+
// return true;
24+
// bool right = traceback(nums, pack1, pack2 + nums[i], i+1);
25+
// if (right)
26+
// return true;
27+
// return false;
28+
// }
29+
30+
31+
32+
bool canPartition(vector<int>& nums) {
33+
int sum = 0;
34+
int n = nums.size();
35+
for (int i = 0; i < n; i++)
36+
{
37+
sum += nums[i];
38+
}
39+
if (sum % 2 != 0) // 不为偶数,定不可二分相同
40+
{
41+
return false;
42+
}
43+
int target = sum / 2;
44+
vector<vector<bool>> dp(n, vector<bool>(target + 1, false)); // dp[i][j] 前i个物品,
45+
for (int i = 0; i < n; i++)
46+
{
47+
// 背包容量为 0 说明装满
48+
dp[i][0] = true;
49+
}
50+
for (int i = 1; i < n; i++)
51+
{
52+
for (int j = 0; j < target + 1; j++)
53+
{
54+
if (j - nums[i] < 0)
55+
{
56+
dp[i][j] = dp[i-1][j];
57+
} else
58+
dp[i][j] = dp[i-1][j] || dp[i-1][j - nums[i]];
59+
}
60+
}
61+
return dp[n-1][target];
62+
}
63+
64+
};
65+
// @lc code=end
66+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode id=518 lang=cpp
3+
*
4+
* [518] Coin Change 2
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int change(int amount, vector<int>& coins) {
11+
vector<int> dp(amount + 1);
12+
// base case
13+
for (int j = 0; j < coins.size(); j++) {
14+
if (coins[j] < amount + 1)
15+
{
16+
dp[coins[j]] = 1;
17+
}
18+
}
19+
for (int i = 1; i < amount + 1; i++)
20+
{
21+
for (int j = 0; j < coins.size(); j++)
22+
{
23+
if (i - coins[j] >= 0)
24+
{
25+
dp[i] += dp[i - coins[j]];
26+
}
27+
}
28+
}
29+
30+
return dp[amount];
31+
}
32+
};
33+
// @lc code=end
34+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode.cn id=518 lang=cpp
3+
*
4+
* [518] 零钱兑换 II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
11+
int change(int amount, vector<int>& coins) {
12+
int n = coins.size();
13+
vector<int> dp(amount + 1);
14+
// base case
15+
dp[0] = 1;
16+
17+
for (int i = 0; i < n; i++)
18+
{
19+
for (int j = coins[i]; j < amount + 1; j++)
20+
{
21+
// 不放入 dp[j] 为自身,不改变组合数
22+
// 放入则 加上 j-coins[i] 的组合数
23+
dp[j] += dp[j-coins[i]];
24+
}
25+
}
26+
27+
return dp[amount];
28+
}
29+
};
30+
// @lc code=end
31+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode.cn id=202 lang=cpp
3+
*
4+
* [202] 快乐数
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool isHappy(int n) {
11+
unordered_set<int> s;
12+
vector<int> table = { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81};
13+
int num = 0;
14+
s.insert(n);
15+
while (1)
16+
{
17+
while (n)
18+
{
19+
num += table[n % 10];
20+
n = n / 10;
21+
}
22+
23+
if (num == 1)
24+
return true;
25+
if (s.find(num) != s.end())
26+
return false;
27+
n = num;
28+
num = 0;
29+
s.insert(n);
30+
}
31+
}
32+
};
33+
// @lc code=end
34+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode.cn id=242 lang=cpp
3+
*
4+
* [242] 有效的字母异位词
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool isAnagram(string s, string t) {
11+
if (s.size() != t.size())
12+
return false;
13+
vector<int> table(26); // 只有小写字母
14+
for (int i = 0; i < s.size(); i++)
15+
{
16+
table[s[i] - 'a'] += 1;
17+
}
18+
for (int i = 0; i < t.size(); i++)
19+
{
20+
if (table[t[i] - 'a'] <= 0)
21+
return false;
22+
table[t[i] - 'a'] -= 1;
23+
}
24+
25+
return true;
26+
}
27+
};
28+
// @lc code=end
29+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=349 lang=cpp
3+
*
4+
* [349] 两个数组的交集
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
11+
int MAX_ITEM = 1000;
12+
unordered_set<int> result_set;
13+
vector<bool> table(MAX_ITEM + 1, false);
14+
for (size_t i = 0; i < nums1.size(); i++)
15+
{
16+
table[nums1[i]] = true;
17+
}
18+
for (size_t i = 0; i < nums2.size(); i++)
19+
{
20+
if (table[nums2[i]])
21+
{
22+
result_set.insert(nums2[i]);
23+
}
24+
25+
}
26+
return vector<int>(result_set.begin(), result_set.end());
27+
}
28+
};
29+
// @lc code=end
30+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode.cn id=383 lang=cpp
3+
*
4+
* [383] 赎金信
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
bool canConstruct(string ransomNote, string magazine) {
11+
if (ransomNote.size() > magazine.size())
12+
return false;
13+
vector<int> table(26);
14+
for (char c : magazine)
15+
{
16+
table[c - 'a'] += 1;
17+
}
18+
for (char c : ransomNote)
19+
{
20+
if (table[c - 'a'] <= 0)
21+
return false;
22+
table[c - 'a'] -= 1;
23+
}
24+
return true;
25+
}
26+
};
27+
// @lc code=end
28+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode.cn id=454 lang=cpp
3+
*
4+
* [454] 四数相加 II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
11+
unordered_map<int, int> m;
12+
for (int a : nums1)
13+
{
14+
for (int b : nums2)
15+
{
16+
m[a + b]++;
17+
}
18+
}
19+
int res = 0;
20+
21+
for (int c : nums3)
22+
{
23+
for (int d : nums4)
24+
{
25+
res += m[0 - (c + d)];
26+
}
27+
}
28+
return res;
29+
}
30+
};
31+
// @lc code=end
32+

0 commit comments

Comments
(0)

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