Leetcode Letter Combinations of a Phone Number

Viewed 37

I want to know why I can't add into List ans which is a global variables.

enter image description here This is the picture when I compile below codes;


class Solution {

List<String> ans = new ArrayList<>();
public List<String> letterCombinations(String digits) {
    
    
    char[] arr = digits.toCharArray();
    if(arr.length == 0) return ans;
    
    List<String> str = new ArrayList<>();
    
    for(int i=0;i< arr.length; i++){
        if(arr[i] =='2') str.add("abc");
        if(arr[i] == '3') str.add("def");
        if(arr[i] == '4') str.add("ghi");
        if(arr[i] == '5') str.add("jkl");
        if(arr[i] == '6') str.add("mno");
        if(arr[i] == '7') str.add("pqrs");
        if(arr[i] == '8') str.add("tuv");
        if(arr[i] == '9') str.add("wxyz");
    }
    
    Solution S = new Solution();
    S.dfs(str, "", arr.length, 0);
    
    return ans;
    
}
public void dfs(List<String> str, String output, int length, int depth){
    if(depth == length){
        String putIn = output;
        ans.add(putIn);
        System.out.print(output + ",");
        return;
    }
    String temp = str.get(depth);
    for(int i=0; i<3; i++){
        String strTemp = output;
        strTemp +=temp.toCharArray()[i];
        dfs(str, strTemp, length, depth+1);
        
    }
}

} ******************************************************************88

1 Answers

Here is a modified version of your code that incorporates this and proper use of the method without making more new classes of Solution.

public class Solution {
    List<String> ans = new ArrayList<>();


    public static void main(String[] args){
        Solution sol = new Solution();
        System.out.println();
        System.out.println(sol.letterCombinations("23"));  //this works if you return S.ans or this.ans
        System.out.println(sol.ans.toString());  //this only works if you are working with this.ans
        //because you are rewriting the ans of "this" class, not the class of S

    }

    public List<String> letterCombinations(String digits) {


        char[] arr = digits.toCharArray();
        if(arr.length == 0) return this.ans;

        List<String> str = new ArrayList<>();

        for(int i=0;i< arr.length; i++){
            if(arr[i] =='2') str.add("abc");
            if(arr[i] == '3') str.add("def");
            if(arr[i] == '4') str.add("ghi");
            if(arr[i] == '5') str.add("jkl");
            if(arr[i] == '6') str.add("mno");
            if(arr[i] == '7') str.add("pqrs");
            if(arr[i] == '8') str.add("tuv");
            if(arr[i] == '9') str.add("wxyz");
        }

        Solution S = new Solution();
        S.dfs(str, "", arr.length, 0); //used in conjunction with return S.ans

        //removed Solutions class
        //dfs(str, "", arr.length, 0); //modified to call the method directly used in conjunction with return this.ans

        return S.ans;
        //return this.ans;

    }
    public void dfs(List<String> str, String output, int length, int depth){
        if(depth == length){
            String putIn = output;
            this.ans.add(putIn);
            System.out.print(output + ",");
            return;
        }
        String temp = str.get(depth);
        for(int i=0; i<3; i++){
            String strTemp = output;
            strTemp +=temp.toCharArray()[i];
            dfs(str, strTemp, length, depth+1);

        }
    }
}
Related