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

Java | Leetcode Java题解之第433题最小基因变化

题目:

题解:

class Solution {public int minMutation(String start, String end, String[] bank) {int m = start.length();int n = bank.length;List<Integer>[] adj = new List[n];for (int i = 0; i < n; i++) {adj[i] = new ArrayList<Integer>();}int endIndex = -1;for (int i = 0; i < n; i++) {if (end.equals(bank[i])) {endIndex = i;}for (int j = i + 1; j < n; j++) {int mutations = 0;for (int k = 0; k < m; k++) {if (bank[i].charAt(k) != bank[j].charAt(k)) {mutations++;}if (mutations > 1) {break;}}if (mutations == 1) {adj[i].add(j);adj[j].add(i);}}}if (endIndex == -1) {return -1;}Queue<Integer> queue = new ArrayDeque<Integer>();boolean[] visited = new boolean[n];int step = 1;for (int i = 0; i < n; i++) {int mutations = 0;for (int k = 0; k < m; k++) {if (start.charAt(k) != bank[i].charAt(k)) {mutations++;}if (mutations > 1) {break;}}if (mutations == 1) {queue.offer(i);visited[i] = true;}}        while (!queue.isEmpty()) {int sz = queue.size();for (int i = 0; i < sz; i++) {int curr = queue.poll();if (curr == endIndex) {return step;}for (int next : adj[curr]) {if (visited[next]) {continue;}visited[next] = true;queue.offer(next);}}step++;}return -1;}
}

http://www.mrgr.cn/news/34742.html

相关文章:

  • Python | Leetcode Python题解之第433题最小基因变化
  • scss知识汇总
  • JS数组随机取数
  • Java运算符
  • centos7 docker部署nacos
  • 公安局党建平台建设方案和必要性-———未来之窗行业应用跨平台架构
  • 【我的 PWN 学习手札】fastbin reverse into tcache —— tcache key 绕过
  • 使用GLib进行C语言编程的实例
  • typename、非类型模板参数、模板参数的特化、模板类成员函数声明和定义分离、继承等的介绍
  • LED显示屏驱动电源:恒流与恒压,谁更胜一筹?
  • 在uboot中添加自定义命令
  • Robot Operating System——带有时间戳和坐标系信息的多边形信息
  • ubuntu内网穿透后在公网使用ssh登录
  • could not broadcast input array from shape
  • 盘点那些功能强大的思维导图在线工具,你用过几个
  • 图数据库之TigerGraph
  • R语言 基础笔记 2
  • 【CSS in Depth 2 精译_038】6.2 CSS 定位技术之:绝对定位
  • util-linux 和 dosfstools 开发 ,fdisk mkfs工具移植
  • 【C++ 11多线程加速计算实操教程】