Skip to content

Commit f3f7eea

Browse files
author
wsz
committed
添加之前写的题目
1 parent f646d5f commit f3f7eea

33 files changed

+1035
-0
lines changed

1.两数之和.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @before-stub-for-debug-begin
2+
#include <vector>
3+
#include <string>
4+
5+
using namespace std;
6+
// @before-stub-for-debug-end
7+
8+
/*
9+
* @lc app=leetcode.cn id=1 lang=cpp
10+
*
11+
* [1] 两数之和
12+
*/
13+
#include <bits/stdc++.h>
14+
using namespace std;
15+
// @lc code=start
16+
class Solution {
17+
public:
18+
vector<int> twoSum(vector<int>& nums, int target) {
19+
unordered_map<int,int> hashTable;
20+
vector<int> ans;
21+
int n = nums.size();
22+
int val;
23+
for (int i = 0; i < n; i++)
24+
{
25+
val = target - nums[i];
26+
if (hashTable.count(val))
27+
return {i, hashTable[val]};
28+
hashTable[nums[i]] = i;
29+
}
30+
31+
return move(ans);
32+
}
33+
};
34+
// @lc code=end
35+

10.正则表达式匹配.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @lc app=leetcode.cn id=10 lang=cpp
3+
*
4+
* [10] 正则表达式匹配
5+
*/
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
// @lc code=start
9+
class Solution {
10+
public:
11+
bool isMatch(string s, string p) {
12+
13+
}
14+
};
15+
// @lc code=end
16+

1143.最长公共子序列.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode.cn id=1143 lang=cpp
3+
*
4+
* [1143] 最长公共子序列
5+
*/
6+
7+
// @lc code=start
8+
#include <bits/stdc++.h>
9+
using namespace std;
10+
class Solution
11+
{
12+
public:
13+
vector<vector<int>> mem;
14+
int dfs(string &s1, string &s2, int a, int b, int &ans, int curLen)
15+
{
16+
if (s1.size() == a || s2.size() == b)
17+
{
18+
if (curLen > ans)
19+
ans = curLen;
20+
return curLen;
21+
}
22+
23+
if (!mem[a][b])
24+
{
25+
if (s1[a] == s2[b])
26+
mem[a + 1][b + 1] = dfs(s1, s2, a + 1, b + 1, ans, curLen + 1);
27+
else
28+
{
29+
mem[a + 1][b] = dfs(s1, s2, a + 1, b, ans, curLen);
30+
mem[a][b + 1] = dfs(s1, s2, a, b + 1, ans, curLen);
31+
}
32+
}
33+
34+
return mem[a][b];
35+
}
36+
int longestCommonSubsequence(string text1, string text2)
37+
{
38+
int ans = 0;
39+
mem = vector<vector<int>>(text1.size(), vector<int>(text2.size(), 0));
40+
dfs(text1, text2, 0, 0, ans, 0);
41+
return ans;
42+
}
43+
};
44+
// @lc code=end

119.杨辉三角-ii.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* @lc app=leetcode.cn id=119 lang=cpp
3+
*
4+
* [119] 杨辉三角 II
5+
*/
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
// @lc code=start
9+
class Solution
10+
{
11+
public:
12+
13+
vector<int> getRow(int rowIndex)
14+
{
15+
16+
}
17+
};
18+
// @lc code=end

120.三角形最小路径和.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @lc app=leetcode.cn id=120 lang=cpp
3+
*
4+
* [120] 三角形最小路径和
5+
*/
6+
7+
#include <bits/stdc++.h>
8+
using namespace std;
9+
// @lc code=start
10+
class Solution
11+
{
12+
public:
13+
int minimumTotal(vector<vector<int>> &dp)
14+
{
15+
int n1 = dp.size() - 1,n2,row,nextRow,column;
16+
for (row = dp.size() - 2; row >= 0; --row)
17+
{
18+
nextRow = row + 1;
19+
n2 = dp[nextRow].size() - 1;
20+
21+
for (column = 0; column < n2; ++column)
22+
{
23+
dp[row][column] = min(dp[nextRow][column], dp[nextRow][column + 1]) + dp[row][column];
24+
}
25+
}
26+
return dp[0][0];
27+
}
28+
};
29+
// @lc code=end
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @lc app=leetcode.cn id=122 lang=cpp
3+
*
4+
* [122] 买卖股票的最佳时机 II
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int maxProfit(vector<int>& prices) {
11+
12+
}
13+
};
14+
// @lc code=end
15+

14.最长公共前缀.cpp

Whitespace-only changes.

1446.连续字符.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=1446 lang=cpp
3+
*
4+
* [1446] 连续字符
5+
*/
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
// @lc code=start
9+
class Solution
10+
{
11+
public:
12+
int dfs(string &s, int len){
13+
14+
}
15+
int maxPower(string s)
16+
{
17+
int n = s.size();
18+
vector<int> dp(n + 1, 1);
19+
--n;
20+
for (int i = 0; i < n; ++i)
21+
{
22+
if (s[i] == s[i + 1])
23+
dp[i + 1] = dp[i] + 1;
24+
if (dp[i + 1] > dp[n])
25+
dp[n] = dp[i + 1];
26+
}
27+
return dp[n];
28+
}
29+
};
30+
// @lc code=end

15.三数之和.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @lc app=leetcode.cn id=15 lang=cpp
3+
*
4+
* [15] 三数之和
5+
*/
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
// @lc code=start
9+
class Solution
10+
{
11+
public:
12+
vector<vector<int>> threeSum(vector<int> &nums)
13+
{
14+
int n = nums.size(), t, l, r, s;
15+
vector<vector<int>> ans;
16+
sort(nums.begin(), nums.end());
17+
for (int i = 0; i < n; ++i)
18+
{
19+
t = -nums[i];
20+
l = i + 1;
21+
r = n - 1;
22+
while (l < r)
23+
{
24+
s = nums[l] + nums[r];
25+
if (s > t)
26+
--r;
27+
else if (s < t)
28+
++l;
29+
else
30+
{
31+
ans.emplace_back(vector{nums[i], nums[l], nums[r]});
32+
33+
while (l < r && nums[l] == ans.back()[1])
34+
++l;
35+
while (l < r && nums[r] == ans.back()[2])
36+
--r;
37+
}
38+
}
39+
while (i + 1 < n && nums[i] == nums[i + 1])
40+
++i;
41+
}
42+
43+
return move(ans);
44+
}
45+
};
46+
// @lc code=end

151.翻转字符串里的单词.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=151 lang=cpp
3+
*
4+
* [151] 翻转字符串里的单词
5+
*/
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
// @lc code=start
9+
class Solution
10+
{
11+
public:
12+
string reverseWords(string s)
13+
{
14+
int n = s.size();
15+
int l = 0, r = n - 1;
16+
17+
while (s[l] == ' ')
18+
++l;
19+
while (s[r] == ' ')
20+
--r;
21+
string ans(r - l + 1, 0);
22+
int cur = 0;
23+
while (l <= r)
24+
{
25+
26+
}
27+
return move(ans);
28+
}
29+
};
30+
// @lc code=end

1816.截断句子.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @lc app=leetcode.cn id=1816 lang=cpp
3+
*
4+
* [1816] 截断句子
5+
*/
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
// @lc code=start
9+
class Solution {
10+
public:
11+
string truncateSentence(string s, int k) {
12+
istringstream in(s);
13+
ostringstream out;
14+
string word;
15+
while(!(in>>word).eof() && k){
16+
if (k)
17+
out << word << " ";
18+
else
19+
out << word;
20+
--k;
21+
}
22+
23+
return out.str();
24+
}
25+
};
26+
// @lc code=end
27+

198.打家劫舍.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @lc app=leetcode.cn id=198 lang=cpp
3+
*
4+
* [198] 打家劫舍
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int rob(vector<int>& nums) {
11+
12+
}
13+
};
14+
// @lc code=end
15+

0 commit comments

Comments
 (0)