LeetCode刷題記錄(401-425)
阿新 • • 發佈:2021-11-10
401. 二進位制手錶
Tag: 列舉
class Solution { public: vector<string> readBinaryWatch(int turnedOn) { vector<string> res; for (int i = 0; i < 12; i++) { for (int j = 0; j < 60; j++) { if (count1(i) + count1(j) == turnedOn) { res.push_back(to_string(i) + ":" + (j < 10 ? "0" + to_string(j) : to_string(j))); } } } return res; } int count1(int n) { int cnt = 0; while (n) { cnt++; n = n & (n - 1); } return cnt; } };
402. 移掉 K 位數字
Tag: 單調棧
class Solution { public: string removeKdigits(string num, int k) { vector<int> stk; for (int i = 0; i < num.size(); i++) { while (!stk.empty() && k && stk.back() > num[i]) { k--; stk.pop_back(); } stk.push_back(num[i]); } while (k--) { stk.pop_back(); } string res; bool flag = true; for (int i = 0; i < stk.size(); i++) { if (flag && stk[i] == '0') { continue; } flag = false; res += stk[i]; } return res == "" ? "0" : res; } };
406. 根據身高重建佇列
Tag: 排序
class Solution { public: vector<vector<int>> reconstructQueue(vector<vector<int>>& people) { vector<vector<int>> res; sort(people.begin(), people.end(), [](const vector<int>& u, const vector<int>& v) { if (u[0] == v[0]) { return u[1] < v[1]; } return u[0] > v[0]; }); for (int i = 0; i < people.size(); i++) { res.insert(res.begin() + people[i][1], people[i]); } return res; } };
413. 等差數列劃分
Tag: 動態規劃
class Solution {
public:
int numberOfArithmeticSlices(vector<int>& nums) {
int n = nums.size();
if (n < 3) {
return 0;
}
vector<int> dp(n, 0);
int ans = 0;
for (int i = 2; i < n; i++) {
if (2 * nums[i - 1] == nums[i - 2] + nums[i]) {
dp[i] = dp[i - 1] + 1;
ans += dp[i];
}
}
return ans;
}
};
416. 分割等和子集
Tag: 動態規劃
class Solution {
public:
bool canPartition(vector<int>& nums) {
int total = 0;
int n = nums.size();
if (n < 2) {
return false;
}
int maxNum = 0;
for (int i = 0; i < n; i++) {
total += nums[i];
maxNum = max(maxNum, nums[i]);
}
if (total & 1) {
return false;
}
int target = total / 2;
if (maxNum > target) {
return false;
}
vector<vector<bool>> dp(n, vector<bool>(target + 1, false));
for (int i = 0; i < n; i++) {
dp[i][0] = true;
}
dp[0][nums[0]] = true;
for (int i = 1; i < n; i++) {
for (int j = 1; j <= target; j++) {
if (j >= nums[i]) {
dp[i][j] = dp[i - 1][j] | dp[i - 1][j - nums[i]];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[n - 1][target];
}
};
417. 太平洋大西洋水流問題
Tag: 回溯
class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
int rows = heights.size(), cols = heights[0].size();
vector<vector<bool>> P(rows, vector<bool>(cols, false));
vector<vector<bool>> A(rows, vector<bool>(cols, false));
for (int i = 0; i < rows; i++) {
dfs(heights, P, INT32_MIN, i, 0, rows, cols);
dfs(heights, A, INT32_MIN, i, cols - 1, rows, cols);
}
for (int i = 0; i < cols; i++) {
dfs(heights, P, INT32_MIN, 0, i, rows, cols);
dfs(heights, A, INT32_MIN, rows - 1, i, rows, cols);
}
vector<vector<int>> res;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (P[i][j] && A[i][j]) {
res.push_back({i, j});
}
}
}
return res;
}
void dfs(vector<vector<int>>& mat, vector<vector<bool>>& visited, int prev, int i, int j, int rows, int cols) {
if (i < 0 || i >= rows || j < 0 || j >= cols || visited[i][j] || prev > mat[i][j]) {
return;
}
visited[i][j] = true;
dfs(mat, visited, mat[i][j], i + 1, j, rows, cols);
dfs(mat, visited, mat[i][j], i - 1, j, rows, cols);
dfs(mat, visited, mat[i][j], i, j + 1, rows, cols);
dfs(mat, visited, mat[i][j], i, j - 1, rows, cols);
}
};