代刷业务网站建设,虹口免费网站制作,大学专业网站,免费商标图案logo题目
https://www.lintcode.com/problem/1827/
有一个长度为 arrLen 的数组#xff0c;开始有一个指针在索引 0 处。
每一步操作中#xff0c;你可以将指针向左或向右移动 1 步#xff0c;或者停在原地#xff08;指针不能被移动到数组范围外#xff09;。
给你两个整数…题目
https://www.lintcode.com/problem/1827/
有一个长度为 arrLen 的数组开始有一个指针在索引 0 处。
每一步操作中你可以将指针向左或向右移动 1 步或者停在原地指针不能被移动到数组范围外。
给你两个整数 steps 和 arrLen 请你计算并返回在恰好执行 steps 次操作以后指针仍然指向索引 0,0 处的方案数。由于答案可能会很大请返回方案数 模 1000000007 后的结果。1≤steps≤5001≤arrLen≤10 ^6样例
样例 1输入
3
2
输出
4
解释
3 步后总共有 4 种不同的方法可以停在索引 0 处。
向右向左不动
不动向右向左
向右不动向左
不动不动不动
样例 2输入
2
4
输出
2
解释
2 步后总共有 2 种不同的方法可以停在索引 0 处。
向右向左
不动不动
样例 3输入
4
2
输出
8参考代码
public class Solution {/*** param steps: steps you can move* param arrLen: the length of the array* return: Number of Ways to Stay in the Same Place After Some Steps*/public int numWays(int steps, int arrLen) {//算法体系课200-201 类似
// int[][] map new int[arrLen1][steps1];
// for (int[] ints : map) {
// Arrays.fill(ints,-1);
// }//return dfs(0,steps,arrLen,map);return f1(steps,arrLen);}public static int f1(int steps,int arrlen){int[][] dp new int[arrlen1][steps1];dp[0][0] 1;for (int rest 1; rest steps ; rest) {for (int x 0; x arrlen ; x) {int wayspick(dp,x-1,arrlen,rest-1);wayspick(dp,x,arrlen,rest-1);wayspick(dp,x1,arrlen,rest-1);dp[x][rest] ways%1000000007;}}return dp[0][steps];}public static int pick(int[][] dp,int x,int arrlen,int rest){if(x0 || xarrlen )return 0;return dp[x][rest];}
}