zoukankan      html  css  js  c++  java
  • Well-ordered String

    Problem

    You know a password is well-ordered string. Well-ordered string means that the order of the characters is in an alphabetical increasing order. Like “abEm” is a well-ordered number. However, “abmE” is not a well-order number. Given an input# that tells you also how many digits are in the password, print all possible 

    Solution

    Recursive + DFS

     1     public static List<List<String>> hackCode(int num) {
     2         List<List<String>> res = new ArrayList<List<String>>();
     3         if (num < 0) {
     4             return res;
     5         }
     6 
     7         List<String> ls = new ArrayList<String>();
     8         helper(res, ls, 0, num, 0);
     9         return res;
    10     }
    11 
    12     public static void helper(List<List<String>> res, List<String> ls, int prev,
    13             int num, int pos) {
    14         if (pos == num) {
    15             res.add(new ArrayList<String>(ls));
    16             return;
    17         }
    18 
    19         for (int i = prev; i < 26; i++) {
    20             char c = (char) ('a' + i);
    21             ls.add(String.valueOf(c));
    22             helper(res, ls, i + 1, num, pos + 1);
    23             ls.remove(ls.size() - 1);
    24         }
    25     }
  • 相关阅读:
    ARTS第八周打卡
    ARTS第七周打卡
    ARTS第六周打卡
    ARTS第五周打卡
    ARTS第四周打卡
    ARTS第三周打卡
    ARTS 第二周
    uniapp——头部导航栏配置
    码云、Git使用教程
    超出文本宽度点点显示——css
  • 原文地址:https://www.cnblogs.com/superbo/p/4111922.html
Copyright © 2011-2022 走看看