Skip to content

Commit 0c937ac

Browse files
committed
更新leetcode 321, 322, 324, 326, 327, 328, 329, 330, 331, 332, 334, 337, 338
1 parent b64aa63 commit 0c937ac

15 files changed

+94
-71
lines changed

cpp/leetcode/320. generalized-abbreviation.cpp renamed to cpp/leetcode/321. create-maximum-number.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "public.h"
22

3-
//56ms, 87.25%
3+
//48ms, 91.61%
44
//分治
55
//一定是从nums1中取i个数, 从nums2中取k-i个数
66

cpp/leetcode/322. coin-change.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,39 @@
11
#include "public.h"
22

3-
//68ms, 79.30%
3+
//40ms, 97.13%
44
//DP problem
55
//每一个当前的金额都可以用coins中的任意一个加上之前的金额数量获得, 然后扣出最小值即可
66

77
class Solution {
88
public:
99
int coinChange(vector<int>& coins, int amount) {
10+
sort(coins.begin(), coins.end());
11+
vector<int> dp(amount + 1, 0x7f7f7f7f);
1012

11-
vector<int> dp(amount + 1, 0);
13+
dp[0] = 0;
1214

1315
for (int i = 1; i <= amount; ++i)
1416
{
1517
//挨个凑
16-
int mintemp = INT_MAX;
17-
bool haschance = false;
18+
int mintemp = 0x7f7f7f7f;
1819
for (int c = 0; c < coins.size(); ++c)
1920
{
2021
int history = i - coins[c];
21-
if ((history >= 0) && (dp[history] != -1))
22-
{
23-
mintemp = min(mintemp, dp[history] + 1);
24-
haschance = true;
25-
}
22+
if (history < 0) break;
23+
else mintemp = min(mintemp, dp[history] + 1);
2624
}
27-
if (haschance) dp[i] = mintemp;
28-
else dp[i] = -1;
25+
dp[i] = mintemp;
2926
}
30-
return dp[amount];
27+
return dp[amount] < 0x7f7f7f7f ? dp[amount] : -1;
3128
}
3229
};
3330

3431
/*
3532
int main()
3633
{
3734
Solution* s = new Solution();
38-
vector<int> coins = { 2 };
39-
cout << s->coinChange(coins, 3);
35+
vector<int> coins = { 1,2,5 };
36+
cout << s->coinChange(coins, 11);
4037
return 0;
4138
}
4239
*/

cpp/leetcode/324. wiggle-sort-ii.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "public.h"
22

3-
//108ms, 77.78%
4-
//我看了评论, 还不如先排序然后插入数据
3+
//88ms, 91.70%
4+
//O(nlogn), 理论时间复杂度可以O(n), 待优化
5+
//先排序然后插入数据
56

67
class Solution {
78
public:
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#include "public.h"
22

3-
//16ms, 95.20%
4-
//¿´´úÂë°É
3+
//16ms, 88.65%
4+
//使用极大的3的幂次来操作
55

66
class Solution {
77
public:
88
bool isPowerOfThree(int n) {
9-
if (n <= 0) return false;
10-
11-
int thefuck3 = (int)pow(3, 19);
12-
13-
return (thefuck3%n == 0);
9+
//int thefuck3=(int)pow(3,19);
10+
return (n > 0) && (1162261467 % n == 0);
1411
}
1512
};

cpp/leetcode/327. count-of-range-sum.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//TLE
44
//典型的线段树
55
//目前是无懒惰标记的线段树, 时间复杂度O(nnlogn) 比暴力还大...
6+
//待更久之后研究, 线段树难度太大
67

78
class Solution {
89
private:
@@ -80,10 +81,12 @@ class Solution {
8081
}
8182
};
8283

84+
/*
8385
int main()
8486
{
8587
Solution* s = new Solution();
8688
vector<int> nums = {-2,5,-1};
8789
cout << s->countRangeSum(nums, -2, 2);
8890
return 0;
8991
}
92+
*/

cpp/leetcode/328. odd-even-linked-list.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
#include "listnode.h"
22

3-
//24ms, 83.79%
4-
//easy problem, but code may be difficult
3+
//20ms, 89.16%
4+
//Ęąźä¸´ÔÓśČO(n)żŐźä¸´ÔÓśČO(1)
55

6-
/**
7-
* Definition for singly-linked list.
8-
* struct ListNode {
9-
* int val;
10-
* ListNode *next;
11-
* ListNode(int x) : val(x), next(NULL) {}
12-
* };
13-
*/
146
class Solution {
157
public:
168
ListNode* oddEvenList(ListNode* head) {

cpp/leetcode/329. longest-increasing-path-in-a-matrix.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include "public.h"
22

3-
//4ms, 98.91%
3+
//44ms, 94.30%
44
//DFS加记忆化
5-
//搜过的地方存储结果, 如果某个未搜过的地方搜到了搜过的地方, 可以直接相加, 这是因为那个搜过的地方的路径绝不可能与当前走过的路径相交(总路径是递增路径)
5+
//搜过的地方存储结果, 如果某个未搜过的地方搜到了搜过的地方, 可以直接相加,
6+
// 这是因为那个搜过的地方的路径绝不可能与当前走过的路径相交(总路径是递增路径)
67

78
class Solution {
89
private:
9-
int DFS(const vector<vector<int>>& matrix, vector<vector<int>>& memory, int row, int col)
10+
int DFS(vector<vector<int>> const& matrix, vector<vector<int>>& memory, int row, int col)
1011
{
1112
if (memory[row][col] != -1) return memory[row][col];
1213

@@ -44,10 +45,12 @@ class Solution {
4445
}
4546
};
4647

48+
/*
4749
int main()
4850
{
4951
Solution* s = new Solution();
5052
vector<vector<int>> matrix = { {3,4,5},{3,2,6},{2,2,1} };
5153
cout << s->longestIncreasingPath(matrix);
5254
return 0;
5355
}
56+
*/

cpp/leetcode/330. patching-array.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "public.h"
22

3-
//8ms, 94.59%
3+
//4ms, 100%
44
/*
55
参考至作者:Faber99
66
链接:https ://leetcode-cn.com/problems/two-sum/solution/an-yao-qiu-bu-qi-shu-zu-tan-lan-by-faber99/

cpp/leetcode/331. verify-preorder-serialization-of-a-binary-tree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "BinaryTree.h"
22

3-
//8ms, 87.56%
3+
//4ms, 97.21%
44
//在未结束之前, 读取到的节点数量应大于叶子节点数量
55
//结束时叶子数量应等于节点数量
66

cpp/leetcode/332. Reconstruct Itinerary.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
#include "public.h"
22

3-
//28ms, 100%
4-
5-
//Graph algorithm, carry out a route that can traverse all the tickets, and is the minimun (string sort)
6-
7-
//problem say that there must be a solution!
8-
//because we need to carry out the minimun (string sort), so we should use multiset<string> to store the next airports
9-
10-
//In DFS: we traverse all the next airports and use ano DFS recursively
11-
//note that if one DFS not include all the routes, this is not bug, because we "traverse" all the next airports
12-
//note that in the whole recursive precedure, the graph is modified anywhere!
3+
//24ms, 96.25%
4+
//图, DFS, 倒过来生成行程, 然后reverse它
5+
//对某个机场的下一个目的地, 按照优先队列排序
136

147
class Solution {
158
private:
@@ -31,11 +24,11 @@ class Solution {
3124
unordered_map<string, priority_queue<string, vector<string>, greater<string>>> graph;
3225

3326
//generate the graph
34-
for (auto ticket : tickets)
27+
for (auto& ticket : tickets)
3528
graph[ticket[0]].push(ticket[1]);
3629

3730
DFS("JFK", graph);
3831
reverse(res.begin(), res.end());
3932
return res;
4033
}
41-
};
34+
};

0 commit comments

Comments
 (0)