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 }
  • 相关阅读:
    6.让代码更具可读性
    5构造函数和析构函数
    4面向对象之类的继承
    3隐形的指针
    2面向对象之类的封装
    od快捷键
    1.纠结的c++
    101宏定义的其他用法
    100解剖宏定义函数
    99,printf scanf手动功能实现
  • 原文地址:https://www.cnblogs.com/feiling/p/3307526.html
Copyright © 2011-2022 走看看