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

电脑网站转手机版西部数码网站管理助手 mysql

电脑网站转手机版,西部数码网站管理助手 mysql,重庆市有几个区,网站开发公司php工资文章目录 DP1 斐波那契数列法1#xff1a;递归法2#xff1a;动态规划法3#xff1a;优化空间复杂度 2.分割连接字符串3. 给定一个字符串s和一组单词dict#xff0c;在s中添加空格将s变成一个句子 DP1 斐波那契数列 法1#xff1a;递归 // 递归 #include iostream递归法2动态规划法3优化空间复杂度 2.分割连接字符串3. 给定一个字符串s和一组单词dict在s中添加空格将s变成一个句子 DP1 斐波那契数列 法1递归 // 递归 #include iostream using namespace std;int Fibonacci(int n) {if(n 0) return 0;if(n 1) return 1;return Fibonacci(n - 1) Fibonacci(n - 2); } int main() {int a;while (cin a) { // 注意 while 处理多个 caseint b Fibonacci(a);cout b endl;} }法2动态规划 // DP #include iostream using namespace std;int Fibonacci(int n) {//创建一个数组保存中间状态的解int* F new int[n 1];//初始化F[0] 0; F[1] 1;//状态公式F[i] F[i - 1] F[i - 2];for(int i 2; i n 1; i){F[i] F[i - 1] F[i - 2];}return F[n]; } int main() {int a;while (cin a) { // 注意 while 处理多个 casecout Fibonacci(a) endl;} }法3优化空间复杂度 #include iostream using namespace std;int Fibonacci(int n) {//状态公式F[i] F[i - 1] F[i - 2];//优化空间复杂度 O(n) - O(1)if(n 0) return 0;if(n 1) return 1;int fn 0, f0 0, f1 1;for(int i 2; i n 1; i){fn f0 f1;//更新中间状态f0 f1;f1 fn;}return fn; } int main() {int a;while (cin a) { // 注意 while 处理多个 casecout Fibonacci(a) endl;} }2.分割连接字符串 1、给定一个字符串s和一组单词dict判断s是否可以用空格分割成一个单词序列使得单词序列中所有的单词都是dict中的单词序列可以包含一个或多个单词。 例如: 给定s“leetcode” dict[“leet”, “code”]. 返回true因为leetcode可以被分割成leet code. #include vector #include string #include unordered_set using namespace std;bool wordBreak(string s, unordered_setstring dict) {// 检查输入是否有效if (s.empty() || dict.empty()) {return false;}// 动态规划数组flag[i]表示s的前i个字符是否可以被拆分vectorbool flag(s.length() 1, false);flag[0] true; // 空字符串可以被拆分// 遍历字符串的每个位置for (int i 1; i s.length(); i) {// 从i-1向前遍历到0for (int j i - 1; j 0; j--) {// 如果前j个字符可以被拆分且从j到i的子字符串在字典中if (flag[j] dict.find(s.substr(j, i - j)) ! dict.end()) {flag[i] true;break; // 当前位置可以被拆分跳出内层循环}}}// 返回整个字符串是否可以被拆分return flag[s.length()]; }3. 给定一个字符串s和一组单词dict在s中添加空格将s变成一个句子 这段代码实现了回溯法深度优先搜索DFS来生成所有可能的单词拆分结果。 2、给定一个字符串s和一组单词dict在s中添加空格将s变成一个句子使得句子中的每一个单词都是dict中的单词 返回所有可能的结果 例如给定的字符串s “catsanddog”, dict [“cat”, “cats”, “and”, “sand”, “dog”]. 返回的结果为[“cats and dog”, “cat sand dog”]. #include vector #include string #include unordered_set using namespace std;class Solution { public:vectorstring wordBreak(string s, unordered_setstring dict) {vectorstring result;DFS(s, dict, s.length(), , result);return result;}private:void DFS(const string s, const unordered_setstring dict, int index, string str, vectorstring result) {// 如果索引小于等于0说明已经处理完整个字符串if (index 0) {if (!str.empty()) {// 去掉最后一个多余的空格并将结果加入到结果列表中result.push_back(str.substr(0, str.length() - 1));}return;}// 从当前索引向前遍历寻找可以拆分的单词for (int i index; i 0; i--) {// 检查从i到index的子字符串是否在字典中if (dict.find(s.substr(i, index - i)) ! dict.end()) {// 将当前单词加入到路径中并继续递归处理DFS(s, dict, i, s.substr(i, index - i) str, result);}}} };
http://www.hkea.cn/news/14310276/

相关文章:

  • 全国建设教育联盟统一平台网站绿色风格网站
  • 摄影作品网站排行榜久久信息网
  • 花园设计网站推荐图片制作软件免费版
  • 用vs2008做网站云南建设工程质量监督网站
  • 网站开发必须要用jswordpress 网站地址
  • 有哪些营销型网站推荐ui做自适应网站
  • 怎么找网站做公示做网站为什么要域名 解析绑定
  • 网站托管维护网络开发公司是干什么的
  • 公司网站建设任务书wordpress头像修改
  • 安远做网站深圳知名的网站公司简介
  • 网站域名空间怎么弄啊万州电商网站建设
  • 网站设计怎么学制定 网站改版优化方案
  • 镇江网站建设介绍WordPress修改登录短代码
  • 计算机专业网站开发开题报告建设网站 程序员的提成
  • 网站的验证码是怎么做的竞价托管外包
  • 门户网站建设信息工作讲话wordpress数据表文档
  • 网站建设著作权办网站 哪些许可
  • 黄冈做网站的公司网站开发的调研
  • 网站建设销售找客户话术知名品牌营销案例100例
  • 免费做长图的网站公司网站建设费用 知乎
  • 宝山做手机网站建设营销有哪些基本内容
  • 做动态图片的网站吗专业手机网站建设
  • 做网站时的注册权起到什么作用WordPress与dz用户恭喜
  • 烂网站做竞价行吗宁波城乡建设网站
  • 网站开发系统架构图wordpress点播视频模版
  • 淘客怎么建网站做推广厦门seo优
  • 电视台网站建设方案.docwordpress 热门排行
  • 手机网站策划书2345网址导航下载到桌面
  • 淘宝网站建设h5和手机网站
  • 尖扎网站建设公司装修设计的网站