当前位置:网站首页>【 Leetcode 】 433. The smallest genetic changes
【 Leetcode 】 433. The smallest genetic changes
2022-08-09 16:48:00 【wangyunpeng33】
433. Minimal Gene Change
https://leetcode-cn.com/problems/minimum-genetic-mutation/.
Solution ideas
Breadth-first search
- In order to improve performance, we will use a two-way search method
- How to search in both directions?
The same as general breadth, the only difference is that each time you need to judge whether to traverse with head or tail
The most basic guiding principle is:
- Use a small number of traversals first
- In order to facilitate quick search, we use set to replace queue
The code that does not consider the wide search
class Solution {public:int minMutation(string start, string end, vector& bank) {int count = 0;for(int i = 7,j=0; i >= 0 && j < 8; i--,j++){if(start[i]!=end[i]){start[i]=end[i];bool isvalid = false;if(find(bank.begin(),bank.end(),start)!=bank.end()){count++;isvalid = true;}else return -1;}}return count;}}; BFS code
class Solution {public:int minMutation(string start, string end, vector& bank) {// Build the dictionary of the bank first, which is convenient for quick search. If it is used, it will be deleted to avoid loop traversal.unordered_set bankSet;for (string& b : bank){bankSet.insert(b);}// pre-judgmentif (bankSet.find(end) == bankSet.end()){return -1;}// replaceable four characterschar replaces[4] = {'A', 'C', 'G', 'T'};// Build the initialized two head and tail collectionsunordered_set heads = {start};unordered_set tails = {end};// a temporary variable to hold the current collectionunordered_set temp;// Times record: Accumulate according to each layerint rounds = 0;while (!heads.empty() && !tails.empty()){++rounds;// Take the smallest set as the traversed object, the smallest is always in the headsif (heads.size() > tails.size()){swap(heads, tails);}for (const string& head : heads){string curr = head;// loop through each characterint n = curr.size();for (int i = 0; i < n; ++i){char old = curr[i];// replace the four possibilitiesfor (int j = 0; j < 4; ++j){curr[i] = replaces[j];// cout << rounds << " : " << curr << " " << head << endl;// If found in the tail, return directlyif (tails.find(curr) != tails.end()){return rounds;}else if (bankSet.find(curr) != bankSet.end()){bankSet.erase(curr);temp.insert(curr);}}// essential restore charactercurr[i] = old;}}// Quick swap assignment to headsswap(heads, temp);temp.clear();}// if not found, return -1return -1;}}; 边栏推荐
猜你喜欢

Qt control - QTextEdit usage record

地铁预约Postman脚本使用

PAT1027 Printing Hourglass

跨平台桌面应用 Electron 尝试(VS2019)

ASP.Net Core实战——使用Swagger

.Net Core动态注入

【论文阅读】LIME:Low-light Image Enhancement via Illumination Map Estimation(笔记最全篇)

数据缺失对任务影响

小型项目如何使用异步任务管理器实现不同业务间的解耦

【论文阅读】Deep Learning for Image Super-resolution: A Survey(DL超分辨率最新综述)
随机推荐
深度神经网络中的多任务学习研究综述
Retrofit2 初印象?
浏览器中的302你真的知道吗
个人域名备案详细流程(图文并茂)
The recycle bin has been showed no problem to empty the icon
ASP.Net Core实战——使用Swagger
Left-handed and Right-handed Binary Sorted Trees
pyspark dataframe分位数计算
NLP-阅读理解任务学习总结概述
The difference between show and exec in Qt dialog
研究生工作周报(第六周)
Candide3人脸动画模型
crontab失效怎么解决
鸡生蛋,蛋生鸡问题。JS顶级对象Function,Object关系
微信小程序转盘demo
浏览器指纹识别是什么意思?
如何在实际操作中进行亚马逊测评
js总结,基础篇
【研究生工作周报】(第八周)
升职加薪之SQL索引