LEETCODE17
来源:互联网 发布:cisco 端口配置 编辑:程序博客网 时间:2024/05/17 09:24
public class Solution { public List<String> letterCombinations(String digits) { LinkedList<String> ans = new LinkedList<String>(); String[] mapping = new String[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; if(digits.equals("")){ ; }else{ ans.add(""); for(int i =0; i<digits.length();i++){ int x = Character.getNumericValue(digits.charAt(i)); while(ans.peek().length()==i){ String t = ans.remove(); for(char s : mapping[x].toCharArray()) ans.add(t+s); } } } return ans; }}
我认为该题目的难点在于对于当前字符i,如何对后面的i+1字符的情况进行操作。这里就自然想到了队列这种数据结构。
例如:digits是23,那么将2代表的a取出,创建一个新的字符串将a与3所代表的字母赋值给该字符串,再进队列。循环该字符串的次数,即可。
判断何时停止取出?利用队列中字符串的长度与I这个指针所指向的位置比较进行判断。
这是我一开始看到这道题目所想到的思路。
0 0
- leetcode17
- LEETCODE17
- LeetCode17
- leetcode17.PalindromePartitioning
- LeetCode17: 4Sum
- Leetcode17: Valid Palindrome
- [LeetCode17]Letter Combinations of a Phone Number
- LeetCode17:Letter Combinations of a Phone Number
- leetcode17 Letter Combinations of a Phone Number
- LeetCode17:Letter Combinations of a Phone Number
- leetcode17---Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number
- leetcode17:Letter Combinations of a Phone Number
- LeetCode17. Letter Combinations of a Phone Number
- leetcode17. Letter Combinations of a Phone Number
- leetcode17 Letter Combinations of a Phone Number
- [Java]LeetCode17 Letter Combinations of a Phone Number
- leetcode17-根据数字找到可能的按键字母组合
- Python 接口测试(五)
- Android中的线程(一)
- 查找某目录下占用空间最大的10个文件
- 数据结构中Java实现KMP与BF算法对比
- C++抽象编程——面向对象(4)——运算符重载与友元函数
- LEETCODE17
- linux系统之网络管理(1)
- listview显示不同布局的一个坑(记一下,以防后面又忘了)
- 51nod1289
- dp总结
- angularJs的表单验证
- 工作日志
- 补间动画
- IDEAL添加jia