【java】784. Letter Case Permutation

xiaoxiao2021-02-28  27

问题原文点击打开链接

对于这道题的思考,我是把字符串想象成树的结构去理解的。如果遇到字母字符,就意味着该节点有两个儿子。否则一个儿子。

要保留前面的结果,其实也就是回溯法的思想。

List<String> res = new LinkedList<>();//设置一个全局变量作为返回值 public List<String> letterCasePermutation(String S) { String current = ""; hel(current,0,S); return res; }     //转换字母大小写 public char tran(char x){ if (x>90){ return (char)(x-32); }else{ return (char)(x+32); } } public void hel(String current,int start,String inti){ if (start == inti.length()){ String tmp = new String(current); res.add(tmp); return; } if (inti.charAt(start) >= 48 && inti.charAt(start) <= 57){ current += inti.charAt(start); hel(current,start+1,inti); }else{ current += inti.charAt(start); hel(current,start+1,inti); current = current.substring(0,current.length()-1); current += tran(inti.charAt(start)); hel(current,start+1,inti); } }
转载请注明原文地址: https://www.6miu.com/read-2627510.html

最新回复(0)