剑指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
- 剑指Offer 读书笔记 (Java实现)(更新中)
- 剑指Offer读书笔记(持续更新中)
- 剑指offer练习题实现(java版,持续更新中)
- 剑指offer(纪念版)读书笔记【实时更新】
- Java编程思想 读书笔记(更新中)
- <<剑指Offer>>java实现(每日更新)
- (剑指offer)JAVA实现
- 剑指offer(Java实现)
- 《剑指offer》读书笔记(1)
- 剑指offer读书笔记(1)
- 剑指offer读书笔记(2)
- 剑指offer读书笔记(3)
- 剑指offer读书笔记(4)
- 剑指Offer读书笔记(3)
- 剑指Offer——编程题的Java实现(更新完毕……)
- 剑指Offer——编程题的Java实现(更新完毕……)
- 《Thinking in Java》读书笔记(更新中2005.11.08)
- 剑指offer编程Java实现pdf——持续更新中...
- Java设计模式之适配器模式
- 二叉树中和为某一值的路径
- js 原型,实例的理解
- GUI简单学习
- Tomcat8.x Servlet的创建和调用
- 剑指Offer 读书笔记 (Java实现)(更新中)
- C语言求一个3*4整数矩阵中的最大元素。
- HTTP协议在java中的运用
- redis集群新增节点:[ERR] Node is not empty
- unity 编辑器bug
- 学android开发 不得不去的好地方
- POJ3041——Asteroids(匈牙利算法)
- 电脑创建WIFI/无线热点之后, 手机QQ能上浏览器不能上网
- MVP架构 + EventBus 构建项目