您现在的位置是:首页 > 文章详情

java -----------------LeetCode——电话号码的字母组合

日期:2018-07-28点击:303

回溯算法是不是很是陌生啊,我敢开始也是一脸你蒙蔽,后来我终于忍不住了,就去查查什么是回溯,原来是这个情况,回溯和递归有点类似,但是又不是递归,比如我现在计算数据从0开始到2,当到达状态值2我就回溯,我从1开始回溯,我在回到0,就是for循环我不按套路从小到打,去输出了,我采用回溯,

public class Main { public static void main(String[] args) { List<String> stringList = begin("23"); System.out.println(stringList.toString()); } // 定义每个数字对应的字符 static String[] a = new String[] {"","","abc","def", "ghi","jkl","mno","pqrs","tuv","wxyz"}; // 这个是输出的字符串 static StringBuffer sb = new StringBuffer(); private static List<String> begin(String str) { if (str.length() == 0) { return null; } List<String> result = new ArrayList<>(15); zihe(str,0,result); return result; } private static void zihe(String str, int n, List<String> result) { if (n == str.length()) { result.add(sb.toString()); return; } for (int i = 0; i < a[str.charAt(n)-'0'].length(); i++) { sb.append(a[str.charAt(n)-'0'].charAt(i)); zihe(str, n + 1, result); sb.deleteCharAt(sb.length() - 1); } } } 
原文链接:https://yq.aliyun.com/articles/625779
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章