剑指Offer 读书笔记 (Java实现)(更新中)

来源:互联网 发布:php开发的主要工作是 编辑:程序博客网 时间:2024/06/08 00:57

  • 剑指Offer 读书笔记 Java实现
    • 更新日志
    • 面试题2 - 实现Singleton模式
    • 面试题3 - 二维数组中的查找
    • 面试题4 - 替换空格
    • 面试题5 - 从尾到头打印链表
    • 面试题6 - 重建二叉树
    • 面试题7 - 用两个栈实现队列

剑指Offer 读书笔记 (Java实现)

更新日志:

8.6 至面试题7

面试题2 - 实现Singleton模式

通过静态变量实现,此种解决方案较好

 package _02_singleton;/** * Created by pokerface_lx on 16/8/5. */public class SingletonCommon {    private static SingletonCommon instance = null;    private SingletonCommon() {    }    public SingletonCommon getInstance() {        if (instance == null) {            synchronized (SingletonCommon.class) {                if (instance == null) {                    instance = new SingletonCommon();                }            }        }        return instance;    }}
package _02_singleton;/** * Created by pokerface_lx on 16/8/5. */public class SingletonBetter {    private SingletonBetter instance = new SingletonBetter();    private SingletonBetter() {    }    public SingletonBetter getInstance() {        return instance;    }}

解决思路:使用static关键字

面试题3 - 二维数组中的查找

在一个二维数组中,每一行都是按照从左至右递增的顺序排序,每一列都是按照从上至下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数

package _03_find_in_partially_sorted_matirx;import java.util.Scanner;/** * Created by pokerface_lx on 16/8/5. */public class FindInPartiallySortedMatirx {    public static void main(String[] args) {        int[][] nums = {{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};        int target;        Scanner scan = new Scanner(System.in);        while (scan.hasNext()) {            target = scan.nextInt();            findTarget(target, nums);        }    }    private static void findTarget(int target, int[][] nums) {        int currentX = 0;        int currentY = nums[0].length - 1;        while (currentX < nums.length && currentY >= 0) {            int current = nums[currentX][currentY];            if (current == target) {                System.out.println("exists, position:x=" + (currentX + 1) +                        ",y=" + (currentY + 1));                return;            }            if (current < target) {                ++currentX;            } else {                --currentY;            }        }        System.out.println("not exists");    }}
  • 如果右上角的数>要查找的数,则删除此列
  • 如果右上角的数<要查找的数,则删除此行

面试题4 - 替换空格

请实现一个函数,把字符串中的每个空格替换成“%20”

package _04_replace_blank;import java.util.Arrays;/** * Created by pokerface_lx on 16/8/5. */public class ReplaceBlank {    public static void main(String[] args) {        String str = " we are happy. ";        replaceBlankByArr(str);         replaceBlankBySB(str);    }    private static void replaceBlankByArr(String str) {        int blankNum = 0;        for (int i = 0; i < str.length(); i++) {            if (str.charAt(i) == ' ') {                blankNum++;            }        }        char[] chars = new char[str.length() + blankNum * 2];        int index = 0;        for (int i = 0; i < str.length(); i++) {            if (str.charAt(i) == ' ') {                chars[index++] = '%';                chars[index++] = '2';                chars[index++] = '0';            } else {                chars[index++] = str.charAt(i);            }        }        System.out.println(String.valueOf(chars));    }    private static void replaceBlankBySB(String str) {        String[] splStr = str.split(" ");        StringBuffer sb = new StringBuffer();        for (String s : splStr) {            sb.append(s).append("%20");        }        System.out.println(sb.toString());    }}

新建数组,拷贝

  • 使用数组的方式实现比StringBuffer快,重复10000次用数组需要30毫秒,用StringBuffer需要180毫秒,大概快6倍

面试题5 - 从尾到头打印链表

输入一个链表的头结点,从尾到头反过来打印每个结点的值

package _05_invert_list_sout;import model.ListNode;import java.util.Stack;/** * Created by pokerface_lx on 16/8/5. */public class InvertedSequence {    public static void main(String[] args) {        ListNode rootNode = new ListNode(0);        // 初始化 此处省略        Stack<Integer> stack = new Stack<>();        ListNode currentNode = rootNode;        while (currentNode.getNextNode() != null) {            stack.add(currentNode.getKey());            currentNode = currentNode.getNextNode();        }        while (!stack.isEmpty()){            System.out.println(stack.pop());        }    }}

使用栈的数据结构

面试题6 - 重建二叉树

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含有重复的数字

package _06_construct_binary_tree;import java.util.Arrays;/** * Created by pokerface_lx on 16/8/6. */public class ConstructBinaryTree {    public static void main(String[] args) {        int[] prOrder = {1, 2, 4, 7, 3, 5, 6, 8};        int[] inOrder = {4, 7, 2, 1, 5, 3, 8, 6};        Node rootNode = new ConstructBinaryTree().constructByPreAndIn(prOrder, inOrder);        System.out.println();    }    private Node constructByPreAndIn(int[] preOrder, int[] inOrder) {        Node rootNode = new Node();        rootNode.data = preOrder[0];        int leftLength = 0;        for (int i = 0; i < inOrder.length; i++) {            if (inOrder[i] == preOrder[0]) {                leftLength = i;                break;            }        }        if(leftLength != 0) {            int[] leftPreOrder = Arrays.copyOfRange(preOrder, 1, leftLength+1);            int[] leftInOrder = Arrays.copyOfRange(inOrder, 0, leftLength);            rootNode.leftNode = constructByPreAndIn(leftPreOrder, leftInOrder);        } else {            rootNode.leftNode = null;        }        if (leftLength != inOrder.length-1) {            int[] rightPreOrder = Arrays.copyOfRange(preOrder, leftLength + 1, preOrder.length);            int[] rightInOrder = Arrays.copyOfRange(inOrder, leftLength + 1, inOrder.length);            rootNode.rightNode = constructByPreAndIn(rightPreOrder, rightInOrder);        }        return rootNode;    }}class Node {    int data;    Node leftNode;    Node rightNode;}

递归

面试题7 - 用两个栈实现队列

package _07_queue_with_two_stacks;import java.util.Stack;/** * Created by pokerface_lx on 16/8/6. */public class QueueWithTwoStacks {    private static Stack pushStack = new Stack();    private static Stack popStack = new Stack();    public static void main(String[] args) {        QueueWithTwoStacks queueWithTwoStacks = new QueueWithTwoStacks();        queueWithTwoStacks.push(1);        queueWithTwoStacks.push(2);        queueWithTwoStacks.pop();        queueWithTwoStacks.push(3);        queueWithTwoStacks.pop();        queueWithTwoStacks.pop();    }    private void push(Object o) {        pushStack.push(o);    }    private void pop() {        if (popStack.isEmpty()) {            if (pushStack.isEmpty()) {                return;            }            while (!pushStack.isEmpty()) {                popStack.push(pushStack.pop());            }        }        System.out.println(popStack.pop());;    }}
0 0