C#学习笔记:生成字符串的全排列
来源:互联网 发布:易语言断网瞬移源码 编辑:程序博客网 时间:2024/06/07 20:42
问题描述:
根据输入的字符串,对字符串进行全排列,输出所有字符串。
例如,输入"ab",输出"ab", "ba";输入"abc",输出"abc", "acb", "bac", "bca", "cab", "cba"。
代码:
static void Main() { string word = Console.ReadLine(); while (!string.IsNullOrEmpty(word)) { foreach (var item in getFullPermutation(word)) { Console.Write(item + ", "); } Console.WriteLine(); word = Console.ReadLine(); } } static IEnumerable<string> getFullPermutation(string word) { if (word.Length < 2) yield return word; else { char current = word[0]; foreach (var item in getFullPermutation(word.Substring(1))) { char[] tmp = new char[word.Length]; for (int i = 0; i < item.Length; i++) tmp[i] = item[i]; tmp[word.Length - 1] = current; for (int i = item.Length; i >= 0; i--) { if (i < item.Length) { tmp[i + 1] = tmp[i]; tmp[i] = current; } string result = string.Empty; for (int j = 0; j < tmp.Length; j++) result += tmp[j]; yield return result; } } } }
0 0
- C#学习笔记:生成字符串的全排列
- 生成字符串的全排列
- 生成字符串的全排列
- 生成字符串的全排列
- 字符串全排列生成算法
- 算法学习-字符串的全排列
- 全排列的生成
- 全排列的生成
- 全排列的生成
- 学习笔记-全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- debian wheezy 安装Latex 详细过程
- 机器学习中的数学(1)-回归(regression)、梯度下降(gradient descent)
- HDU 1150 Machine Schedule (最小点覆盖)
- MySQL索引背后的数据结构及算法原理
- 程矢Axure夜话:中继器系列视频教程之中继器下拉菜单联动
- C#学习笔记:生成字符串的全排列
- linux+lamp+phpmyadmin
- Spring注解@Component、@Repository、@Service、@Controller区别
- CFNetwork 基本结构
- Codeforces 482B. Interesting Array 线段树
- batch gradient descent(批量梯度下降) 和 stochastic gradient descent(随机梯度下降)
- IOS委托详细说明
- GCC __sync_*系列的built-in函数,无锁化编程
- C++中内联函数与宏