掩码的运用
将字符串中的字母以大小写的形式实现全排列输出,数字直接输出
字符串中字母数量为n
外循环掩码为0-(2^n)-1
内循环字符串个数len;if(Character.isLetter(j)&&(mask&1<<k++))大写,else小写
class Solution {
public List letterCasePermutation(String s){
int len = s.length();
int m = 0;
for(int i = 0; i < len; i++){
if(Character.isLetter(s.charAt(i))){
m++;
}
}
List ans = new ArrayList();
for(int mask = 0; mask < (1<<m); mask++){
StringBuilder sb = new StringBuilder();
for(int j = 0, k = 0; j < len; j++){
if(Character.isLetter(s.charAt(j)) && (mask & (1 << k++)) != 0 ){
sb.append(Character.toUpperCase(s.charAt(j)));
}else{
sb.append(Character.toLowerCase(s.charAt(j)));
}
}
ans.add(sb.toString());
}
return ans;
}
}