zoukankan      html  css  js  c++  java
  • string permutation with upcase and lowcase

    Give a string, which only contains a-z. List all the permutation of upcase and lowcase. 
    For example, str = "ab",  the output should be 
    "ab", "aB", "Ab", "AB" 
    for str = "abc", the output should be 
    "abc", "abC", "aBc", "aBC", "Abc", "AbC", "ABc", "ABC" 

    [解题思路]

    本题与其他permutation题目区别在于结果中每位的字符都是固定的,仅仅是大小写的区别

    因而不需要循环来遍历,使字符串的每一位可以遍历任意一个值

     1 public class Solution {
     2     public static void main(String[] args) {
     3         List<String> result = new ArrayList<String>();
     4         String tmp = "";
     5         permutation(tmp, 0, 4, result);
     6         System.out.println(result);
     7     }
     8     private static void permutation(String tmp, int depth, int len,
     9             List<String> result) {
    10         if (depth == len) {
    11             result.add(tmp);
    12             return;
    13         }
    14 
    15         tmp += (char) ('a' + depth);
    16         permutation(tmp, depth + 1, len, result);
    17         tmp = tmp.substring(0, tmp.length() - 1);
    18 
    19         tmp += (char) ('A' + depth);
    20         permutation(tmp, depth + 1, len, result);
    21         tmp = tmp.substring(0, tmp.length() - 1);
    22 
    23     }
    24 }
  • 相关阅读:
    css--兼容写法整理
    vuerouter-7._路由高亮
    vuerouter-6_路由杂项
    vuerouter-5.参数传递
    vuerouter-4.编程式导航
    vuerouter-3.路由嵌套
    正则表达式
    STL容器迭代器失效分析
    coredump
    获取结构体成员偏移量
  • 原文地址:https://www.cnblogs.com/feiling/p/3307526.html
Copyright © 2011-2022 走看看