文章目录
刷题地址: https://leetcode.cn/problem-list/xb9nqhhg/
剑指 Offer 12. 矩阵中的路径
// dfs 边界, 遍历记得 标记 回溯,防止重复走
class Solution {
public:
int n, m;
bool exist(vector<vector<char>>& board, string word) {
n = board.size(), m = board[0].size();
for(int i = 0;i < n;i ++ )
for(int j = 0;j < m;j ++ )
{
if(dfs(board, i, j, word, 0)) return true;
}
return false;
}
bool dfs(vector<vector<char>>& board, int x, int y, string& word, int u)
{
if(board[x][y] != word[u]) return false;
if(u == word.size() - 1) return true;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
char t = board[x][y];
board[x][y] = '#'; // 遍历过了.., 标记一下
for(int i = 0;i < 4;i ++ )
{
int a = x + dx[i], b = y + dy[i];
if(a < 0 || a >= n || b < 0 || b >= m) continue;
if(dfs(board, a, b, word, u + 1)) return true;
}
board[x][y] = t;
return false;
}
};
剑指 Offer 14- I. 剪绳子【DP】
class Solution {
public:
int integerBreak(int n) {
/*动态规划, from 官方题解:https://leetcode.cn/problems/integer-break/solutions/352875/zheng-shu-chai-fen-by-leetcode-solution/
dp[i] : 将i拆分成 k个整数,且乘积最大, ans = dp[n]
假设拆分成j,1 <= j <= 9
dp[i] = max( j * (i - j), j * dp[i - j] )
边界条件: dp[0] = dp[1] = 0
时间复杂度:O(n)
*/
vector<int> dp(n + 1);
dp[0] = dp[1] = 0;
for(int i = 2;i <= n;i ++ ){
int curMax = 0;
for(int j = 1; j <= i;j ++ )
{
curMax = max(curMax, max( j * (i - j), j * dp[i - j] ));
}
dp[i] = curMax;
}
return dp[n];
}
};
剑指 Offer 14- II. 剪绳子 II【贪心】
class Solution {
public:
int cuttingRope(int n) {
/**
题目理解:和 上一题 一样,只是数据范围 变大了 , “因为数据范围变得比较大时,long已经不足以去存储中间结果的状态”
贪心:
当 n >= 5: (n - 3) * 3 = 3n - 9 > n, 说明 n >= 5时,拆成 3+x 更优
当 n == 4: 等同于 2
当 n == 3 或2 时,比如6: 3 * 3 > 2 * 2 * 2, 说明 还是拆成3 更优
*/
int mod = 1e9 + 7;
if(n <= 3) return 1 * (n - 1);
int res = 1;
if(n % 3 == 1) res *= 4, n -= 4;
if(n % 3 == 2) res *= 2, n -= 2;
while(n) res = (1LL * res * 3) % mod, n -= 3;
return res;
}
};
剑指 Offer 15. 二进制中1的个数
class Solution {
public:
int hammingWeight(uint32_t n) {
int res = 0;
for(int i = 0;i < 32;i ++ ){
if(n >> i & 1) res ++ ;
}
return res;
}
};
文章目录
剑指 Offer 16. 数值的整数次方
// 快速幂, 注意负数,溢出..
class Solution {
public:
double qmi(double a, long long k) // debug: -2147483648 不能转 正数..
{
double res = 1;
while(k){
if(k & 1) res = res * a;
a = a * a;
k >>= 1;
}
return res;
}
double myPow(double x, int n) {
bool f = false;
long long my_n = n;
if(n < 0) f = true, my_n = -my_n; // debug: negation of -2147483648 cannot be represented in type 'int';
double res = qmi(x, my_n);
if(f) res = 1 / res;
return res;
}
};
剑指 Offer 17. 打印从1到最大的n位数
// 大数问题,没那么简单...
class Solution {
public:
vector<int> printNumbers(int n) {
int k = pow(10, n);
vector<int> res;
for(int i = 1;i < k;i ++ ) res.push_back(i);
return res;
}
};
剑指 Offer 18. 删除链表的节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
auto dummy = new ListNode(-1);
dummy->next = head;
auto p = dummy;
while(p->next){
if(p->next->val == val){
p->next = p->next->next;
break;
}
p = p->next;
}
return dummy->next;
}
};