当前位置:网站首页>【 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;}}; 边栏推荐
- 微信小程序自定义日期选择器(带标题的)
- 如何防止浏览器指纹关联
- Several important functions of singly linked list (including insertion, deletion, reversal, etc.)
- Xgboost系列-XGB实际参数调优指南附源码
- 抱抱脸(hugging face)教程-中文翻译-对预先训练过的模特进行微调
- 【深度学习】目标检测之评价指标
- stream去重相同属性对象
- More than pytorch from zero to build neural network to realize classification (training data sets)
- 升职加薪之SQL索引
- 面试合集
猜你喜欢
随机推荐
工作不等于生活,但生活离不开工作 | 2022 年中总结
Sequelize配置中的timezone测试
pyspark jieba 集群模型 对文本进行切词
Why learn the principles of compiling
桥接模式下虚拟机连接不上网络的解决方法(WIFI)
(13)Filter过滤器
路由的懒加载与接口的封装
.Net Core动态注入
抱抱脸(hugging face)教程-中文翻译-创建一个自定义架构
大数组减小数组常用方法
js总结,基础篇
【深度学习】归一化(十一)
从数组到js基础结束
微信小程序自定义日期选择器(带标题的)
微信小程序tabs
Candide3人脸动画模型
人脸识别示例代码解析(二)——人脸识别解析
WebGL:BabylonJS入门——初探:我的世界
Qt control - QTextEdit usage record
.Net Core后台任务启停(BackgroundService)









