java面试算法题(经典)
2008-02-18 20:00
567 查看
算法程序题:
该公司笔试题就1个,要求在10分钟内作完。
题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
基本思路:
1 把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。
2 显然这个结果集还未达到题目的要求。从以下几个方面考虑:
1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果
3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。
采用二维数组定义图结构,最后的代码是:
import java.util.Iterator;
import java.util.TreeSet;
public class TestQuestion {
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean
;
visited =falsh;
private int[][] a = new int
;
private String result = "";
private TreeSet TreeSet = new TreeSet();
public static void main(String[] args) {
new TestQuestion().start();
}
private void start() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}a[3][5] = 0;
a[5][3] = 0;
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
TreeSet .add(result);
}
for(int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}
注:郁闷,花了半个多小时才能写出来,还是看的提示!!!无向图,学数据结构时对他就不是很感冒
该公司笔试题就1个,要求在10分钟内作完。
题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
基本思路:
1 把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。
2 显然这个结果集还未达到题目的要求。从以下几个方面考虑:
1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果
3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。
采用二维数组定义图结构,最后的代码是:
import java.util.Iterator;
import java.util.TreeSet;
public class TestQuestion {
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean
;
visited =falsh;
private int[][] a = new int
;
private String result = "";
private TreeSet TreeSet = new TreeSet();
public static void main(String[] args) {
new TestQuestion().start();
}
private void start() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}a[3][5] = 0;
a[5][3] = 0;
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
TreeSet .add(result);
}
for(int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}
注:郁闷,花了半个多小时才能写出来,还是看的提示!!!无向图,学数据结构时对他就不是很感冒
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】
- 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
- 【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】
- 【LeetCode-面试算法经典-Java实现】【004-Median of Two Sorted Arrays(两个排序数组的中位数)】
- 【LeetCode-面试算法经典-Java实现】【108-Convert Sorted Array to Binary Search Tree(排序数组转变为平衡二叉树)】
- 【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】
- 【LeetCode-面试算法经典-Java实现】【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】
- 【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】
- JAVA经典算法40题(供面试所用)
- 【LeetCode-面试算法经典-Java实现】【223-Rectangle Area(矩形区域)】
- 笔试面试算法经典--判断二叉树是否是平衡二叉树(Java)
- 笔试面试算法经典--二叉搜索树转有序的双向链表(Java)
- 【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】
- 【LeetCode-面试算法经典-Java实现】【083-Remove Duplicates from Sorted List(排序的单链表中删除重复的结点)】
- 【LeetCode-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】
- 【LeetCode-面试算法经典-Java实现】【206-Reverse Linked List(反转一个单链表)】
- 【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】
- 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】