lc 41 ~ 50
原地哈希
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for(int i = 0; i < n; i++)
while(nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]){
int temp = nums[nums[i] - 1];
nums[nums[i] - 1] = nums[i];
nums[i] = temp;
}
for(int i = 0; i < n; i++)
if(nums[i] != i + 1)
return i + 1;
return n + 1;
}
};
单调栈
class Solution {
public:
int trap(vector<int>& height) {
stack<int> stk;
int res = 0, len = height.size();
for(int i = 0; i < len; i++){
int last = 0;
while(stk.size() && height[stk.top()] <= height[i]){
res += (height[stk.top()] - last) * (i - stk.top() - 1);
last = height[stk.top()];
stk.pop();
}
if(stk.size()) res += (i - stk.top() - 1) * (height[i] - last);
stk.push(i);
}
return res;
}
};
高精度乘法
class Solution {
public:
string multiply(string num1, string num2) {
if(num1 == "0" || num2 == "0") return "0";
int len1 = num1.length(), len2 = num2.length();
vector<int> a, b;
vector<int> c(len1 + len2);
string ans;
for(int i = num1.length() - 1; i >= 0; i--) a.push_back(num1[i] - '0');
for(int i = num2.length() - 1; i >= 0; i--) b.push_back(num2[i] - '0');
for(int i = 0; i < num1.length(); i++)
for(int j = 0; j < num2.length(); j++)
c[i + j] += a[i] * b[j];
int t = 0;
for(int i = 0; i < len1 + len2; i++){
t += c[i];
c[i] = t % 10;
t /= 10;
}
int k = c.size() - 1;
while(k > 0 && !c[k]) k--;
for(int j = k; j >= 0; j--) ans += c[j] + '0';
return ans;
}
};
DP,自己写的,虽然找了很久bug
class Solution {
public:
bool isMatch(string s, string p) {
s = ' ' + s, p = ' ' + p;
int n = s.size(), m = p.size();
vector<vector<bool>> dp(n, vector<bool>(m));
dp[0][0] = true;
int k = 1;
while(k < m && p[k] == '*') dp[0][k++] = true;
for(int i = 1; i < n; i++)
for(int j = 1; j < m; j++){
if(p[j] != '?' && p[j] != '*')
dp[i][j] = dp[i - 1][j - 1] && (p[j] == s[i]);
else if(p[j] == '?'){
dp[i][j] = dp[i - 1][j - 1];
}
else{
dp[i][j] = dp[i][j - 1] || dp[i - 1][j];
}
}
return dp[n - 1][m - 1];
}
};
y总版,码风很值得学习
class Solution {
public:
bool isMatch(string s, string p) {
int n = s.size(), m = p.size();
s = ' ' + s, p = ' ' + p;
vector<vector<bool>> dp(n + 1, vector<bool>(m + 1));
dp[0][0] = true;
for(int i = 0; i <= n; i++)
for(int j = 1; j <= m; j++){
if(p[j] == '*')
dp[i][j] = dp[i][j - 1] || i && dp[i - 1][j];
else
dp[i][j] = (p[j] == s[i] || p[j] == '?') && i && dp[i - 1][j - 1];
}
return dp[n][m];
}
};
贪心,正向查找可到达的最大位置
class Solution {
public:
int jump(vector<int>& nums) {
int end = 0, maxPos = 0, step = 0, len = nums.size();
for(int i = 0; i < len - 1; i++){
maxPos = max(maxPos, i + nums[i]);
if(i == end){
end = maxPos;
step++;
}
}
return step;
}
};
DP
class Solution {
public:
int jump(vector<int>& nums) {
int len = nums.size();
vector<int> dp(len);
for(int i = 1, j = 0; i < len; i++){
while(j + nums[j] < i) j++;
dp[i] = dp[j] + 1;
}
return dp[len - 1];
}
};
简单DFS
class Solution {
public:
vector<vector<int>> ans;
vector<int> path;
bool st[10];
vector<vector<int>> permute(vector<int>& nums) {
dfs(nums, 0);
return ans;
}
void dfs(vector<int>& nums, int u){
if(u == nums.size()){
ans.push_back(path);
}
for(int i = 0; i < nums.size(); i++){
if(!st[i]){
st[i] = true;
path.push_back(nums[i]);
dfs(nums, u + 1);
st[i] = false;
path.pop_back();
}
}
}
};
DFS + set去重(效率低)
class Solution {
public:
vector<vector<int>> ans;
set<vector<int>> s;
bool st[10];
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<int> path(nums.size());
dfs(nums, 0, path);
return ans;
}
void dfs(vector<int>& nums, int u, vector<int>& path){
if(u == nums.size()){
if(s.find(path) == s.end()){
ans.push_back(path);
s.insert(path);
}
return;
}
for(int i = 0; i < nums.size(); i++){
if(!st[i]){
st[i] = true;
path[u] = nums[i];
dfs(nums, u + 1, path);
st[i] = false;
}
}
}
};
巧妙的去重方法,排序
class Solution {
public:
vector<vector<int>> ans;
vector<int> path;
vector<bool> st;
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
path = vector<int>(nums.size());
st = vector<bool>(nums.size());
dfs(nums, 0);
return ans;
}
void dfs(vector<int>& nums, int u){
if(u == nums.size()){
ans.push_back(path);
return;
}
for(int i = 0; i < nums.size(); i++){
if(!st[i]){
if(i && nums[i - 1] == nums[i] && !st[i - 1]) continue;
st[i] = true;
path[u] = nums[i];
dfs(nums, u + 1);
st[i] = false;
}
}
}
};
脑筋急转弯
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int n = matrix.size();
for(int i = 0; i < n; i++)
for(int j = 0; j < i; j++)
swap(matrix[i][j], matrix[j][i]);
for(int i = 0; i < n; i++)
for(int l = 0, r = n - 1; l < r; l++, r--)
swap(matrix[i][l], matrix[i][r]);
}
};
哈希表
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> hash;
vector<vector<string>> ans;
for(auto& s : strs){
string temp = s;
sort(temp.begin(), temp.end());
hash[temp].push_back(s);
}
for(auto& item : hash) ans.push_back(item.second);
return ans;
}
};
快速幂
class Solution {
public:
double myPow(double x, int n) {
typedef long long LL;
bool is_minus = false;
double ans = 1;
if(n < 0) is_minus = true;
for(LL k = abs((LL)n); k; k >>= 1){
if(k & 1) ans *= x;
x *= x;
}
if(is_minus) ans = 1 / ans;
return ans;
}
};
|