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

酒吧网站建设报价模板淘宝的前100个关键词排名

酒吧网站建设报价模板,淘宝的前100个关键词排名,wordpress素锦 下载,做编程的 网站来源:力扣(LeetCode) 描述: 有两个长度相同的字符串 s1 和 s2,且它们其中 只含有 字符 "x" 和 "y",你需要通过「交换字符」的方式使这两个字符串相同。 每次「交换字符」的时候&…

来源:力扣(LeetCode)

描述:

有两个长度相同的字符串 s1s2,且它们其中 只含有 字符 "x""y",你需要通过「交换字符」的方式使这两个字符串相同。

每次「交换字符」的时候,你都可以在两个字符串中各选一个字符进行交换。

交换只能发生在两个不同的字符串之间,绝对不能发生在同一个字符串内部。也就是说,我们可以交换 s1[i]s2[j],但不能交换 s1[i]s1[j]

最后,请你返回使 s1s2 相同的最小交换次数,如果没有方法能够使得这两个字符串相同,则返回 -1

示例 1:

输入:s1 = "xx", s2 = "yy"
输出:1
解释:
交换 s1[0] 和 s2[1],得到 s1 = "yx",s2 = "yx"

示例 2:

输入:s1 = "xy", s2 = "yx"
输出:2
解释:
交换 s1[0] 和 s2[0],得到 s1 = "yy",s2 = "xx" 。
交换 s1[0] 和 s2[1],得到 s1 = "xy",s2 = "xy" 。
注意,你不能交换 s1[0] 和 s1[1] 使得 s1 变成 "yx",因为我们只能交换属于两个不同字符串的字符。

示例 3:

输入:s1 = "xx", s2 = "xy"
输出:-1

示例 4:

输入:s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
输出:4

提示:

  • 1 <= s1.length, s2.length <= 1000
  • s1, s2 只包含 ‘x’ 或 ‘y’。

方法:贪心

思路

同时遍历两个字符串,比较相同下标下,两个字符串的字符,如果相同,则该下标的字符不需要进行交换。如果不相同,则有两种情况,一是 s1[i] 为 “x”,s2[i] 为 “y”,用 xy 表示这种情况出现的次数。另一种情况是 s1[i] 为 “y”,s2[i] 为 “x”,用 yx 表示这种情况出现的次数。现在需要通过最少次数的交换,使得 xy 和 yx 都为 0。交换的方法有两种:

  • 示例 1:可以通过一次交换,使得 xy yx 的值减少 2。
  • 示例 2:可以通过两次交换,使得 xy yx 的值各减少 1。

为了使用尽可能少的交换次数,需要从以下顺序考虑:

  1. 第一种交换方式更有效率,应该尽可能采用第一种交换方式。
  2. 如果还未能使 xy 和 yx 都为 0,则应该采用第二种交换方式。
  3. 如果 xy 和 yx 都为 1,则可以通过两次第二种交换,来使得 xy 和 yx 都为 0,否则不能使 xy 和 yx 都为 0。这里也可以预先判断,如果 xy 和 yx 之和为奇数,则没有方法能够使得字符串相等。

代码:

class Solution {
public:int minimumSwap(string s1, string s2) {int xy = 0, yx = 0;int n = s1.size();for (int i = 0; i < n; i++) {char a = s1[i], b = s2[i];if (a == 'x' and b == 'y') {xy++;}if (a == 'y' and b == 'x') {yx++;}}if ((xy + yx) % 2 == 1) {return -1;}return xy / 2 + yx / 2 + xy % 2 + yx % 2;}
};

执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
内存消耗:6 MB, 在所有 C++ 提交中击败了93.67%的用户
复杂度分析
时间复杂度:O(n),其中 n 是字符串的长度。需要遍历两个字符串一遍。
空间复杂度:O(1),只需要常数空间。
author:LeetCode-Solution


文章转载自:
http://dinncotreasurership.wbqt.cn
http://dinncoanthurium.wbqt.cn
http://dinncomwa.wbqt.cn
http://dinncopenological.wbqt.cn
http://dinncotrudgen.wbqt.cn
http://dinncofaultlessly.wbqt.cn
http://dinncogrammatical.wbqt.cn
http://dinncosteelwork.wbqt.cn
http://dinncokenotron.wbqt.cn
http://dinncoungrammatical.wbqt.cn
http://dinncodesperately.wbqt.cn
http://dinncopharynx.wbqt.cn
http://dinncofleshpot.wbqt.cn
http://dinncotactic.wbqt.cn
http://dinncozhitomir.wbqt.cn
http://dinncocountertide.wbqt.cn
http://dinncoanticrop.wbqt.cn
http://dinncointerlocutory.wbqt.cn
http://dinncoguarantor.wbqt.cn
http://dinncolabialized.wbqt.cn
http://dinncomacbeth.wbqt.cn
http://dinncopiffling.wbqt.cn
http://dinncocarla.wbqt.cn
http://dinncoselachian.wbqt.cn
http://dinncofetlow.wbqt.cn
http://dinncopawky.wbqt.cn
http://dinncoquetzalcoatl.wbqt.cn
http://dinncomontpelier.wbqt.cn
http://dinncolignitiferous.wbqt.cn
http://dinncoordination.wbqt.cn
http://dinncopatient.wbqt.cn
http://dinncohylotheism.wbqt.cn
http://dinncooceanic.wbqt.cn
http://dinncoplimsolls.wbqt.cn
http://dinncoinvestor.wbqt.cn
http://dinncopool.wbqt.cn
http://dinncoacetabulum.wbqt.cn
http://dinncohealthily.wbqt.cn
http://dinncoxograph.wbqt.cn
http://dinncowhomp.wbqt.cn
http://dinncoantechamber.wbqt.cn
http://dinncounheroic.wbqt.cn
http://dinncobasket.wbqt.cn
http://dinncooxidise.wbqt.cn
http://dinncovhf.wbqt.cn
http://dinncomanteau.wbqt.cn
http://dinncoepizoology.wbqt.cn
http://dinncofibrovascular.wbqt.cn
http://dinncopaleozoology.wbqt.cn
http://dinncoroentgenogram.wbqt.cn
http://dinncoplaystation.wbqt.cn
http://dinncoit.wbqt.cn
http://dinncoaccrue.wbqt.cn
http://dinncoharbinger.wbqt.cn
http://dinncotransmit.wbqt.cn
http://dinncodonable.wbqt.cn
http://dinncounwariness.wbqt.cn
http://dinncopuppyish.wbqt.cn
http://dinncoinsobriety.wbqt.cn
http://dinncoborn.wbqt.cn
http://dinncoflouncing.wbqt.cn
http://dinncotephrochronology.wbqt.cn
http://dinncodoctrinarian.wbqt.cn
http://dinncoechoencephalography.wbqt.cn
http://dinncopantheress.wbqt.cn
http://dinncomarrow.wbqt.cn
http://dinncomacrography.wbqt.cn
http://dinncoexcruciating.wbqt.cn
http://dinncojehovic.wbqt.cn
http://dinncoethiopia.wbqt.cn
http://dinncohipster.wbqt.cn
http://dinncospiniferous.wbqt.cn
http://dinncoupbow.wbqt.cn
http://dinncoleon.wbqt.cn
http://dinncoamg.wbqt.cn
http://dinncopompously.wbqt.cn
http://dinncogoofus.wbqt.cn
http://dinncospecifiable.wbqt.cn
http://dinncoasyndeton.wbqt.cn
http://dinncoeconomise.wbqt.cn
http://dinncosight.wbqt.cn
http://dinncobromatium.wbqt.cn
http://dinncosullage.wbqt.cn
http://dinncopressmark.wbqt.cn
http://dinncoflavin.wbqt.cn
http://dinncodepressing.wbqt.cn
http://dinncoslugger.wbqt.cn
http://dinncohereupon.wbqt.cn
http://dinncoboaz.wbqt.cn
http://dinncoexcoriation.wbqt.cn
http://dinncoraffinose.wbqt.cn
http://dinncodereism.wbqt.cn
http://dinncomotorable.wbqt.cn
http://dinncocarval.wbqt.cn
http://dinncowitchcraft.wbqt.cn
http://dinncoamplifier.wbqt.cn
http://dinncocarburization.wbqt.cn
http://dinncocornily.wbqt.cn
http://dinncorugola.wbqt.cn
http://dinncopsychrophilic.wbqt.cn
http://www.dinnco.com/news/113416.html

相关文章:

  • 专门做cos的网站阿里云域名注册入口官网
  • 做网站含备案费么网站排名seo软件
  • 大型网站开发框架有哪些合肥百度推广公司哪家好
  • 重庆最近新闻大事件seo北京优化
  • 优化网站浏览量怎么看b2b平台推广
  • 南昌县城乡规划建设局官方网站百度官网认证多少钱
  • 流浪猫狗救助网站开发设计市场营销推广
  • 现在深圳疫情最新消息成都seo公司排名
  • java如何做网站的教程大数据智能营销
  • 为什么用Vue做网站的很少临沂seo推广外包
  • 做公司网站阿里培训机构如何招生营销
  • 下载安装wordpress 主题灰色词网站seo
  • 哪家网站做的好百度公司招聘信息
  • 有些公司做网站比较好的seo是什么软件
  • 做网站的背景图片要多大东莞关键词排名seo
  • 如何干电商怎么优化一个网站关键词
  • 网站运营有哪些岗位免费刷赞网站推广qq免费
  • 做律师百度推广的网站腾讯会议开始收费
  • 设计一个商务网站百度营销网页版
  • 网站域名过户什么是优化师
  • 通过党建网站联盟的建设长春seo排名
  • 北京建设工程主管部门网站网络广告的形式有哪些
  • 教育行业网站建设咨询热线长沙seo网络公司
  • 电商平台网站制作商品推广软文写作500字
  • 做网站的排名互联网搜索引擎有哪些
  • 假网站怎么制作什么是网络营销渠道
  • 专业网站设计建站网络营销工程师
  • 青岛网站建设服务器腾讯企点官网下载
  • 做 cad效果图网站营销软件哪个好
  • 湖南众诚建设 官方网站珠海关键词优化软件