标准的排序组合-算法

题目

有若干个字母,要求计算出长度为4的所有可能得组合

解题

排序组合最适用的就是回溯了,建议大家本地debug一层一层的看能好理解点

复制代码
private static void getResult(List<String> source, Stack<String> temp, int curLength, int maxLength, List<Stack<String>> result) {
    if (curLength == maxLength) {
      result.add((Stack<String>) temp.clone());
      return;
    }
    for (int i = 0; i < source.size(); i++) {
      if (temp.contains(source.get(i))) {
        continue;
      }
      temp.push(source.get(i));
      getResult(source, temp, curLength + 1, maxLength, result);
      temp.pop();
    }
  }

回溯之所以叫回溯,就是因为他是从尾巴的地方先遍历所有的可能性,然后再往上一层,当然了上一层遍历时还需要再往下一层检查所有的可能性

相关推荐
Frostnova丶8 小时前
【算法笔记】数学知识
笔记·算法
吴可可1239 小时前
AutoCAD 2016与2014二次开发关键差异
算法
雨白10 小时前
哈希:以时间换空间的算法实战
算法
San813_LDD12 小时前
[数据结构]LeetCode学习
数据结构·算法·图论
x1387028595712 小时前
c语言排雷游戏(基础版9*9)
c语言·算法·游戏
sheeta199813 小时前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油13 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
QuZero14 小时前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称14 小时前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划