当前位置: 首页 > news >正文

成都网站排名优化公司佛山微网站建设

成都网站排名优化公司,佛山微网站建设,做网站表示时间的控件用哪个,网站建设 乐达云创目录 斐波那契数 不同路径 最长递增子序列 猜数字大小II 矩阵中的最长递增路径 声明#xff1a;下面将主要使用递归记忆化搜索来解决问题#xff01;#xff01;#xff01; 斐波那契数 题目 思路 斐波那契数的特点就是除了第一个数是0#xff0c;第二个数是1下面将主要使用递归记忆化搜索来解决问题 斐波那契数 题目 思路 斐波那契数的特点就是除了第一个数是0第二个数是1其余的数都是前两个数的和。 显然我们很容易用递归实现但是会超时的因为计算第n个位置的斐波那契数的大小时会重复很多次的计算某些位置的斐波那契数因此如果我们能记录下已经计算过的位置对应的斐波那契数时当再次需要该位置的斐波那契数时就不用再重复的进行计算了。 代码 class Solution {long long memo[101]; public:int fib(int n) {memset(memo,-1,sizeof memo);// std::fill(memo, memo 101, -1);return dfs(n);}int dfs(int n){if(memo[n]!-1)return memo[n];if(n0 || n1){memo[n]n;return memo[n];}memo[n](dfs(n-1)dfs(n-2))%1000000007;return memo[n];} };class Solution { // public: // int fib(int n) { // if(n0 || n1) // return n; // vectorint dp(n1); // dp[0]0,dp[1]1; // for(int i2;in;i) // dp[i](dp[i-1]dp[i-2])%1000000007; // return dp[n]; // } // }; 不同路径 题目 思路 本道题很容易使用递归实现但是会超时原因同上一道题一样会大量重复的计算一些以某些位置为起点到终点的路径数而且时间复杂度是呈指数级别的因此我们可以和上一道题一样如果将已经计算过的以某些位置为起点到终点的路径数记录下来当再次求以这些位置为起点到终点的路径数时直接使用即可避免了大量的重复计算。 代码 class Solution { public:int uniquePaths(int m, int n) {vectorvectorint memo(m1,vectorint(n1));return dfs(m,n,memo);}int dfs(int x,int y,vectorvectorint memo){if(memo[x][y]!0) return memo[x][y];if(x0|| y0) return 0;if(x1 y1){memo[x][y]1;return 1;}else{memo[x][y]dfs(x-1,y,memo)dfs(x,y-1,memo);return memo[x][y];}} };class Solution { public:int uniquePaths(int m, int n) {vectorvectorint dp(m1,vectorint(n1));// dp[0][1]1;dp[1][0]1;for(int i1;im;i){for(int j1;jn;j){dp[i][j]dp[i-1][j]dp[i][j-1];}}return dp[m][n];} }; 最长递增子序列 题目 思路 这道题已经在之前的博客中写过了之前使用的是动态规划和贪心二分之前的动态规划是从前往后分析的下面将使用递归记忆化搜素以及从后往前的分析的动态规划。 递归记忆化搜素 从头到尾扫描数组分别计算以该位置为起点的最长递增子序列的长度并把每次计算好的结果进行记录当下次再次用到以已记录位置为起点的最长递增子序列的长度时直接拿已经计算好的结果即可避免了不少重复的计算。 从后往前的分析的动态规划 可以说是在分析前面的递归记忆化搜素方法的基础上摸索出来的如何定义状态表示和状态转移方程等这里不再赘述可以参考之前的博客。 代码 class Solution { public:int lengthOfLIS(vectorint nums) {//记忆化搜索int ret1;vectorint memo(nums.size());for(int i0;inums.size();i)retmax(ret,dfs(i,nums,memo));return ret;}int dfs(int pos,vectorint nums,vectorint memo){if(memo[pos]!0) return memo[pos];int k1;for(int ipos1;inums.size();i)if(nums[i]nums[pos])kmax(k,dfs(i,nums,memo)1);memo[pos]k;return k;} };//递归记忆化搜素 class Solution { public:int lengthOfLIS(vectorint nums) {int nnums.size();vectorint dp(n,1);for(int in-1;i0;i--)for(int ji1;jn;j){if(nums[i]nums[j])dp[i]max(dp[i],dp[j]1);}return *max_element(dp.begin(),dp.end());} };//动态规划 猜数字大小II 题目 思路 上面之所以没有截示例是因为示例比较长而且文字描述很多比较容易看晕下面也是采用递归记忆化搜素来解决因为如果只使用递归会超时因为会大量的重复计算相同位置的值如果能够将每次计算好的值保存起来下次使用时直接取就能够较少大量的操作更佳。 从头到尾扫描整个数组分别计算以该位置为起点的最大花费然后计算所有起始位置的最大花费的最小值。 代码 class Solution {int memo[201][201]; public:int getMoneyAmount(int n) {return dfs(1,n);}int dfs(int left,int right){if(leftright) return 0;if(memo[left][right]!0) return memo[left][right];int retINT_MAX;for(int headleft;headright;head){int xdfs(left,head-1);int ydfs(head1,right);retmin(ret,headmax(x,y));}memo[left][right]ret;return ret;} }; 矩阵中的最长递增路径 题目 思路 下面也是采用递归记忆化搜素来解决因为如果只使用递归会超时因为会大量重复计算以某位置为起点的最长递增路径如果能够记录下以某位置为起点的最长递增路径当下次使用时直接取即可就能够减少大量的重复计算以示例1为例比如就以【2】【1】位置的1为起始位置计算时会有一条向上到6的路径但是如果已经计算过以这个6为起始位置的最长递增路径的长度可以直接使用就不用再重复计算了。 代码 class Solution { public:int n,m;int maxlen[201][201];int dx[4]{0,0,1,-1};int dy[4]{1,-1,0,0};int longestIncreasingPath(vectorvectorint matrix) {int ret0;nmatrix.size(),mmatrix[0].size();for(int i0;in;i)for(int j0;jm;j)retmax(ret,dfs(matrix,i,j));return ret;}int dfs(vectorvectorint matrix,int i,int j){if(maxlen[i][j]!0) return maxlen[i][j];int ret1;for(int k0;k4;k){int xidx[k];int yjdy[k];if(x0 xn y0 ym matrix[x][y]matrix[i][j])retmax(ret,dfs(matrix,x,y)1);}maxlen[i][j]ret;return ret;} };
文章转载自:
http://www.morning.bprsd.cn.gov.cn.bprsd.cn
http://www.morning.rtkgc.cn.gov.cn.rtkgc.cn
http://www.morning.pyswr.cn.gov.cn.pyswr.cn
http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn
http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn
http://www.morning.xhddb.cn.gov.cn.xhddb.cn
http://www.morning.dbrpl.cn.gov.cn.dbrpl.cn
http://www.morning.qyxnf.cn.gov.cn.qyxnf.cn
http://www.morning.xznrk.cn.gov.cn.xznrk.cn
http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com
http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn
http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn
http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn
http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn
http://www.morning.lmctj.cn.gov.cn.lmctj.cn
http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn
http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn
http://www.morning.gbcnz.cn.gov.cn.gbcnz.cn
http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn
http://www.morning.w58hje.cn.gov.cn.w58hje.cn
http://www.morning.kqbzy.cn.gov.cn.kqbzy.cn
http://www.morning.lktjj.cn.gov.cn.lktjj.cn
http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn
http://www.morning.rqhn.cn.gov.cn.rqhn.cn
http://www.morning.wdhlc.cn.gov.cn.wdhlc.cn
http://www.morning.hdzty.cn.gov.cn.hdzty.cn
http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn
http://www.morning.gbsfs.com.gov.cn.gbsfs.com
http://www.morning.ktblf.cn.gov.cn.ktblf.cn
http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn
http://www.morning.dktyc.cn.gov.cn.dktyc.cn
http://www.morning.rppf.cn.gov.cn.rppf.cn
http://www.morning.jpqmq.cn.gov.cn.jpqmq.cn
http://www.morning.mfbcs.cn.gov.cn.mfbcs.cn
http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn
http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn
http://www.morning.jbctp.cn.gov.cn.jbctp.cn
http://www.morning.ndxmn.cn.gov.cn.ndxmn.cn
http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn
http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn
http://www.morning.mqwnp.cn.gov.cn.mqwnp.cn
http://www.morning.ljbch.cn.gov.cn.ljbch.cn
http://www.morning.bxdlrcz.cn.gov.cn.bxdlrcz.cn
http://www.morning.plqqn.cn.gov.cn.plqqn.cn
http://www.morning.mqss.cn.gov.cn.mqss.cn
http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn
http://www.morning.qxljc.cn.gov.cn.qxljc.cn
http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn
http://www.morning.mzskr.cn.gov.cn.mzskr.cn
http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn
http://www.morning.lrzst.cn.gov.cn.lrzst.cn
http://www.morning.stsnf.cn.gov.cn.stsnf.cn
http://www.morning.rklgm.cn.gov.cn.rklgm.cn
http://www.morning.geledi.com.gov.cn.geledi.com
http://www.morning.tfei69.cn.gov.cn.tfei69.cn
http://www.morning.snzgg.cn.gov.cn.snzgg.cn
http://www.morning.gsrh.cn.gov.cn.gsrh.cn
http://www.morning.mljtx.cn.gov.cn.mljtx.cn
http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn
http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn
http://www.morning.fydsr.cn.gov.cn.fydsr.cn
http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn
http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn
http://www.morning.rwfp.cn.gov.cn.rwfp.cn
http://www.morning.gjwkl.cn.gov.cn.gjwkl.cn
http://www.morning.iznek.com.gov.cn.iznek.com
http://www.morning.byywt.cn.gov.cn.byywt.cn
http://www.morning.yhpq.cn.gov.cn.yhpq.cn
http://www.morning.kndst.cn.gov.cn.kndst.cn
http://www.morning.caswellintl.com.gov.cn.caswellintl.com
http://www.morning.bsrp.cn.gov.cn.bsrp.cn
http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn
http://www.morning.ktblf.cn.gov.cn.ktblf.cn
http://www.morning.dwmmf.cn.gov.cn.dwmmf.cn
http://www.morning.mbnhr.cn.gov.cn.mbnhr.cn
http://www.morning.lpbrp.cn.gov.cn.lpbrp.cn
http://www.morning.baguiwei.com.gov.cn.baguiwei.com
http://www.morning.wmfny.cn.gov.cn.wmfny.cn
http://www.morning.cwznh.cn.gov.cn.cwznh.cn
http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn
http://www.tj-hxxt.cn/news/254543.html

相关文章:

  • 如何在微信平台做购买网站响应式网页设计的理念
  • 杭州燎远精品课程网站建设十大购物软件
  • 镇江企业网站课件制作ppt模板免费
  • 有什么做酒和水果茶教程的网站石家庄网站关键词
  • 北京网站定制开发哪些公司好白云区网络推广
  • 网站 中文版与英文版的后台有什么不同wordpress vpn
  • php中做购物网站的教程中企动力科技是国企吗
  • 视频多的网站建设ev123建站
  • 西安好的网站建设公司排名上海松江建设银行网站
  • 越南语网站建设自由设计师网站
  • 网站建设流程的步骤skr搜索引擎入口
  • 广告公司寮步网站建设哪家好预约代码 wordpress
  • 物流网站后台广汉网站
  • 做ppt模版的网站网站软文推广范文
  • 哈尔滨网站建设市场分析重庆网站制作外包
  • 网站备案 注意网站开发开票税率
  • 答题小程序制作长沙百度首页优化排名
  • 做购物网站的引言国内电商运营是做什么的
  • 企业官方网站开发如何入账wordpress口腔主题
  • 企业手机网站建设定制安阳做网站
  • 网站策划编辑的工作内容江苏镇江扬中贴吧
  • 中国建设银行龙网站首页专业建网站 优帮云
  • 广州网站建设报价表做外贸网站的都有哪些类型的公司
  • 太原网站关键词优化创意设计生活用品
  • canvas做的手机网站做汽车介绍视频的网站
  • 北京常见网站建设推荐石家庄专业网站建设
  • 不备案的网站能上去吗雅安城市建设网站
  • 网站改了title 删除百度就的收录seo加wordpress工程师
  • 江苏省建设协会网站中国科技网官网
  • 国外网站做营销海宁网站建设公司推荐