关注
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()); } } }
查看原帖
点赞 评论
相关推荐
点赞 评论 收藏
分享
2025-12-16 17:17
门头沟学院 产品经理 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 在大厂上班是一种什么样的体验 #
11007次浏览 138人参与
# 你的mentor是什么样的人? #
50826次浏览 713人参与
# 程序员找工作至少要刷多少题? #
18974次浏览 248人参与
# 机械人避雷的岗位/公司 #
43553次浏览 300人参与
# 我现在比当时_,你想录用我吗 #
8826次浏览 111人参与
# 设计人如何选offer #
189486次浏览 868人参与
# 为了减少AI幻觉,你注入过哪些设定? #
4840次浏览 150人参与
# 你的秋招进行到哪一步了 #
2529794次浏览 23253人参与
# 机械人还在等华为开奖吗? #
311964次浏览 1582人参与
# 牛客AI体验站 #
6927次浏览 189人参与
# 重来一次,我还会选择这个专业吗 #
411047次浏览 3898人参与
# 12306一秒售罄,你抢到回家的票了吗? #
2018次浏览 48人参与
# 论秋招对个人心气的改变 #
11267次浏览 156人参与
# 一张图晒一下你的AI员工 #
5183次浏览 115人参与
# AI Coding的使用心得 #
4741次浏览 101人参与
# 刚入职的你踩过哪些坑 #
6961次浏览 128人参与
# 关于春招/暑期实习,你想知道哪些信息? #
7648次浏览 119人参与
# 应届生进小公司有什么影响吗 #
118325次浏览 1159人参与
# 晒晒你司的新年福利 #
8576次浏览 106人参与
# 找工作的破防时刻 #
254114次浏览 1963人参与

OPPO公司福利 1108人发布