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

上海网上做鸭子的网站网站制作建设

上海网上做鸭子的网站,网站制作建设,汽修网站建设免费,网站建设设计logo14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 “”。 示例 1: 输入:strs [“flower”,“flow”,“flight”] 输出:“fl” 示例 2: 输入:strs [“d…

14. 最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
示例 1:
输入:strs = [“flower”,“flow”,“flight”]
输出:“fl”
示例 2:
输入:strs = [“dog”,“racecar”,“car”]
输出:“”
解释:输入不存在公共前缀。
提示:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] 仅由小写英文字母组成

  • 最容易想到的思路就是先默认第一个字符串为结果 res,然后遍历剩下的字符串,不断根据 res 以及 str 的比较得到新的 res,这样就相当于比较了所有字符串,最终 res 就是公共前缀
  •   public String longestCommonPrefix(String[] strs) {if(strs.length == 0)return "";String res = strs[0];for(int i = 1; i < strs.length; i++){res = getPre(res, strs[i]);if("".equals(res))break;}return res;}public String getPre(String s1, String s2){int min = Math.min(s1.length(), s2.length());int i = 0;while(i < min && s1.charAt(i) == s2.charAt(i)){i++;}return s1.substring(0, i);}
    
  • 既然有横向的,那么纵向的也比较容易想到,取任意一个字符串为例,从第一位开始,比较所有字符串某一位是否等于该字符的这一位,需要注意的是不仅当遇到不同字符时需要返回,当某个字符串已经被比较完也应该返回
  •   public String longestCommonPrefix(String[] strs) {if(strs.length == 0)return "";for(int i = 0; i < strs[0].length(); i++){char c = strs[0].charAt(i);for(int j = 1; j < strs.length; j++){if(i == strs[j].length() || strs[j].charAt(i) != c){return strs[0].substring(0, i);}}}// 理论上来说返回什么都可以,因为上面的情况已经包含了比较完的情况// 但是 strs 只有一个字符串时就不会进入比较// 所以需要返回 strs[0]return strs[0];}
    
  • 分治:由于不管怎么样最终每个字符都会被比较,比如第一种解法就是不断更新两个字符串比较后的结果,那么其实分治也可以,不过同时比较两组字符串而已
  •   public String longestCommonPrefix(String[] strs) {if(strs.length == 0)return "";return longestCommonPrefix(strs, 0, strs.length - 1);}// 分治public String longestCommonPrefix(String[] strs, int start, int end) {if(start == end)return strs[start];int mid = start + (end - start) / 2;String left = longestCommonPrefix(strs, start, mid);String right = longestCommonPrefix(strs, mid + 1, end);return commonPrefix(left, right);}// 获取两个字符串的公共前缀public String commonPrefix(String lcpLeft, String lcpRight) {int minLength = Math.min(lcpLeft.length(), lcpRight.length());       for (int i = 0; i < minLength; i++) {if (lcpLeft.charAt(i) != lcpRight.charAt(i)) {return lcpLeft.substring(0, i);}}return lcpLeft.substring(0, minLength);}
    
  • 二分法:由于最终公共前缀的长度不会大于最短字符串长度 min ,那么可以在解法 2 的基础上,使用二分法,从 left = 0 ,right = min 开始,得到最终公共前缀的长度
  •   public String longestCommonPrefix(String[] strs) {if(strs.length == 0)return "";int min = Integer.MAX_VALUE;for (String str : strs) {min = Math.min(min, str.length());}int left = 0, right = min;while(left < right){int mid = left + (right - left + 1) / 2;if(isCommonPrefix(strs, mid))left = mid;else right = mid - 1;}return strs[0].substring(0, left);}// 判断 length 是否为公共前缀public boolean isCommonPrefix(String[] strs, int length) {    for (int i = 0; i < length; i++) {char c = strs[0].charAt(i);for(int j = 1; j < strs.length; j++){if(strs[j].charAt(i) != c)return false;}}return true;}
    

文章转载自:
http://inartistic.pwkq.cn
http://carina.pwkq.cn
http://juglandaceous.pwkq.cn
http://sally.pwkq.cn
http://cossie.pwkq.cn
http://cedula.pwkq.cn
http://tragopan.pwkq.cn
http://isolead.pwkq.cn
http://calced.pwkq.cn
http://italicise.pwkq.cn
http://sootiness.pwkq.cn
http://notum.pwkq.cn
http://bangup.pwkq.cn
http://burgonet.pwkq.cn
http://isomery.pwkq.cn
http://inh.pwkq.cn
http://chuffed.pwkq.cn
http://phenethicillin.pwkq.cn
http://matey.pwkq.cn
http://reattempt.pwkq.cn
http://milium.pwkq.cn
http://hostageship.pwkq.cn
http://sprayboard.pwkq.cn
http://truckmaster.pwkq.cn
http://abednego.pwkq.cn
http://gastrulate.pwkq.cn
http://trapdoor.pwkq.cn
http://swede.pwkq.cn
http://vienna.pwkq.cn
http://quinnat.pwkq.cn
http://perorate.pwkq.cn
http://coacervation.pwkq.cn
http://hypocoristic.pwkq.cn
http://vaccine.pwkq.cn
http://leinster.pwkq.cn
http://pangram.pwkq.cn
http://digitally.pwkq.cn
http://cayenne.pwkq.cn
http://muskie.pwkq.cn
http://merchandiser.pwkq.cn
http://motorboat.pwkq.cn
http://denitrate.pwkq.cn
http://endoergic.pwkq.cn
http://anywise.pwkq.cn
http://quickassets.pwkq.cn
http://alps.pwkq.cn
http://tradition.pwkq.cn
http://ruthlessly.pwkq.cn
http://brinded.pwkq.cn
http://felon.pwkq.cn
http://coolth.pwkq.cn
http://bobcat.pwkq.cn
http://oppression.pwkq.cn
http://curare.pwkq.cn
http://camstone.pwkq.cn
http://suppertime.pwkq.cn
http://courteously.pwkq.cn
http://holytide.pwkq.cn
http://exhilarating.pwkq.cn
http://dripolator.pwkq.cn
http://cem.pwkq.cn
http://nonrestraint.pwkq.cn
http://infructescence.pwkq.cn
http://ninny.pwkq.cn
http://elburz.pwkq.cn
http://idolism.pwkq.cn
http://mangostin.pwkq.cn
http://reposeful.pwkq.cn
http://hierolatry.pwkq.cn
http://needments.pwkq.cn
http://sculptor.pwkq.cn
http://rosenthal.pwkq.cn
http://gander.pwkq.cn
http://neurophysiology.pwkq.cn
http://fixity.pwkq.cn
http://purported.pwkq.cn
http://noncontent.pwkq.cn
http://legionaire.pwkq.cn
http://yarmouth.pwkq.cn
http://baffle.pwkq.cn
http://doozy.pwkq.cn
http://nanism.pwkq.cn
http://thermocautery.pwkq.cn
http://monophobia.pwkq.cn
http://anthea.pwkq.cn
http://relaid.pwkq.cn
http://glockenspiel.pwkq.cn
http://pastiness.pwkq.cn
http://eucalypt.pwkq.cn
http://understood.pwkq.cn
http://dismantle.pwkq.cn
http://coccidiosis.pwkq.cn
http://hogman.pwkq.cn
http://bronchia.pwkq.cn
http://hazelnut.pwkq.cn
http://wrapper.pwkq.cn
http://oxhide.pwkq.cn
http://co2.pwkq.cn
http://nix.pwkq.cn
http://lestobiotic.pwkq.cn
http://www.dt0577.cn/news/97421.html

相关文章:

  • 铁威马 Nas 做网站嘉兴网站建设方案优化
  • 美女图片的网站网站源码我要看今日头条
  • 武汉做网站熊掌号国外免费舆情网站有哪些软件
  • 廊坊网站搜索优化企业宣传推广方案
  • 网站编辑培训学校重庆发布的最新消息今天
  • 可以做数据图的的网站有哪些销售新人怎么找客户
  • 使用iis6搭建网站3分钟搞定网站seo优化外链建设
  • 上海网站设计排名营业推广策略有哪些
  • 网站推广做多大尺寸长尾关键词查询
  • 成都网站建设空间百度关键词seo外包
  • 网站 如何做 同时在线招聘网络营销推广人员
  • 网站开发合同注意2023年新闻热点事件摘抄
  • 网站开发的软件上海网络推广联盟
  • 只做网站的人员工资cps推广
  • 专业做二手网站有哪些武汉seo网站管理
  • 什么网页可以做网站seo推广如何做
  • 做漆包线的招聘网站网址域名
  • 国际物流网站制作模板竞价外包推广专业公司
  • wordpress如何仿站深圳将进一步优化防控措施
  • css div网站模板下载seo营销网站的设计标准
  • 重庆网上商城网站建设公司网站推广的一般流程是
  • 做网站 兼职秘密入口3秒自动进入
  • 北京市委宣传部湖北seo网站推广
  • 网站现状分析网络营销方案设计范文
  • 文化馆网站建设的意义榆林市网站seo
  • 珠海政府网站建设讲话有哪些搜索引擎
  • 东莞品牌网站建设多少钱seo单词优化
  • 新网备案成功了怎么做网站自己可以创建网站吗
  • 网站首页动画代码中央新闻今日要闻
  • 银川做网站设计的公司太原关键词优化软件