`
文章列表
see   http://eriol.iteye.com/blog/1186408 http://blog.csdn.net/vinglemar/article/details/3605813 import java.util.ArrayList; import java.util.List; public class FloydInGraph { /** * 图 邻接矩阵 最短路径 弗洛伊德算法 */ private static int INF=Integer.MAX_VALUE; //dist[i][j]=INF ...
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Stack; public class Graph { /**关键字:图 邻接表 深度优先遍历 广度优先遍历 最短路径 * key words:Graph Adjacent-list depthFirstVisit breadthFirstVisit getCheapestPath * 1.Graph is a collection of Vertex and Ed ...
import java.util.ArrayList; import java.util.List; public class Combination { public static void main(String[] args) { char[] a = { 'a', 'b', 'c' }; List<Character> list = new ArrayList<Character>(); for (int i = 1, len = a.length; i <= len; i++) { combine(a, 0 ...
see http://www.leetcode.com/2011/08/reverse-bits.html public class ReverseBitsOfInteger { /** * like reversing a string. * we swap the bits in (0,N-1),(1,N-2)...... * we do it with XOR * */ public static void main(String[] args) { int y = 0x01010101; int z = reverseBit ...
the idea is from:http://blog.csdn.net/zhanxinhang/article/details/6731134 public class MaxSubMatrix { /**see http://blog.csdn.net/zhanxinhang/article/details/6731134 * Q35 求一个矩阵中最大的二维矩阵 ( 元素和最大 ). 如 : 1 2 0 3 4 2 3 4 5 1 1 1 5 3 0 中最大的是 : 4 5 5 3 three solutions: 1.brutalFind:calc ...
public class EightQueen { /** * 八皇后问题 * obviously,the location of a queen includes two index: row and column * 1.the eight queens should be put in different rows and different columns * 2.so,a integer array-(we name it columnIndex[8]) * the index of array is from 0 to 7,we c ...
import java.util.Arrays; public class MinSumASumB { /** * Q32.有两个序列a,b,大小都为n,序列元素的值任意整数,无序. * * 要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。 * 例如: * int[] a = {100,99,98,1,2,3}; * int[] b = {1, 2, 3, 4,5,40}; * * 求解思路: * 当前数组a和数组b的和之差为 A = sum(a) - sum(b) a的第i个 ...
public class BuildTreePreOrderInOrder { /** * Build Binary Tree from PreOrder and InOrder * _______7______ / \ __10__ ___2 / \ / 4 3 _8 \ / 1 11 */ public static void ma ...
public class PalindromeInt { /** * PalindromeInt,like 1,121,12321.... * you should consider the possibility that the reversed number might overflow * eg. 1...................9,after reversing,9 comes to first,and...you know that. * */ public static void main(String[] args) { ...
import java.util.ArrayList; import java.util.List; import java.util.Stack; public class CombinationToSum { /* 第21 题 2010 年中兴面试题 编程求解: 输入两个整数 n 和 m ,从数列 1 , 2 , 3.......n 中随意取几个数 , 使其和等于 m , 要求将其中所有的可能组合列出来 . * two solutions * permutation01:Recursion.easy to write and read-->p ...
import java.util.Random; public class CrapsGame { /** * *一个简单的赌*博游戏,游戏规则如下: *玩家掷两个骰子,点数为1到6,如果第一次点数和为7或11,则玩家胜, *如果点数和为2、3或12,则玩家输, *如果和为其它点数,则记录第一次的点数和,然后继续掷骰,直至点数和等于第一次掷出的点数和,则玩家胜, *如果在这之前掷出了点数和为7,则玩家输。 *it looks difficult,but it's easy actually */ public static ...
//use recursion public static void mirrorHelp1(Node node){ if(node==null)return; swapChild(node); mirrorHelp1(node.getLeft()); mirrorHelp1(node.getRight()); } //use no recursion but stack public static void mirrorHelp2(Node node){ if(node==null)return; Stack<Node> st ...
import java.util.LinkedList; import java.util.List; import java.util.Stack; public class BinTreeTraverse { //private int[] array={ 1, 2, 3, 4, 5, 6, 7, 8, 9 }; private int[] array={ 10,6,14,4,8,12,16};//BinarySearchTree private static List<Node> nodeList=null; public static ...
two cursors. Make the first cursor go K steps first. /* * 第 13 题:题目:输入一个单向链表,输出该链表中倒数第 k 个节点 */ public void displayKthItemsBackWard(ListNode head,int k){ ListNode p1=head,p2=head; while(--k>0){ p1=p1.next; } while(p1.next!=null){ p1=p1.next; p2=p2.next; } S ...
单个链表:         a.判断是否有环:快慢法(p1=p2=head,p1=p1.next,p2=p2.next.next)         b.环的第一个节点:假设p1,p2在环内的某点p相遇,则p1从p点继续遍历,p2从头节点开始遍历,两者相等的第一个点即为所求(证明略)         c.反向输出链表:利用递归。(printNode(p.next);print(p)) 两个链表(均无环): a.是否相交:各自遍历至最后一个节点,若最后一个节点相等,则相交 b.相交的第一个点: solution1 人工构环。将B链表的表尾和A链表的表头相连,转化为求单个有环链表 ...
Global site tag (gtag.js) - Google Analytics