2018.10.02 LeetCode 刷题日记 第17题

手机小键盘 2-9 数字键上有分别对应的字母,输入一串数字,如234,则 2-abc,3-def,4-ghi,按顺序每个数字选择一个字母,输出全部的字母组合

从第一个数的第一个字母开始,向下找数,每种情况结束后进行回溯

读入 23

2- abc

选a

读3 - def

选d 、选 e 、选f

此时再进行回溯方法,索引已经超过范围,第一次结束

2中选b  依次进行回溯

回溯方法总结:

确定函数结束条件

依次往下找,知道触碰结束,回到上层,重新开始

代码如下:

class Solution {
  public List<String> letterCombinations(String digits) {
    if(digits.length() == 0){
       return new ArrayList<String>();
    }
    List<String> ans = new ArrayList();
    String [] key = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};

    helper(ans,key,digits,0,"");
    return ans;
    }  

  public void helper(List<String> ans,String[] key,String digits,int index,String cur){
    if(index == digits.length()){
      if(cur != null){
        ans.add(cur);
    }
      return;
    }

    String temp = key[digits.charAt(index) - ‘0‘];
    for(int i = 0; i < temp.length(); i++){
      String next = cur + temp.charAt(i);
      helper(ans,key,digits,index + 1,next);
    }
  }
}

原文地址:https://www.cnblogs.com/tiansiyuan-program/p/9736843.html

时间: 2024-10-08 19:11:39

2018.10.02 LeetCode 刷题日记 第17题的相关文章

2018.9.30 LeetCode 刷题日记 第16题

第 16 题  最接近目标数的三数之和 对一个数组来说,找出其中的三个数,使得三数之和与target最接近,最先想到的是暴力法求解,对i = 0; j = i+ 1; k = j+1;进行三重遍历,记录对target距离的最小值,但是三重循环,时间复杂度0(n3). 改进 : 对寻求目标数来说,三数之和要么比target 大 ,要么比target小,确定寻找方向很重要,可以对数组进行先排序,再找寻正确组合 首先,排序好的数组可以从两侧向中间逼近,最小数和最大数加和,如果和比target大,可以从

2018.10.02 练习赛

[T1 蒜头君当大厨] 题解: 显然差分约束,怕你看不出样例还疯狂暗示你= = \(code\): #include<stdio.h> #include<algorithm> #include<vector> #include<queue> #include<ctype.h> #define ll long long using namespace std; char buf[1<<20],*p1,*p2; inline char g

每日一句2018.10.02

Practice, under pressure, with focus, and with that glorious end goal in sight, makes perfect. 在压力下练习,专注,眼中只有美好的最终目标,才能造就完美. 原文地址:https://www.cnblogs.com/565261641-fzh/p/9736835.html

【leetcode刷题笔记】Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 题解:如果没有重复的元素,那么就可以根据target是否在某一半而扔掉另外一半.但是如果有

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t