java常见算法

欢迎关注 每日持续更新优质题目

  1. 回溯

给你一个 无重复元素 的整数数组 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);
        }
    }

}

2.回溯-------去重  重要

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次 。

注意:解集不能包含重复的组合。 

示例 1:

输入: candidates = [10,1,2,7,6,1,5], target = 8,

输出:

[

[1,1,6],

[1,2,5],

[1,7],

[2,6]

]

示例 2:

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

输出:

[

[1,2,2],

[5]

]


class Solution {

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

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


        len = candidates.length;

        Arrays.sort(candidates);

        backtrink(target,candidates,0);

        return lists;
    }

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

        if (target < 0)
            return;

        if (target == 0) {

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

        for (int i = indexStart; i < len; i++) {
            
            //在这去重
            //1 1 6 7
            //第一个1使用了后面的元素  第二个就不能用了
            if(i > indexStart && candidates[i] == candidates[i-1])
                continue;
            
            integers.add(candidates[i]);
            backtrink(target - candidates[i],candidates,i + 1);

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

}

3.

给出一个字符串数组 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" 

一点一点匹配


class Solution {
     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);
               }
          });
         
          HashSet set = new HashSet();
          int len = words.length;

          String longest = "";
          set.add("");
          for (int i = 0; i < len; i++) {

               String word = words[i];
               if (set.contains(word.substring(0,word.length()-1))) {
               set.add(word);
               
               longest = word;}
          }
          
          return longest;
     }
}

4.深度优先

小朋友 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。


class Solution {
     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);
          }
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java常见算法题有很多,以下是一些常见算法题目及其解决方法。 1. 求两个整数的最大公约数和最小公倍数。可以使用辗转相除法来求最大公约数,即不断用较大数除以较小数,直到余数为0,则较小数就是最大公约数。最小公倍数等于两数的乘积除以最大公约数。 2. 数组中找出第K大(小)的数。可以使用快速排序的思想,选取一个基准元素,将数组分为大于基准元素和小于基准元素的两部分,递归地在其中一部分中查找第K大(小)的数。 3. 判断一个字符串是否为回文串。可以使用双指针法,分别从字符串的开头和结尾开始遍历,判断对应字符是否相等,直到两指针相遇或交叉。 4. 实现链表的反转。可以使用迭代或递归的方式,将当前节点的下一个节点指向上一个节点,然后继续遍历链表。 5. 实现二分查找算法。对于有序数组,可以使用二分查找法,在数组的中间位置判断目标值与中间值的大小关系,然后缩小查找范围,直到找到目标值或查找范围为空。 6. 实现图的深度优先搜索(DFS)和广度优先搜索(BFS)。DFS使用递归的方式进行搜索,遍历当前节点的邻接节点,直到遍历完所有节点或找到目标节点。BFS使用队列进行搜索,将当前节点的邻接节点加入队列,并依次出队访问,直到找到目标节点或队列为空。 以上只是一些常见算法题目,掌握这些算法可以帮助我们更好地理解和解决实际问题。当然,还有许多其他的算法题目,不断学习和练习才能更好地掌握。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

海边的彩虹与你

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

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

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

打赏作者

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

抵扣说明:

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

余额充值