java常见算法

1.(哈希表)

给出一个字符串数组 words 组成的一本英语词典。返回 words 中最长的一个单词,该单词是由 words 词典中其他单词逐步添加一个字母组成。

若其中有多个可行的答案,则返回答案中字典序最小的单词。若无答案,则返回空字符串。

示例 1:

输入:words = ["w","wo","wor","worl", "world"]
输出:"world"
解释: 单词"world"可由"w", "wo", "wor", 和 "worl"逐步添加一个字母组成。
示例 2:

输入:words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
输出:"apple"
解释:"apply" 和 "apple" 都能由词典中的单词组成。但是 "apple" 的字典序小于 "apply" 

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

class ggg {
     public String longestWord(String[] words) {

               Arrays.sort(words, (a, b) ->  {
                    if (a.length() != b.length()) {
                         return a.length() - b.length();
                    } else {
                         return b.compareTo(a);
                    }
               });

          String longest = "";
          Set<String> set = new HashSet<String>();
          set.add("");
          int n = words.length;
          for (int i = 0; i < n; i++) {
               String word = words[i];
               if (set.contains(word.substring(0, word.length() - 1))) {
                    set.add(word);
                    longest = word;
               }
          }
          return longest;
     }
}

2.深度优先搜索

小朋友 A 在和 ta 的小伙伴们玩传信息游戏,游戏规则如下:

有 n 名玩家,所有玩家编号分别为 0 ~ n-1,其中小朋友 A 的编号为 0
每个玩家都有固定的若干个可传信息的其他玩家(也可能没有)。传信息的关系是单向的(比如 A 可以向 B 传信息,但 B 不能向 A 传信息)。
每轮信息必须需要传递给另一个人,且信息可重复经过同一个人
给定总玩家数 n,以及按 [玩家编号,对应可传递玩家编号] 关系组成的二维数组 relation。返回信息从小 A (编号 0 ) 经过 k 轮传递到编号为 n-1 的小伙伴处的方案数;若不能到达,返回 0。

示例 1:

输入:n = 5, relation = [[0,2],[2,1],[3,4],[2,3],[1,4],[2,0],[0,4]], k = 3

输出:3

解释:信息从小 A 编号 0 处开始,经 3 轮传递,到达编号 4。共有 3 种方案,分别是 0->2->0->4, 0->2->1->4, 0->2->3->4。

示例 2:

输入:n = 3, relation = [[0,2],[2,1]], k = 2

输出:0

解释:信息不能从小 A 处经过 2 轮传递到编号 2

import java.util.ArrayList;
import java.util.List;

class aa {
     int count,n,k;
     List<List<Integer>> res;

     public int numWays(int n, int[][] relation, int k) {

          count = 0;
          this.k = k;
          this.n = n;
          res = new ArrayList<List<Integer>>();
          int len = relation.length;

          for(int i = 0;i < n;i++) {

               res.add(new ArrayList<Integer>());
          }

          for(int[] arr : relation) {

               int a = arr[0],b = arr[1];
               res.get(a).add(b);
          }

          dfs(0,0);

          return count;
     }

     public void dfs(int index,int steps) {

          if(steps == k) {

               if(index == n - 1)
                    count++;

               return;
          }

          List<Integer> list = res.get(index);

          for(int next : list) {

               dfs(next,steps + 1);
          }
     }
}

3.回溯

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。 

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

示例 1:

输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:

输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:

输入: candidates = [2], target = 1
输出: []

class Solution {

    ArrayList<List<Integer>> lists = new ArrayList<>();
    int len;
    ArrayList<Integer> integers = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {

        len = candidates.length;


        backtrink(0,target,candidates,0);

        return lists;
    }

    public void backtrink(int sum,int target,int[] candidates,int indexStart) {

        if (sum > target || indexStart >= len)
            return;

        if (sum == target) {

            ArrayList<Integer> arrayList = new ArrayList<>(this.integers);
            lists.add(arrayList);
            return;
        }

        for (int i = indexStart; i < len; i++) {

            integers.add(candidates[i]);
            backtrink(sum + candidates[i],target,candidates,i);

            integers.remove(integers.size() - 1);
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海边的彩虹与你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值