零标题:算法(leetode,附思维导图+全解)300题(38)出场顺序致力于写极简但完整的题解(算法)的博主1题目描述2解决方案概述(思维导图)3所有解决方案1方案11)代码://方案1“类似斐波那契数列,使用2个变量”//思路://1)状态初始化//2)循环处理n-1次,连续处理pre和now值//2.1)根据pre值计算now值//2.2)赋值now值给pre值,设置now为'',然后进行next循环处理-->根据pre值计算now值//3)返回结果的pre值varcountAndSay=function(n){//1)状态初始化letpre='1',now='';//2)循环处理n-1次,不断处理pre和now值for(leti=0;i根据pre值,计算now值pre=now;现在='';}//3)返回结果pre值returnpre;}2场景21)代码://场景2“递归-普通法则”varcountAndSay=function(n){constdfs=(n)=>{//1)递归退出if(n===1){return'1';}//2)递归体returndfs(n-1).replace(/(\d)\1*/g,item=>`${item.length}${item[0]}`);};returndfs(n);}3Scheme31)code://scheme3"recursion-concisemethod"varcountAndSay=function(n){if(n===1){return'1';}}returncountAndSay(n-1).replace(/(\d)\1*/g,item=>`${item.length}${item[0]}`);}4方案41)代码://方案4“正则+循环法”//思路://1)状态初始化//2)循环处理:不断结合正则匹配情况,使用replace函数更新resStr//2.1)注(核心):'111221'。match(/(\d)\1*/g),output['111','22','1']//看下面的代码就容易多了,item是'111','22','1'//3)返回结果resStrvarcountAndSay=function(n){//1)状态初始化letresStr='1';//2)循环处理:不断结合正则匹配情况,使用replace函数对resStrUpdatefor(leti=1;i`${item.length}${item[0]}`);}//3)返回结果resStrreturnresStr;};5方案51)代码://方案5《JS安装方式:正则+递归-->1行代码》//思路://countAndSay(3)=countAndSay(2).replace(...)//=countAndSay(1).replace(...).replace(...)//=1.replace(...).replace(...).replace(...)varcountAndSay=function(n){//注意:理解下面的注释,代码很简单//countAndSay(3)=countAndSay(2).replace(...)//=countAndSay(1).replace(...).replace(...)//=1.replace(...).replace(...).replace(...)返回n==1?'1':countAndSay(n-1).replace(/(\d)\1*/g,item=>`${item.length}${item[0]}`);};