java算法之字符组合排序

开发 后端 算法
Java算法包括冒泡排序,选择排序等。本文主要介绍的是字符组合排序,通过例子的形式,向大家说明字符组合排序的重点和难点。供参考。

Java字符组合排序,不是特别难的题目,暴力算和用图论算(深度遍历)都可以,结果是198.图论的话就是构造无向图,然后深度优先递归。
题目:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。

java代码:

 

  1. package com.graphic;  
  2. import java.util.Iterator;  
  3. import java.util.TreeSet;  
  4. public class CharSequence {  
  5. private String[] c = {"1","2","2","3","4","5"};  
  6. private int n = c.length;  
  7. private boolean[] visited = new boolean[n];  
  8. private int[][] g = new int[n][n];  
  9. private TreeSet ts = new TreeSet();  
  10. private String result = "";  
  11. public CharSequence(){  
  12. for(int i=0; i  
  13. for(int j=0; j  
  14. if(i == j) g[i][j] = 0;  
  15. else g[i][j] = 1;  
  16. }  
  17. }  
  18. g[3][5] = 0;  
  19. g[5][3] = 0;  
  20. }  
  21. public void depthFirst(int index){  
  22. visited[index] = true;  
  23. result += c[index];  
  24. if(result.length() == n){  
  25. ts.add(result);  
  26. resultresult = result.substring(0,result.length()-1);  
  27. visited[index] = false;  
  28. }  
  29. else{  
  30. for(int i=0; i  
  31. if(!visited[i] && g[index][i] == 1){  
  32. depthFirst(i);  
  33. }else continue;  
  34. }  
  35. resultresult = result.substring(0,result.length()-1);  
  36. visited[index] = false;  
  37. }  
  38. }  
  39. public void graphicGet(){  
  40. for(int i=0; i  
  41. depthFirst(i);  
  42. }  
  43. int count = 0;  
  44. System.out.print("图论的结果:");  
  45. Iterator it = ts.iterator();  
  46. while(it.hasNext()){  
  47. String tmp = it.next();  
  48. if(tmp.contains("35")) continue;  
  49. if(tmp.contains("53")) continue;  
  50. if(tmp.charAt(3) == '4') continue;  
  51. System.out.println(tmp);  
  52. count++;  
  53. }  
  54. System.out.println("共计:"+count+"个");  
  55. }  
  56. public void bruteForce(){  
  57. System.out.println("暴力搜的结果:");  
  58. int count = 0;  
  59. for(int i = 122345; i<543222; i++){  
  60. String tmp = ""+i;  
  61. if(tmp.charAt(3) == '4') continue;  
  62. if(tmp.contains("35")) continue;  
  63. if(tmp.contains("53")) continue;  
  64. if(tmp.contains("5") && tmp.contains("4") && tmp.contains("3") && tmp.contains("1"))  
  65. {  
  66. int index = tmp.indexOf("2");  
  67. if(index == -1) continue;  
  68. if(index == tmp.length()-1) continue;  
  69. if(tmp.substring(index+1).contains("2")){  
  70. System.out.println(tmp);  
  71. count++;  
  72. }  
  73. }  
  74. }  
  75. System.out.print("共计:"+count+"个");  
  76. }  
  77. public void recrusive(){  
  78. }  
  79. public static void main(String[] args) {  
  80. CharSequence cs = new CharSequence();  
  81. //图论的方法  
  82. cs.graphicGet();  
  83. //暴力搜索  
  84. cs.bruteForce();  
  85. }  

 

 通过例子,希望会对你有帮助。下面一篇将要介绍Java排序算法总结。

【编辑推荐】

  1. 浅谈Java读取Csv实践
  2. 三种常见的Java应用性能挑战
  3. 用Java做互联网开发优势何在?
  4. Java多线程程序设计详细解析
责任编辑:于铁 来源: 帮考网
相关推荐

2021-01-19 07:02:26

算法数据结构堆排序

2021-11-05 22:47:44

冒泡排序选择插入

2022-11-21 07:58:10

Java排序冒泡排序

2011-04-20 10:58:34

Java

2011-04-20 13:56:08

选择排序

2011-04-20 14:19:00

希尔排序

2011-04-20 14:07:37

冒泡排序

2011-04-20 15:06:44

堆排序

2011-04-20 15:20:03

快速排序

2021-06-09 09:06:52

Go语言算法

2009-08-11 09:19:52

C#选择排序C#算法

2021-08-04 08:56:34

语言Go排序

2011-04-20 14:29:07

归并排序

2011-04-20 12:49:44

插入排序

2022-01-06 16:20:04

Java排序算法排序

2011-04-20 16:05:15

基数排序

2022-04-06 08:58:39

归并排序Go算法

2014-08-13 11:04:02

搜索引擎排序算法

2017-05-10 16:01:39

推荐系统算法实践

2020-03-27 09:06:54

选择排序算法冒泡排序
点赞
收藏

51CTO技术栈公众号