LeetCode刷题day017 (Jieky)

LeetCode第17题

/*
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 below. Note that 1 does not map to any letters.

2 abc
3 def
4 ghi
5 jkl
6 mno
7 pqrs
8 tuv
9 wxyz

Example:
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
*/

import java.util.*;
public class LetterCombinationsPhoneNumber{
     
	public static void main(String[] args){
     
		String str = "23";
		LetterCombinationsPhoneNumber lcpn = new LetterCombinationsPhoneNumber();
		List<String> result = lcpn.letterCombinations03(str);
		System.out.println(result);
	}
	
	// 树形递归,到页节点解add到List中
	public List<String> letterCombinations03(String digits) {
     
		// Given a string containing digits from 2-9 inclusive
		
		// 使用LinkedList,省去多次申请新的空间
		List<String> ret = new ArrayList<>();
		if (digits == null || digits.length() == 0) return ret;
		
		String[] strs = {
     "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 
		
		// 为了省去第一个元素递归起始问题,可以使用""
		combination(ret,digits,strs,0,"");
		
		return ret;
	}
	private void combination(List<String> ret,String digits,String[] strs,int index,String prefix){
     
		// 停止条件
		if(index == digits.length()){
     
			// 达到叶结点时,即完成组合,将当前叶结点加入List
			ret.add(prefix);
			return; 
		}
		
		// String temp = strs[Integer.parseInt(digits.substring(index,index+1)) - 2];
		String temp = strs[Character.getNumericValue(digits.charAt(index)) - 2];
		// 对每一个元素递归
		for (int i=0;i<temp.length();i++){
     
			combination(ret,digits,strs,index+1,prefix+temp.substring(i,i+1));
		}
	}
	
	// 优化,更加优美
	public List<String> letterCombinations02(String digits) {
     
		// Given a string containing digits from 2-9 inclusive
		
		// 使用LinkedList,省去多次申请新的空间
		LinkedList<String> ret = new LinkedList<>();
		if (digits == null || digits.length() == 0) return ret;
		
		String[] strs = {
     "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 
		
		// 省去for循环里面的判断语句,这个语句不能出现在输入检查之前
		ret.add("");
		for (int i=0;i<digits.length();i++){
     
			// Integer.parseInt只能转化字符串,不能转化字符
			// Character.getNumericValue将字符转化为数值
			String temp = strs[Character.getNumericValue(digits.charAt(i)) - 2];
			// 保留当前的LinkedList的大小。ret.add("")操作使得ret的初始大小为1,省去if判断。
			int len = ret.size();
			for (int j = 0;j < len;j++){
     
				// 删除双链表的第一个元素
				String getStr = ret.removeFirst();
				for (int k=0;k<temp.length();k++){
     
					// 往双链表表尾添加元素
					ret.addLast(getStr+temp.substring(k,k+1));
				}
			}
		}
		return ret;
	}
	
	public List<String> letterCombinations01(String digits) {
     
		// Given a string containing digits from 2-9 inclusive
		List<String> ret = new ArrayList<>();
		if (digits == null || digits.length() == 0) return ret;
		
		String[] strs = {
     "abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; 
		
		for (int i=0;i<digits.length();i++){
     
			// Integer.parseInt只能转化字符串,不能转化字符
			String temp = strs[Integer.parseInt(digits.substring(i,i+1)) - 2];
			
			if (ret.size() > 0){
     
				// 后期取出ret的内容,往每个内容末尾添加新的元素
				List<String> ret_temp = new ArrayList<>();
				for (String p:ret){
     
					for (int j=0;j<temp.length();j++){
     
						ret_temp.add(p+temp.substring(j,j+1));
					}
				}
				// 更新List
				ret = ret_temp;
			}else{
     
				// 起始的时候直接将字符串加入ret
				for (int j=0;j<temp.length();j++){
     
					ret.add(temp.substring(j,j+1));
				}
			}
			
		}
		return ret;
	}
}

你可能感兴趣的:(LeetCode,leetcode,java)