问:
解: 1.
const numWays = function(n) {
const dp = []
const mod = 1000000007
dp[n] = 1
for (let i = n - 1; i >= 0; i--) {
dp[i] = (dp[i + 1] + (dp[i + 2] ?? 0)) % mod
}
return dp[0]
};
const exist = function(board, word) {
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[0].length; j++) {
if (getRes(i, j, 0)) return true
}
}
return false
function getRes(x, y, len) {
if (x < 0 || y < 0 || x > board.length - 1 || y > board[0].length - 1 || board[x][y] !== word[len]) return false
if (len === word.length - 1) return true
board[x][y] = ''
const res = getRes(x + 1, y, len + 1) || getRes(x - 1, y, len + 1) || getRes(x, y + 1, len + 1) || getRes(x, y - 1, len + 1);
board[x][y] = word[len]
return res
}
};
const movingCount = function(m, n, k) {
const isActive = []
for (let i = 0; i < m; i++) {
isActive[i] = []
}
function getRes(i, j) {
if (i < 0 || j < 0 || i >= m || j >= n || sum(i, j) > k || isActive[i][j]) return 0
isActive[i][j] = true
return getRes(i, j + 1) + getRes(i, j - 1) + getRes(i + 1, j) + getRes(i - 1, j) + 1
}
return getRes(0, 0, false)
function sum(i, j) {
i = i.toString()
j = j.toString()
const arr = i.split('').concat(j.split(''))
return arr.reduce((pre, item) => {
return pre + +item
}, 0)
}
};
const cuttingRope = function(n) {
const dp = []
dp[0] = 1
dp[1] = 1
dp[2] = 1
for (let i = 3; i <= n; i++) {
dp[i] = 0
for (let j = 2; j < i; j++) {
dp[i] = Math.max(Math.max(dp[i - j] * j, j * (i - j)), dp[i])
}
}
return dp[n]
};