剑指Offer之java版

题目1:设计一个类,我们只能生成该类的一个实例
//双检锁/双重校验锁(DCL,即 double-checked locking)
public class Singleton {
private volatile static Singleton sInstance = null;

private Singleton() {

}

public static Singleton getInstance() {
    if (sInstance == null) {
        synchronized (Singleton.class) {
            if (sInstance == null) {
                sInstance = new Singleton();
            }
        }
    }
    return  sInstance;
}
}
题目2:找出数组中的重复数字,时间复杂度O(n)
//对于一维数组排序满足O(n)时间复杂度,优先考虑hashmap
private static int findDuplicateNumber(int[] list) {
    int number = -1;
    //以数组的数作为key,如果key有重复,value递增
    HashMap map = new HashMap();
    for (int i=0; i entry: map.entrySet()) {
        if (entry.getValue() > 1) {
            number = entry.getKey();
        }
    }
    return number;
}
题目3:实现一个函数,把字符串每中的每个空格替换成“%20”。例如输入“we are happy.”,则输出“we%20are%20happy.”
public static void main (String[] args) {
    String str = "We are happy!";
    replaceBlank(str.toCharArray());
}

private static void replaceBlank(char[] arr) {
    int count = 0;
    int lenth = 0;
    //计算新数组长度,等于原长度加上空格数*2
    for (int i=0; i
题目4:有两个排序的数组A1和A2,内存在A1的末尾有足够多的空余空间容纳A2.请实现一个函数,把A2中的所有数字插入A1中,并且所有的数字是排序的。
public static void main(String[] args) {
    int[] A1 = {1, 3, 6, 7, 10, 23};
    int[] A2 = {2, 6, 8, 24};
    merged(A1, A2);
}

private static void merged(int[] A1, int[] A2) {
    int indexA1 = A1.length - 1;
    int indexA2 = A2.length - 1;
    int mergedIndex = A1.length + A2.length - 1;
    int[] mergedA = new int[A1.length + A2.length];
    //从后往前插入,也可以从前往后插入
    while (indexA1>=0 && indexA2>=0) {
        if (A1[indexA1] >= A2[indexA2]) {
            mergedA[mergedIndex--] = A1[indexA1--];
        } else {
            mergedA[mergedIndex--] = A2[indexA2--];
        }
    }

    while (indexA1>=0) {
        mergedA[mergedIndex--] = A1[indexA1--];
    }

    while (indexA2>=0) {
        mergedA[mergedIndex--] = A2[indexA2--];
    }

    for(int i=0; i
题目5:输入一个链表的头结点,从尾到头反过来打印每个节点的值
//链表节点定义
static class ListNode {
    int data;
    ListNode next;
}

public static void main(String[] args) {
    ListNode listNode = new ListNode();
    ListNode listNode2 = new ListNode();
    ListNode listNode3 = new ListNode();
    ListNode listNode4 = new ListNode();
    ListNode listNode5 = new ListNode();

    listNode.data = 1;
    listNode.next = listNode2;

    listNode2.data = 5;
    listNode2.next = listNode3;

    listNode3.data = 10;
    listNode3.next = listNode4;

    listNode4.data = 15;
    listNode4.next = listNode5;

    listNode5.data = 20;

    //打印链表
    printListNode(listNode);

    System.out.print("\n----------------------------\n");

    //使用堆反向打印链表
    printListReversByStack(listNode);

    System.out.print("\n----------------------------\n");

    //使用递归反向打印堆栈
    printListReversByRecursive(listNode);
}

//顺序打印链表
private static void printListNode(ListNode node) {
    if (node == null) {
        return;
    }

    while (node != null) {
        System.out.print(node.data);
        node = node.next;
        if (node != null) {
            System.out.print(" --> ");
        }
    }
}

//使用堆反向打印链表
private static void printListReversByStack(ListNode node) {
    Stack stack = new Stack();

    while (node != null) {
        stack.push(node);
        node = node.next;
    }

    while (!stack.isEmpty()) {
        System.out.print(stack.pop().data);

        if (!stack.isEmpty()) {
            System.out.print(" --> ");
        }
    }
}

//使用递归反向打印堆栈
private static void printListReversByRecursive(ListNode node) {
    if (node != null) {
        if (node.next != null) {
            printListReversByRecursive(node.next);
        }
    }

    System.out.print(node.data);
}
题目5:重构二叉树
public static class BinaryTreeNode {
    int value;
    BinaryTreeNode left;
    BinaryTreeNode right;
}

public  static  void  main(String[] args) {
    int[] preOrder = {1,2,4,7,3,5,6,8};
    int[] inOrder={4,7,2,1,5,3,8,6};
    BinaryTreeNode node = reConstruct( preOrder, inOrder );
    printTree( node );
}

private static BinaryTreeNode reConstruct (int[] prOrder, int[] inOrder) {
    if (prOrder == null || inOrder == null || prOrder.length != inOrder.length || prOrder.length < 1) {
        return  null;
    }
    return constrct(prOrder, 0, prOrder.length-1, inOrder, 0, inOrder.length-1);
}

/*
*  @param preOrder 前序遍历序列
*  @param preBegin 前序遍历开始位置
*  @param preEnd 前序遍历结束位置
*  @param inOrder 中序遍历序列
*  @param inBegin 中序遍历序列开始位置
*  @param inEnd 中序遍历序列结束位置
* */
private static BinaryTreeNode constrct (int[] preOrder, int preBegin, int preEnd, int[] inOrder, int inBegin, int inEnd) {
    if (preBegin > preEnd) return  null;

    int root = preOrder[preBegin];
    int index = inBegin;

    while (index <= inEnd && inOrder[index] != root) {
        index ++;
    }

    if (index > inEnd) {
        throw new RuntimeException( "invalid input index: " + index);
    }

    BinaryTreeNode node = new BinaryTreeNode();
    node.value = root;
    node.left = constrct( preOrder, preBegin+1, preBegin+index-inBegin, inOrder, inBegin, index-1 );
    node.right =  constrct( preOrder, preBegin+index-inBegin+1, preEnd, inOrder, index+1, inEnd );
    return  node;
}

/*
* 中序遍历递归打印
* */
private static void printTree (BinaryTreeNode node) {
    if (node != null) {
        printTree( node.left );
        System.out.print( node.value + " " );
        printTree( node.right );
    }
}

你可能感兴趣的:(剑指Offer之java版)