Java for LeetCode 017 Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

解题思路:

使用DFS算法,JAVA实现如下:

	    static String[] alpha = new String[] {
	    		" ","1", "abc", "def","ghi", "jkl", "mno","pqrs", "tuv", "wxyz"
	    };
	    static StringBuilder sb = new StringBuilder();
	    static  void dfs(List<String> list, String digits, int cur) {
	        if (cur >= digits.length())
	            list.add(sb.toString());
	        else {
	            for (int i = 0; i < alpha[digits.charAt(cur) - ‘0‘].length(); i++) {
	                sb.append(alpha[digits.charAt(cur) - ‘0‘].charAt(i));
	                dfs(list, digits, cur + 1);
	                sb.deleteCharAt(sb.length() - 1);
	            }
	        }
	    }
	   static public List<String> letterCombinations(String digits) {
	        List<String> list = new ArrayList<String>();
	        if (digits.length()==0)
	          return list;
	        dfs(list, digits, 0);
	        return list;
	    }

思路二:

凡是用到递归的地方都能用循环解决,因此可以用循环算法,JAVA实现如下:

static public List<String> letterCombinations(String digits) {
	        List<String> list = new ArrayList<String>();
	        String[] alpha = new String[] {
		    		" ","1", "abc", "def","ghi", "jkl", "mno","pqrs", "tuv", "wxyz"
		    };
	        if (digits.length()==0)
	        	return list;
	        int[] number = new int[digits.length()];//存储每次遍历字符位置
	        int index = 0;
	        while(index>=0) {
	            StringBuilder sb = new StringBuilder();
	            for(int i=0; i<digits.length(); i++)
	                sb.append(alpha[digits.charAt(i)-‘0‘].charAt(number[i]));
	            list.add(sb.toString());
	            // 每回合需要重置index到末尾
	            index = digits.length()-1;
	            while(index>=0) {
	                if( number[index] < (alpha[digits.charAt(index)-‘0‘].length()-1) ) {
	                    number[index]++;
	                    break;
	                } else {
	                    number[index] = 0;
	                    index--;
	                }
	            }
	        }
	        return list;
	    }
时间: 2024-08-24 20:34:19

Java for LeetCode 017 Letter Combinations of a Phone Number的相关文章

LeetCode 017 Letter Combinations of a Phone Number

题目描述:Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23"

[LeetCode] 017. Letter Combinations of a Phone Number (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 017.Letter_Combinations_of_a_Phone_Number (Medium) 链接: 题目:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/ 代码(github):https://github.co

[LeetCode][JavaScript]Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 40709 Total Submissions: 157759My Submissions Question Solution Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just l

【leetcode】Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Outpu

leetCode 17. Letter Combinations of a Phone Number 字符串 | 回溯 | Medium

17. Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23"Ou

LeetCode Solution:Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 17652 Total Submissions: 66854My Submissions Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephon

Leetcode dfs Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 15964 Total Submissions: 60700My Submissions Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephon

Leetcode 17. Letter Combinations of a Phone Number(水)

17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given

LeetCode 17 Letter Combinations of a Phone Number(C,C++,Java,Python)

Problem: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae&