41. 缺失的第一个正数
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int len = nums.size();
for (int i = 0; i < len; ++ i) {
while (nums[i] > 0 && nums[i] <= len && nums[nums[i] - 1] != nums[i])
swap(nums[i], nums[nums[i] - 1]);
}
for (int i = 0; i < len; ++ i)
if (nums[i] != i + 1) return i + 1;
return len + 1;
}
};
42. 接雨水
class Solution {
public:
int trap(vector<int>& height) {
stack<int> st;
int ans = 0;
for (int i = 0; i < height.size(); ++ i) {
while (st.size() && height[st.top()] < height[i]) {
int x = st.top();st.pop();
if (!st.size()) break;
int l = st.top();
int h = min(height[i], height[l]) - height[x];
int w = i - l - 1;
ans += h * w;
}
st.push(i);
}
return ans;
}
};
43. 字符串相乘
class Solution {
public:
string multiply(string num1, string num2) {
if (num1 == "0" || num2 == "0") return "0";
vector<int> ve(num1.size() + num2.size());
for (int i = num1.size() - 1; i >= 0; -- i)
for (int j = num2.size() - 1; j >= 0; -- j) {
int val = ve[i + j + 1] + (num1[i] - '0') * (num2[j] - '0');
ve[i + j] += val / 10;
ve[i + j + 1] = val % 10;
}
string ans;
for (int i = 0; i < ve.size(); ++ i) {
if (!i && !ve[i]) continue;
ans.push_back(ve[i] + '0');
}
return ans;
}
};
44. 通配符匹配
class Solution {
public:
bool isMatch(string s, string p) {
int n = s.size(), m = p.size();
vector<vector<int>> f(n + 1, vector<int>(m + 1));
f[0][0] = 1;
for (int i = 1; i <= m; ++ i)
if (p[i - 1] == '*') f[0][i] = 1;
else break;
for (int i = 1; i <= n; ++ i)
for (int j = 1; j <= m; ++ j) {
switch(p[j - 1]) {
case '?': f[i][j] = f[i - 1][j - 1]; break;
case '*': f[i][j] = f[i - 1][j] || f[i][j - 1] || f[i - 1][j - 1]; break;
default: f[i][j] = (s[i - 1] == p[j - 1]) && f[i - 1][j - 1];
}
}
return f[n][m];
}
};
45. 跳跃游戏 II
class Solution {
public:
int jump(vector<int>& nums) {
int n = nums.size() - 1;
vector<int> f(n + 1, 0x3f3f3f3f);
f[0] = 0;
for (int i = 0; i <= n; ++ i) {
for (int j = 1; j <= nums[i] && i + j <= n; ++ j)
f[i + j] = min (f[i + j], f[i] + 1);
}
return f[n];
}
};
46. 全排列
class Solution {
public:
void dfs (int cur, vector<int>& vis, vector<int>& temp, vector<int>& num, vector<vector<int>>& ans) {
if (cur > num.size()) {
ans.push_back(temp);
return;
}
for (int i = 1; i <= num.size(); ++ i) {
if (vis[i - 1]) continue;
vis[i - 1] = 1;
temp.push_back(num[i - 1]);
dfs (cur + 1, vis, temp, num, ans);
temp.pop_back();
vis[i - 1] = 0;
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> vis(nums.size(), 0);
vector<int> temp;
dfs (1, vis, temp, nums, ans);
return ans;
}
};
47. 全排列 II
class Solution {
public:
void dfs (int cur, vector<int>& vis, vector<int>& temp, vector<int>& num, vector<vector<int>>& ans) {
if (cur > num.size()) {
ans.push_back(temp);
return;
}
for (int i = 1; i <= num.size(); ++ i) {
if (vis[i - 1]) continue;
if (i != 1 && num[i - 1] == num[i - 2] && !vis[i - 2]) {
continue;
}
vis[i - 1] = 1;
temp.push_back(num[i - 1]);
dfs (cur + 1, vis, temp, num, ans);
temp.pop_back();
vis[i - 1] = 0;
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> vis(nums.size(), 0);
vector<int> temp;
sort(nums.begin(), nums.end());
dfs (1, vis, temp, nums, ans);
return ans;
}
};
48. 旋转图像
class Solution {
public:
void dfs (int x1, int x2, vector<vector<int>>& m) {
if (x2 - x1 < 1) return;
int n = m.size() - 1;
for (int i = x1; i < x2; ++ i) {
int x = x1, y = i;
int temp = m[x][y];
m[x][y] = m[n - y][x];
m[n - y][x] = m[n - x][n - y];
m[n - x][n - y] = m[y][n - x];
m[y][n - x] = temp;
}
dfs (x1 + 1, x2 - 1, m);
}
void rotate(vector<vector<int>>& matrix) {
dfs (0, matrix.size() - 1, matrix);
}
};
49. 字母异位词分组
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
map<string, vector<string>> mp;
for (auto& i: strs) {
string t = i;
sort(t.begin(), t.end());
mp[t].push_back(i);
}
vector<vector<string>> ans;
for (auto& [x, y]: mp) {
ans.push_back(y);
}
return ans;
}
};
50. Pow(x, n)
class Solution {
public:
double myPow(double x, int n) {
long f = n < 0 ? -1 : 1, y = abs(n);
double ans = 1, t = x;
if (f == -1) t = 1 / t;
while (y) {
if (y & 1) ans *= t;
t *= t;
y >>= 1;
}
return ans;
}
};
|