一、题目:字符串的排列

题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。

二、解题思路

2.1 核心步骤

  我们可以把一个字符串看成由两部分组成:第一部分为它的第一个字符,第二部分是后面的所有字符。在下图中,我们用两种不同的背景颜色区分字符串的两部分。

  Step1.把字符串分为两部分,一部分是字符串的第一个字符,另一部分是第一个字符以后的所有字符(有阴影背景的区域)。

  Step2.接下来我们求阴影部分的字符串的排列,拿第一个字符和它后面的字符逐个交换。

2.2 代码实现

    public static void Permutation(char[] str)
{
if (str == null)
{
return;
} Permutation(str, str, );
} public static void Permutation(char[] str, char[] begin, int startIndex)
{
if (startIndex == str.Length)
{
Console.WriteLine(str);
}
else
{
for (int i = startIndex; i < str.Length; i++)
{
char temp = begin[i];
begin[i] = begin[startIndex];
begin[startIndex] = temp; Permutation(str, begin, startIndex + ); temp = begin[i];
begin[i] = begin[startIndex];
begin[startIndex] = temp;
}
}
}

三、单元测试

3.1 测试用例

  (1)封装测试辅助方法

    public static void TestPortal(string str)
{
if (string.IsNullOrEmpty(str))
{
Console.WriteLine("Test for NULL begins:");
Permutation(null);
}
else
{
Console.WriteLine("Test for {0} begins:", str);
Permutation(str.ToCharArray());
} Console.WriteLine();
}

  (2)功能测试、特殊输入测试

    public static void Test1()
{
TestPortal(null);
} public static void Test2()
{
string str = "";
TestPortal(str);
} public static void Test3()
{
string str = "a";
TestPortal(str);
} public static void Test4()
{
string str = "ab";
TestPortal(str);
} public static void Test5()
{
string str = "abc";
TestPortal(str);
}

3.2 测试结果

作者:周旭龙

出处:http://edisonchou.cnblogs.com

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

最新文章

  1. 判断是否为gif/png图片的正确姿势
  2. Android 应用程序集成Google 登录及二次封装
  3. 转:ibatis动态sql
  4. 五子棋AI清月连珠开源
  5. 小数量宽带用户的福音,Panabit 云计费easyradius 接口隆重发布,PA宽带计费系统
  6. Dynamics AX 2012 R2 在报表上显示和打印条码
  7. H264 Profile
  8. 如何删除google流氓扩展(强制安装,并且无权限删除)
  9. HHVM简介(译)
  10. Java添加自定义注解
  11. C++重载赋值运算符
  12. java中多态的使用
  13. [UWP]依赖属性1:概述
  14. Web API框架学习——消息管道(二)
  15. canvas,html2canvas等合成图片不清晰问题
  16. Redis 部署主从哨兵 C#使用,实现自动获取redis缓存 实例2
  17. Python Cookbook(第3版)中文版:15.21 诊断分段错误
  18. uc伯克利人工分割图像.seg文件解析
  19. Jmeter二次开发代码(3)
  20. PHPsession工作机制以及销毁session

热门文章

  1. AgileEAS.NET SOA中间件平台更新日志 2015-04-28
  2. fatal error LNK1169: 找到一个或多个多重定义的符号
  3. WM_COPYDATA实现的不同进程间通信
  4. 将 xunit.runner.dnx 的 xml 输出转换为 Nunit 格式
  5. XACML学习
  6. cant create oci environment
  7. 浅谈CSS hack(浏览器兼容)
  8. Codeforces CF#628 Education 8 B. New Skateboard
  9. 同步机制 note
  10. [R语言]R语言计算unix timestamp的坑