• 算法day30


    第一题

    433. 最小基因变化

            题型转化:可以转化为边权为一的最短路问题

            将最开始的字符串定义为起点,我们将初识字符串每一个元素改变一次定义为移动一个位置,最后的字符串定义为中点,就这样每一次改变一个元素,最后成功改变成最终字符且改变的次数最少的次数就是我们需要的答案;

    步骤一:

            使用hash表来存放基因库中的字符串,方便我们在改变字符后检查改变后的字符串是否合理;

            使用hash表2来存放我们已经改变过的字符串,这样方便我们检查我们的已经改变过的字符串变回原来的样子;

    步骤二:

            将我们需要改变成的元素放在数组中,这样方便后期改变元素;

    步骤三:

            我们改变后的字符串,判断条件(没有改变回之前的样子且满足基因库的条件),才可以存放进队列,进行以下一次的改变;

    至此,代码如下:

    1. class Solution {
    2. public int minMutation(String startGene, String endGene, String[] bank) {
    3. Set vis = new HashSet<>();//用来标记已经搜索过的状态
    4. Set hash = new HashSet<>();//用来统计基因库里面的字符串
    5. for(String s : bank){
    6. hash.add(s);
    7. }
    8. char[] cha = {'A','C','G','T'};
    9. if(startGene.equals(endGene)) return 0;
    10. if(!hash.contains(endGene)) return -1;
    11. Queue q = new LinkedList<>();
    12. q.add(startGene);
    13. vis.add(startGene);
    14. int step = 0;
    15. while(!q.isEmpty()){
    16. step++;
    17. int size = q.size();
    18. while(size-- != 0){
    19. String t = q.poll();
    20. for(int i = 0;i < 8 ;i++){
    21. char[] tmp = t.toCharArray();//字符串变数组
    22. for(int j = 0 ;j< 4 ;j++){
    23. tmp[i] = cha[j];
    24. String next = new String(tmp);
    25. if(hash.contains(next) && !vis.contains(next)) {
    26. if(next.equals(endGene))
    27. return step;
    28. q.add(next);
    29. vis.add(next);
    30. }
    31. }
    32. }
    33. }
    34. }
    35. return -1;
    36. }
    37. }

    第二题

    127. 单词接龙

    本题分析如上题故事,代码如下:

    1. class Solution {
    2. public int ladderLength(String beginWord, String endWord, List wordList) {
    3. Set hash = new HashSet<>();
    4. Set vis = new HashSet<>();
    5. for(String s : wordList){
    6. hash.add(s);
    7. }
    8. if(!hash.contains(endWord)) return 0;
    9. Queue q = new LinkedList<>();
    10. q.add(beginWord);
    11. vis.add(beginWord);
    12. int ret = 1;
    13. while(!q.isEmpty()){
    14. ret ++;
    15. int size = q.size();
    16. while(size-- != 0){
    17. String t = q.poll();
    18. for(int i = 0;i < t.length();i++){
    19. char[] tmp = t.toCharArray();
    20. for(char ch = 'a';ch <= 'z';ch++){
    21. tmp[i] = ch;
    22. String next = new String(tmp);
    23. if(hash.contains(next) && !vis.contains(next)){
    24. if(next.equals(endWord)) return ret;
    25. q.add(next);
    26. vis.add(next);
    27. }
    28. }
    29. }
    30. }
    31. }
    32. return 0;
    33. }
    34. }

    第三题

    675. 为高尔夫比赛砍树

            如下图所示,为砍树的图,

            按照题意的规则,就是从(0,0)坐标出发,先到2砍树,再到6、7、9、9、21依次砍树;并最后返回移动的最短次数;

    步骤一:

            首先我们遍历整个矩阵,将有树的位置都放入到一个队列中,并按照该位置中树的个数由小到大依次排序;

    步骤二:

            我们按照上述队列中位置的顺序,依次进行bfs移动,并记录每一个位置到下一个位置的移动次数,当所有的有效位置都便利完成时即砍完树了,最后返回总移动的步数;        

    至此,代码如下:

    1. class Solution {
    2. int m,n;
    3. public int cutOffTree(List> f) {
    4. m = f.size();
    5. n = f.get(0).size();
    6. //1、将所有的树放入到表中,并从小到大排序
    7. List<int[]> trees = new ArrayList<>();
    8. for(int i = 0; i
    9. for(int j = 0; j< n ;j++){
    10. if(f.get(i).get(j) > 1){
    11. trees.add(new int[]{i,j});
    12. }
    13. }
    14. }
    15. Collections.sort(trees,(a,b) -> {
    16. return f.get(a[0]).get(a[1]) - f.get(b[0]).get(b[1]);
    17. });
    18. //2、按照顺序砍树
    19. int ret = 0;
    20. int bx = 0,by = 0;
    21. for(int[] t : trees){
    22. int x = t[0],y = t[1];
    23. int step = bfs(f,bx,by,x,y);
    24. if(step == -1) return -1;//此时到不了下一个节点,即砍不了树
    25. ret += step;
    26. bx = x;by = y;
    27. }
    28. return ret;
    29. }
    30. int[] dx = {0,0,1,-1};
    31. int[] dy = {1,-1,0,0};
    32. public int bfs (List> f,int bx, int by, int ex, int ey){
    33. if(bx == ex && by == ey) return 0;
    34. Queue<int[]> q = new LinkedList<>();
    35. boolean[][] vis = new boolean[m][n];
    36. q.add(new int[]{bx,by});
    37. vis[bx][by] = true;
    38. int step = 0;
    39. while(!q.isEmpty()){
    40. step ++;
    41. int sz = q.size();
    42. while(sz-- != 0){
    43. int[] t = q.poll();
    44. int a = t[0],b = t[1];
    45. for(int i = 0; i < 4;i++){
    46. int x = a+dx[i],y = b+dy[i];
    47. if(x >= 0 && y >=0 && y0 && !vis[x][y]){
    48. if(x == ex && y == ey) return step;
    49. q.add(new int[]{x,y});
    50. vis[x][y] = true;
    51. }
    52. }
    53. }
    54. }
    55. return -1;
    56. }
    57. }

    ps:本次的内容就到这里了,如果对你有所帮助的话就请一键三连哦!!!

  • 相关阅读:
    seq2seq与引入注意力机制的seq2seq
    Swift 周报 第十七期
    仓库太大,clone 后,git pull 老分支成功,最新分支失败
    【C语言】-结构体内存对齐。附详细图解
    深入理解Spring AOP中的@EnableAspectJAutoProxy
    torch.nn.functional.grid_sample(F.grid_sample)函数的说明 & 3D空间中的点向图像投影的易错点
    LeetCode117. Populating Next Right Pointers in Each Node II
    前端页面JS事务学习Day02
    如何进行LLM大模型推理优化
    ZXing.Net 的Core平台生成二维码
  • 原文地址:https://blog.csdn.net/2202_76101487/article/details/139673977