LeetCode刷題記錄(326-350)
阿新 • • 發佈:2021-11-13
328. 奇偶連結串列
Tag: 連結串列
class Solution { public: ListNode *oddEvenList(ListNode *head) { if (head == nullptr || head->next == nullptr) { return head; } ListNode *odd_h = head, *even_h = head->next; ListNode *p = odd_h, *q = even_h, *prev = nullptr; while (p) { auto nextP = q ? q->next : nullptr; auto nextQ = nextP ? nextP->next : nullptr; p->next = nextP; if (q) { q->next = nextQ; } prev = p; p = p->next; q = q ? q->next : nullptr; } prev->next = even_h; return odd_h; } };
329. 矩陣中的最長遞增路徑
Tag: 動態規劃
class Solution { public: int longestIncreasingPath(vector<vector<int>>& matrix) { vector<vector<int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int m = matrix.size(), n = matrix[0].size(); vector<vector<int>> tmp; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { tmp.push_back({matrix[i][j], i, j}); } } auto myCmpF = [](const vector<int>& a, const vector<int>& b) { return a[0] > b[0]; }; sort(tmp.begin(), tmp.end(), myCmpF); int res = 0; vector<vector<int>> dp(m, vector<int>(n, 1)); for (auto& item : tmp) { int val = item[0], i = item[1], j = item[2]; for (auto& dir : dirs) { int nextI = i + dir[0], nextJ = j + dir[1]; if (nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && matrix[nextI][nextJ] > val) { dp[i][j] = max(dp[i][j], dp[nextI][nextJ] + 1); } } res = max(res, dp[i][j]); } return res; } };
331. 驗證二叉樹的前序序列化
Tag: 二叉樹 棧
class Solution { public: bool isValidSerialization(string preorder) { int n = preorder.size(); int i = 0; stack<int> stk; stk.push(1); while (i < n) { if (stk.empty()) { return false; } if (preorder[i] == ',') { i++; } else if (preorder[i] == '#') { stk.top() -= 1; if (stk.top() == 0) { stk.pop(); } i++; } else { while (i < n && preorder[i] != ',') { i++; } stk.top() -= 1; if (stk.top() == 0) { stk.pop(); } stk.push(2); } } return stk.empty(); } };
class Solution {
public:
bool isValidSerialization(string preorder) {
int n = preorder.size();
int i = 0;
int slots = 1;
while (i < n) {
if (slots == 0) {
return false;
}
if (preorder[i] == ',') {
i++;
} else if (preorder[i] == '#') {
slots--;
i++;
} else {
while (i < n && preorder[i] != ',') {
i++;
}
slots = slots - 1 + 2;
}
}
return slots == 0;
}
};
334. 遞增的三元子序列
Tag: 貪心
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int small = INT32_MAX, mid = INT32_MAX;
for (int n : nums) {
if (n <= small) {
small = n;
} else if (n <= mid) {
mid = n;
} else if (n > mid) {
return true;
}
}
return false;
}
};
337. 打家劫舍 III
Tag: 動態規劃
class Solution {
private:
unordered_map<TreeNode*, int> memo;
public:
int rob(TreeNode* root) {
if (root == nullptr) {
return 0;
}
if (memo.find(root) != memo.end()) {
return memo[root];
}
int robRoot = root->val;
if (root->left) {
robRoot += rob(root->left->left) + rob(root->left->right);
}
if (root->right) {
robRoot += rob(root->right->left) + rob(root->right->right);
}
int noRobRoot = rob(root->left) + rob(root->right);
int ans = max(robRoot, noRobRoot);
memo[root] = ans;
return ans;
}
};
338. 位元位計數
Tag: 動態規劃
class Solution {
public:
vector<int> countBits(int n) {
vector<int> res(n + 1, 0);
for (int i = 1; i < n + 1; i++) {
if ((i & 1) == 0) {
//偶數
res[i] = res[i >> 1];
} else {
//奇數
res[i] = res[i - 1] + 1;
}
}
return res;
}
};
class Solution {
public:
vector<int> countBits(int n) {
vector<int> dp(n + 1, 0);
int highBit = 0;
for (int i = 1; i <= n; i++) {
if ((i & (i - 1)) == 0) {
highBit = i;
}
dp[i] = dp[i - highBit] + 1;
}
return dp;
}
};
343. 整數拆分
Tag: 動態規劃
class Solution {
public:
int integerBreak(int n) {
vector<int> dp(n + 1, 0);
dp[2] = 1;
for (int i = 3; i <= n; i++) {
int tmp = 0;
for (int j = 2; j < i; j++) {
tmp = max(max(tmp, j * (i - j)), j * dp[i - j]);
}
dp[i] = tmp;
}
return dp[n];
}
};