FTPrep, 17 letter combination of a phone number
来源:互联网 发布:装修设计效果图软件 编辑:程序博客网 时间:2024/06/06 15:39
感觉这题 已经做过无数次,但是写起来还是不敢说 一次 bug-free 写出来。
写的过程中,对dfs的写法犹豫了,把branch写成了一个list的类型,但发现这样写不通,才反应过来要把list放在参数里,设计成void类型,return是在list.add()动作完成后。写得时候是不是在这个地方return,犹豫了,虽然AC了,但是过程没有特别清楚。在纸上画了下流程图,才确信了。见PPT。
这个过程中的关键点:
1,递归调用是从for loop中出去了,到了叶子节点才会有return发生,return是回到倒数第二层节点的for loop中。
2,而在各个相同层次的节点,从左到右的移动都与return无关,而是于for loop中的遍历有关。
把树形图画出来还不够,还要把各个节点的所处的状态写出来,把该节点对应的 函数名 和其中的各个参数都 写出来,标明清楚。
public class Solution { public List<String> letterCombinations(String digits) { List<String> list = new ArrayList<String>(); if(digits.length()==0) return list; HashMap<Character, String> table=new HashMap<>(); table.put('2',"abc"); table.put('3',"def"); table.put('4',"ghi"); table.put('5',"jkl"); table.put('6',"mno"); table.put('7',"pqrs"); table.put('8',"tuv"); table.put('9',"wxyz"); table.put('0',"abc"); table.put('1',""); branch(digits, table, 0, list, ""); return list; } private void branch(String digits, HashMap<Character, String> table, int index, List<String> list, String item){ if(index==digits.length()) { list.add(item); return; } else{ String str= table.get(digits.charAt(index)); for(int i=0; i<str.length(); ++i){ branch(digits, table, index+1, list, item+str.charAt(i)); } } }}
阅读全文
0 0
- FTPrep, 17 letter combination of a phone number
- Letter Combination of a Phone Number
- leetcode-letter combination of a phone number
- leetcode-17 Letter Combination of a Phone Number
- Letter Combination of phone number
- Letter Combination of phone number
- [Leetcode] 17. Letter Combination of a Phone Number
- Python 刷题日记: Letter Combination of a Phone Number
- 17,Letter Combinations of a Phone Number
- #17 Letter Combinations of a Phone Number
- 17Letter Combinations of a Phone Number
- 17 Letter Combinations of a Phone Number
- 17 Letter Combinations of a Phone Number
- 【17】Letter Combinations of a Phone Number
- 17 Letter Combinations of a Phone Number
- 17-Letter Combinations of a Phone Number
- LeetCode 17: Letter Combinations of a Phone Number
- LeetCode(17) Letter Combinations of a Phone Number
- Python:对称的二叉树
- Transfer water
- dnsmasq使用注意事项
- Java之执行Windows 命令行-yellowcong
- Java之字符串工具类-yellowcong
- FTPrep, 17 letter combination of a phone number
- Java之日期工具类-yellowcong
- LuaIde 免费版本
- Java之文件工具类-yellowcong
- 调试视频教程
- Java之文件下载工具类-yellowcong
- Cocos2d 调试
- 如何编写复用性、可读性和可维护性的代码
- 调试代码下载