关注
DFS做法, 能满足需求,有改进的地方欢迎大家提出 import java.util.*; //Author: Zhenjie Hao public class Main { public static void main(String[] args){ int input = 123; List<List<String>> result = new ArrayList<>(); result = converse(input); display(result); } public static List<List<String>> converse(int input){ String str = String.valueOf(input); List<List<String>> result = new ArrayList<>(); dfs(str, 0 , new ArrayList<>(), result); return result; } public static void dfs(String str, int startIndex, List<String> path, List<List<String>> result){ if(startIndex == str.length()){ result.add(new ArrayList<String>(path)); } for(int i = startIndex + 1; i <= str.length(); i++){ String sub = str.substring(startIndex, i); if(!isValid(sub)){ break; } path.add(numToAlg(sub)); dfs(str, i, path, result); path.remove(path.size()-1); } } public static boolean isValid(String str){ //str = "12" or "27" if(str.charAt(0) == '0'){ return false; } int num = Integer.parseInt(str); if(num < 1 || num > 26){ return false; } return true; } public static String numToAlg(String str){ //str = "12" //putput = "d" int num = Integer.parseInt(str); return String.valueOf((char)('a' + num - 1)); } public static void display(List<List<String>> result){ for(int i = 0; i < result.size(); i++){ StringBuilder sb = new StringBuilder(); for(int j = 0; j < result.get(i).size(); j++){ sb.append(result.get(i).get(j)); } System.out.println(sb.toString()); } } }
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 面试体验最好和最差的公司 #
6117次浏览 41人参与
# 如何提高实习转正率? #
99372次浏览 575人参与
# 厦门银行科技岗值不值得投 #
17179次浏览 412人参与
# 烂工作和没工作哪个更痛苦? #
7316次浏览 148人参与
# 重来一次,我还会选择这个专业吗 #
444323次浏览 3945人参与
# 给工作过的公司写一条大众点评,你会怎么写? #
3046次浏览 50人参与
# 春招至今,你收到几个面试了? #
14363次浏览 229人参与
# AI替代不了什么? #
6319次浏览 95人参与
# 现在入门AI首先要做什么? #
1523次浏览 49人参与
# 一人分享一个skill #
1192次浏览 35人参与
# 银行笔面经互助 #
190166次浏览 1312人参与
# 总结:offer选择,我是怎么选的 #
280624次浏览 1550人参与
# 有必要和同事成为好朋友吗? #
43859次浏览 439人参与
# Agent面试会问什么? #
5086次浏览 128人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
10827次浏览 56人参与
# 学历VS实习,哪个更重要? #
18575次浏览 250人参与
# 选完offer后,你后悔学本专业吗 #
67970次浏览 267人参与
# 职场吐槽大会 #
344950次浏览 2275人参与
# 如果实习可以转正,你会不会放弃秋招 #
968873次浏览 6870人参与
# 机械人,你的秋招第一份简历被谁挂了 #
261019次浏览 2435人参与
# 通信硬件薪资爆料 #
1289399次浏览 7263人参与
查看15道真题和解析