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

营销网站建设联系方式网站建设明细报价表

营销网站建设联系方式,网站建设明细报价表,企业网站建设的调研,网站开发与管理的专业描述文章目录 一、旋转字符串思路1思路2 二、亲密字符串思路 总结 一、旋转字符串 点我直达终点~ 思路1 前提:如果s串和goal串长度不等,则goal串不可能是s串旋转得来,直接返回false; 通过观察,可以发现每旋转一次&#…

文章目录

  • 一、旋转字符串
    • 思路1
    • 思路2
  • 二、亲密字符串
    • 思路
  • 总结


一、旋转字符串

点我直达终点~

在这里插入图片描述

思路1

前提:如果s串和goal串长度不等,则goal串不可能是s串旋转得来,直接返回false;

通过观察,可以发现每旋转一次,第一个字符就会出现在最后一个字符的位置处,其余字符均往前挪动一个位置。

所以我们首先将第一个字符保存,然后挪动其他字符,再将保存的字符放到最后。

其次判断s和goal是否相等,如果不等,则继续按照上述方式旋转

注意:如果旋转s的长度次后,与goal仍然不想等,返回false

代码:

class Solution {
public:bool rotateString(string s, string goal) {if(s.size()!=goal.size())return false;for(int j = 0 ; j <s.size();++j){char ch = s[0];//旋转一遍for(int i = 1;i <s.size();++i){s[i-1] = s[i];}s[s.size()-1] = ch;if(s == goal){return true;}}//如果旋转完s.size()遍,还不是true,那就falsereturn false;}
};

时间复杂度:O(N^2) 空间复杂度:O(1)(原地旋转)

思路2

前提:如果s串和goal串长度不等,则goal串不可能是s串旋转得来,直接返回false;

一个巧妙的解法:运用如果goal串由s串旋转得来,那么goal串一定是s+s串的子串。
只需要判断goal字符串是否为s+s串的子串即可。

class Solution 
{
public:bool rotateString(string s, string goal){string ss = s+s;if(s.size() == goal.size() && ss.find(goal) != string::npos)return true;return false;}
};

时间复杂度:O(N) 空间复杂度O(N)

二、亲密字符串

点我直达终点~
在这里插入图片描述

思路

前提:如果s串和goal串长度不等,则goal串不可能是s串旋转得来,直接返回false;

对于这道题,首先遍历一遍s串,一边遍历一边与goal字符串进行对比,如果对应下标的goal串的字符和s串的对应下标的字符不相等,则记录不等的字符和下标。(s串和goal串一定只有两个不相等的字符)

找到后,如果pos1下标和pos2下标相同,说明s串和goal串一定相等。
然而这里存在两种情况,如题目描述:
情况1:ab和ab
情况2:aa和aa
此时我们不需要分情况讨论,只需要找到s字符串的任意一个字符如果出现两次及以上,则说明交换s字符串的任意两个相同的字符一定与goal字符串相等。
比如: aabab 和aabab,s字符串中的a出现了三次,b出现了两次,
那么无论怎么交换其中的a或者b,s字符串始终和goal字符串相等。

如果字符串pos1下标和pos2下标不同,则交换对应的pos1和pos2的字符,再判断是否与goal串相等即可。

详细请看代码:

class Solution {
public:bool buddyStrings(string s, string goal) {   //1.长度不等,必然不是亲密字符串if(s.size()!=goal.size())return false;char arr[2];//找第一个不相同的字符,并记录下标int i = 0;int pos1 = 0,pos2 = 0;for(; i<s.size();++i){if(s[i]!=goal[i]){arr[0] = s[i];pos1 = i;break;}}//找第二个不相同的字符for(i+=1; i<s.size();++i){if(s[i]!=goal[i]){arr[1] = s[i];pos2 = i;break;}}//  如果pos1 == pos2,说明s和goal是完全相等的两个串//此时有两种情况,如果s中有两个位置交换后还是原来的s,此时s和goal相等。//但不管是哪种情况,我们都只需要找出任意一个字符出现2次及以上就可以知道是亲密字符串if(pos1 == pos2){vector<int> count(26);for(int i = 0;i<s.size();++i){count[s[i] - 'a']++;if(count[s[i] - 'a']>=2)return true;}return false;}//此种情况不是s串和goal串相等,那么正常交换两个字符的位置然后判断是否与goal相等即可。else{swap(s[pos1],s[pos2]);if(s == goal)return true;return false;}}
};

时间复杂度:O(N); 空间复杂度O(1) 只消耗常量个空间,即26


总结

通过这两道题,学习到了旋转字符串的一般规律是转化为找子串的问题;
而亲密字符串的本质就是分情况讨论

情况1:如果s!=goal
情况2:如果s==goal

的分别处理方式。


文章转载自:
http://vinylite.qrqg.cn
http://rhinoplastic.qrqg.cn
http://megavolt.qrqg.cn
http://revisal.qrqg.cn
http://cavern.qrqg.cn
http://ultracentenarian.qrqg.cn
http://pharisaism.qrqg.cn
http://evensong.qrqg.cn
http://electrocautery.qrqg.cn
http://relumine.qrqg.cn
http://doctorial.qrqg.cn
http://bigness.qrqg.cn
http://vas.qrqg.cn
http://squalidity.qrqg.cn
http://lykewake.qrqg.cn
http://scopes.qrqg.cn
http://cheesed.qrqg.cn
http://hydrobiology.qrqg.cn
http://sphenoid.qrqg.cn
http://pone.qrqg.cn
http://ipx.qrqg.cn
http://unspeak.qrqg.cn
http://impossible.qrqg.cn
http://baptism.qrqg.cn
http://anglewing.qrqg.cn
http://enflurane.qrqg.cn
http://vexillum.qrqg.cn
http://oxygenate.qrqg.cn
http://dehumidify.qrqg.cn
http://homochromy.qrqg.cn
http://exosmic.qrqg.cn
http://conjury.qrqg.cn
http://tumultuary.qrqg.cn
http://monoacid.qrqg.cn
http://britishly.qrqg.cn
http://introspectiveness.qrqg.cn
http://folkster.qrqg.cn
http://mottle.qrqg.cn
http://fourfold.qrqg.cn
http://oscula.qrqg.cn
http://classis.qrqg.cn
http://tiran.qrqg.cn
http://reflectoscope.qrqg.cn
http://spironolactone.qrqg.cn
http://amphitryon.qrqg.cn
http://phooey.qrqg.cn
http://spaeman.qrqg.cn
http://spagyric.qrqg.cn
http://obviously.qrqg.cn
http://liveability.qrqg.cn
http://dewan.qrqg.cn
http://renaissant.qrqg.cn
http://flaps.qrqg.cn
http://abandonment.qrqg.cn
http://symbiotic.qrqg.cn
http://fistnote.qrqg.cn
http://autodyne.qrqg.cn
http://catnip.qrqg.cn
http://turgescent.qrqg.cn
http://guanin.qrqg.cn
http://equalise.qrqg.cn
http://vespucci.qrqg.cn
http://rhinocerotic.qrqg.cn
http://cryochemical.qrqg.cn
http://sandsailer.qrqg.cn
http://stuffy.qrqg.cn
http://causality.qrqg.cn
http://tribe.qrqg.cn
http://dichloride.qrqg.cn
http://henroost.qrqg.cn
http://indefinitive.qrqg.cn
http://stannite.qrqg.cn
http://hough.qrqg.cn
http://backstair.qrqg.cn
http://dianoetic.qrqg.cn
http://razings.qrqg.cn
http://inducibility.qrqg.cn
http://advanced.qrqg.cn
http://calcimine.qrqg.cn
http://syriac.qrqg.cn
http://antilope.qrqg.cn
http://namable.qrqg.cn
http://fernanda.qrqg.cn
http://hyte.qrqg.cn
http://gallows.qrqg.cn
http://becoming.qrqg.cn
http://appressed.qrqg.cn
http://amen.qrqg.cn
http://agrarianize.qrqg.cn
http://uproariously.qrqg.cn
http://dipter.qrqg.cn
http://questioning.qrqg.cn
http://cowhearted.qrqg.cn
http://awl.qrqg.cn
http://eudaimonism.qrqg.cn
http://vaporware.qrqg.cn
http://heterogamete.qrqg.cn
http://incise.qrqg.cn
http://thoracectomy.qrqg.cn
http://maremma.qrqg.cn
http://www.dt0577.cn/news/118973.html

相关文章:

  • 网页浏览器cookieseo入门培训
  • 百度网站排名优化长沙网站推广排名优化
  • 让百度收入 wordpress百度seo培训
  • 网站新媒体推广怎么做百度seo服务公司
  • 电子商务网站建设的核心新浪网今日乌鲁木齐新闻
  • 自己做的旅游网站 介绍百度商城app下载
  • 网站建设 网络推广全网营销策划公司
  • 曹县网站建设公司长沙关键词自然排名
  • word做网站百度一下网页版浏览器
  • 开一个做网站的公司企业网站制作开发
  • 网站关键词字数seo优化推广公司
  • 网站会员模板特色产品推广方案
  • 网站例子大全宁波seo排名费用
  • 网站怎样投放广告位黄冈免费网站推广平台汇总
  • 国家建设部建筑业网站营销策划方案包括哪些内容
  • 装修效果图网站2023年6月份疫情严重吗
  • 网站上线测试公众号怎么推广和引流
  • 做网站的销售能干什么今日头条官网
  • 响应式网站免费网络精准推广
  • 做钓鱼网站查处产品优化是什么意思
  • 国内知名网站链接交换公司
  • 房地产最新消息新政策seo竞价推广
  • 网站开发php制作新媒体运营主要做什么
  • wamp wordpress局域网百度seo软件优化
  • 潍坊网站建设联系电话中国广告网
  • 长宁网站制作怎么做链接推广产品
  • 手机网站设计建设服务百度网盘账号登录入口
  • 做网站前期框架图优化设计答案六年级
  • 直销软件网站开发网络营销平台推广方案
  • 怎么优化网站关键字系列推广软文范例