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