20210416 Likou question 17: letter combination of telephone number

 

1. Title

Given a string containing only the numbers , 2-9 , returns all the letter combinations it can represent. The answers can be returned in any order.

The mapping of numbers to letters is given as follows (the same as telephone keys). Note that 1 does not correspond to any letter.

Source: LeetCode link: https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number

2. Own ideas

First put the code of your own ideas, and then explain that there is a problem. Prepare to delete and rewrite it in the force deduction, and copy the code here first

  1. When I started to write like this, I could only complete two numbers, not others, so I changed it
class Solution {
    public List<String> letterCombinations(String digits) {
        String s[]  = new String[digits.length()];
      
        List<String> list = new ArrayList<>();
          if(s.length == 0 ) return list;

        //Write a switch case
        for(int i = 0 ; i <digits.length();i++)
        {
            switch(digits.charAt(i))
            {
                case '2':s[i] = "abc";break;
                case '3':s[i] = "def";break;
                case '4':s[i] = "ghi";break;
                case '5':s[i] = "jkl";break;
                case '6':s[i] = "mno";break;
                case '7':s[i] = "pqrs";break;
                case '8':s[i] = "tuv";break;
                case '9':s[i] = "wxyz";break;
                
            }
        }
      if(s.length == 1){
         for(int i = 0 ; i <s[0].toCharArray().length;i++)
         {
             String s1 = ""+s[0].toCharArray()[i];
             list.add(s1);
         }
        return list;
      }
        //Got s
        for(int  i = 0 ; i <s.length-1;i++)
        {
            char[] oneStr = s[i].toCharArray();
            char[] secStr = s[i+1].toCharArray();
            for(int j = 0 ; j<oneStr.length;j++)
            {
                for(int k = 0 ; k <secStr.length;k++)
                {
                    String ss = ""+oneStr[j]+secStr[k];
                    list.add(ss);
                }
            }
        }


    return list;

        

    }

}

      if(s.length == 1){
         for(int i = 0 ; i <s[0].toCharArray().length;i++)
         {
             String s1 = ""+s[0].toCharArray()[i];
             list.add(s1);
         }
      }
        //Got s
        for(int  i = 0 ; i <s.length-2;i++)//Traversal s
        {
            //String ss1 = ""+s[0].toCharArray()[i];
            //list.add(ss1);
            list =combineList(s[i],s[i+1]);
            
        }


    return list;

    }

    //In fact, they are strings that combine two by two. Don't be afraid
    public List<String> combineList(String s1,String s2)
    {
        char[] s1char = s1.toCharArray();
        char[] s2char = s2.toCharArray();
        List<String> li = new ArrayList<>();
        for(int i = 0 ; i < s1char.length; i++)
        {
            for(int j = 0 ; j<s2char.length;j++)
            {
                String s1s2 = ""+s1char[i]+s2char[j];
                li.add(s1s2);
            }
        }
        return li;
    }

}

After improving the code for the first time and writing it like this, it is found that there are still problems;

  1. The problem is that the list cannot be reused, so the list can only return two
  2. The parameter passed in must be a list and a string returns a list so that the list can be reused

The answer is to get the numbers corresponding to those letters, then to combine the 22 methods and return to list at last.

if(s.length == 1){
         for(int i = 0 ; i <s[0].toCharArray().length;i++)
         {
             String s1 = ""+s[0].toCharArray()[i];
             list.add(s1);
         }
         return list;
      }
        //Got s
        for(int  i = 0 ; i <s.length;i++)//Traversal s
        {
            
            list =combineList(list,s[i]);
            
        }

        return list;

    }
    //In fact, they are strings that combine two by two. Don't be afraid
    public List<String> combineList(List<String> s1,String s2)
    {
       List<String> str = new ArrayList<>();
       for(int i = 0 ; i < s2.length();i++)
       {
           if(s1.isEmpty())
           {
               str.add(s2.substring(i,i+1));
           }else
           {
               for(String s : s1)
               {
                   str.add(s+s2.substring(i,i+1));
               }
           }
       }
       return str;
    }

Among them, there is still a problem with the combineList. I didn't write it until I looked at other people's methods. Methods and ideas

  1. If the list is empty for the first time, then add the elements of s2 to str one by one
  2. Take out each element of the list, and then add another element of the string

For example, the list is ['a','b','c ']; If the string is' wxyz ', then the loop is aw ax ay az bw bx by bz This goes on

3. Summary

I feel that this blog is a little messy, because in the process of writing, I have a problem with my idea and want to delete it, but I don't remember anything, so I put the code in the blog, and finally wrote it. The initial idea is to write two combinations, but there are three numerical test examples to understand that there is a problem in writing this way, and then I used the method of two combinations, But my problem is when I combine in pairs. I'm a little confused and can't write. Among them, I'm not proficient in the use of substring. After reading other people's code, I know to write pairwise combination.

In addition, I have seen the results submitted by Xiali buckle, and my method already belongs to the normal distribution in the second echelon, indicating that this method still has a lot to correct. It's hard to write it out... (continue to improve, ha ha, I don't want to see the answer when I write it myself)

Keywords: leetcode leecode

Added by ShadowMetis on Sat, 05 Mar 2022 01:34:24 +0200