首页 > 其他 > 详细

Well-ordered String

时间:2014-11-21 06:55:06      阅读:479      评论:0      收藏:0      [点我收藏+]

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     }

 

Well-ordered String

原文:http://www.cnblogs.com/superbo/p/4111922.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!