
剑指offer
csdn_zjp
这个作者很懒,什么都没留下…
展开
-
股票的最大利润(Java实现)
public class E63MaxProfit { //股票的最大利润 public static int getMaxProfit(int[] numbers, int length){ if (numbers == null || length < 2) return 0; int min = numbers...原创 2019-11-12 20:02:47 · 1373 阅读 · 0 评论 -
圆圈中最后剩下的数字(Java实现)
public class E62LastRemainNumber { //圆圈中最后剩下的数字 /*解法一:构造环形链表*/ static class ListNode<T> { //环形链表节点 T value; ListNode<T> nextNode; ListNode(T ...原创 2019-11-12 20:02:05 · 213 阅读 · 0 评论 -
剑指offer第60题:n个骰子的点数(Java实现)
public class E60Probability { //剑指offer第60题:n个骰子的点数 /*打印出和为s的所有可能值及其概率*/ public static void printProbility(int number) { /*维护两个数组,通过前一个数组的值计算下一个数组的值*/ final int MAX_VALUE = ...原创 2019-10-31 21:30:41 · 251 阅读 · 0 评论 -
队列的最大值(Java实现)
import java.util.*;public class E59MaxInQueue { //队列的最大值 /*问题一:滑动窗口的最大值*/ public static Queue<Integer> getMaxInWindows(int[] queue, int windowsSize) { if (queue == null |...原创 2019-10-29 21:43:20 · 1132 阅读 · 0 评论 -
翻转字符串(Java实现)
public class E58ReverseString { //翻转字符串 /*问题一:翻转句子但单词内部不翻转*/ public static String reverseString(String str){ if (str == null) return null; char[] chars = str....原创 2019-10-28 21:03:24 · 326 阅读 · 0 评论 -
和为s的数字(Java实现)
public class E57NumbersWithSum { //和为s的数字 /*问题一:在递增数组中寻找和为s的两个数字*/ public static void findTwoNumbers(int[] numbers, int s){ if (numbers == null || numbers.length < 2) ...原创 2019-10-28 21:02:14 · 161 阅读 · 0 评论 -
数组中数字出现的次数(Java实现)
public class E56NumbersAppearOnce { //数组中数字出现的次数 /*问题一:两个只出现一次的数字+出现两次的其它数字*/ public static void findNumbers(int[] numbers, int length) { if (numbers == null || length <= 1) ...原创 2019-10-23 21:17:43 · 793 阅读 · 0 评论 -
二叉树的深度&判断是否为平衡二叉树(Java实现)
public class E55TreeDepth { //二叉树的深度 /*问题一:二叉树的最长路径*/ public static int getTreeDepth(BinaryTreeNode root) { if (root == null) return 0; int left = getTreeDept...原创 2019-10-23 21:16:18 · 177 阅读 · 0 评论 -
二叉搜索树的第K大节点(Java实现)
public class E54KthBSTNode { //二叉搜索树中第k个节点 private static int index; public static BinaryTreeNode getKthNode(BinaryTreeNode root, int k){ if (root == null) return null...原创 2019-10-22 20:36:02 · 404 阅读 · 0 评论 -
在排序数组中查找数字(Java实现)
public class E53FindInNumbersInOrder { //在排序数组中查找数字 /*问题一:数字在数组中出现的次数*/ public static int getTimes(int[] numbers, int length, int target) { int times = -1; if (numbers !=...原创 2019-10-22 20:34:13 · 306 阅读 · 0 评论 -
两个链表的第一个公共节点(Java实现)
public class E52FirstCommonListNode { //两个链表的第一个公共节点 private static class ListNode { int value; ListNode nextNode; } public static ListNode find(ListNode root1, ListN...原创 2019-10-22 20:31:25 · 221 阅读 · 0 评论 -
数组中的逆序对(Java实现)
public class E51CountInversePairs { //数组中逆序对的数量 public static int inversePairs(int[] data, int length) { if (data == null || length <= 0) return -1; int[] copy...原创 2019-10-16 21:14:11 · 296 阅读 · 0 评论 -
第一个只出现一次的字符(Java实现)
import java.util.HashMap;public class E50FirstNonredundantChar { //第一个不重复的字符 /*问题一:字符串中第一个不重复的字符*/ public static String getChar(String str){ if (str == null || str.length() == 0...原创 2019-10-16 21:13:13 · 146 阅读 · 0 评论 -
丑数(Java实现)
import static jdk.nashorn.internal.objects.NativeMath.min;public class E49UglyNumber { //找到第n个丑数,视1为第一个丑数 public static int getUglyNumber(int index){ if (index <= 0) ...原创 2019-10-12 21:03:46 · 249 阅读 · 0 评论 -
最长不含重复字符的子字符串(Java实现)
public class E48LongestSubstringWithoutDuplication { //最长不含重复字符的子字符串 public static int count(String str){ //记录返回值 int maxLength = 0; //记录当前长度 int currentLength...原创 2019-10-12 21:02:30 · 433 阅读 · 0 评论 -
用两个栈实现队列(Java实现)
用两个栈实现队列import java.util.Stack;public class E09CreateQueueByStack<T> { //用两个栈实现队列,采用泛型 private Stack<T> stack1 = new Stack<>(); private Stack<T> stack2 = new S...原创 2019-09-05 21:43:26 · 101 阅读 · 0 评论 -
二叉搜索树中和为某一值的路径(Java实现)
import java.util.Iterator;import java.util.LinkedList;public class E34FindPathDependOnSum { //二叉树中和为某一值的路径 private static class BinaryTreeNode{ int value; BinaryTreeNode le...原创 2019-09-28 20:50:02 · 160 阅读 · 0 评论 -
复杂链表的复制(Java实现)
public class E35CloneComplexListNode { //复杂链表的复制 //利用哈希表可以用空间换时间,或者分三步复制链表 private static class ComplexListNode{ //复杂链表结构 int value; ComplexListNode next; ...原创 2019-09-30 19:51:27 · 251 阅读 · 0 评论 -
二叉搜索树与双向链表(Java实现)
一、实现public class E36ConvertBSTToDeque { //将二叉搜索树转化为双向链表 private static class BinaryTreeNode{ int value; BinaryTreeNode left; BinaryTreeNode right; } private...原创 2019-09-30 21:31:53 · 112 阅读 · 0 评论 -
字符串的排列(Java实现)
public class E38Permutation { //打印字符串中字符的全排列 //将字符串分为第一个字符和剩下地字符两部分 public static void permutation(String str){ if (str == null) return; char[] chars = str.toC...原创 2019-10-03 20:00:53 · 87 阅读 · 0 评论 -
数组中出现超过一半的数字(Java实现)
public class E39MoreThanHalfNum { //数组中出现次数超过一半的数字 public static int getNumber(int[] numbers, int length){ if (numbers == null || length <= 0) throw new IllegalArgumen...原创 2019-10-04 20:35:37 · 169 阅读 · 0 评论 -
最小的k个数(Java实现)
import java.util.PriorityQueue;public class E40LeastKNumbers { //最小的K个数,利用大小为k的优先队列 public static void findLeastNumbers(int[] numbers, int length, int k){ if (numbers == null || le...原创 2019-10-04 20:36:24 · 207 阅读 · 0 评论 -
数据流中的中位数(Java实现)
import java.util.Comparator;import java.util.PriorityQueue;public class E41MedianOfDynamicArray<T extends Number> { //最小优先队列及最大优先队列 private PriorityQueue<T> min = new PriorityQ...原创 2019-10-06 21:20:34 · 227 阅读 · 0 评论 -
连续子数组的最大和(Java实现)
import java.util.LinkedList;import java.util.List;public class E42GreatestSumOfSubArray { //找出数组中连续子数组的最大和 //动态规划思想:max(f(data[i])),f(data[i])为以第i个元素结尾的连续子数组之和 public static int getGr...原创 2019-10-06 21:24:45 · 162 阅读 · 0 评论 -
1~n整数中1出现的次数(Java实现)
public class E43NumberOf1Between1AndN { //1-N整数中含1的个数 public static int numberOf1(int n){ //时间复杂度为NO(log(N)) if (n <= 0) return -1; int count = 0; for ...原创 2019-10-08 21:27:41 · 205 阅读 · 0 评论 -
数字序列中某一位的数字(Java实现)
public class E44findDigitInSequence { //数字序列中某一位的数字 public static int digitAtIndex(int index){ if (index < 0) return -1; int digits = 1; while(true){...原创 2019-10-08 21:28:23 · 255 阅读 · 0 评论 -
把数组排成最小的数(Java实现)
import java.util.Arrays;import java.util.Comparator;public class E45SplicingToBeMin { //将数组中的数字拼接成最新小的数字 public static String splicing(int[] numbers, int length){ if (numbers == n...原创 2019-10-10 20:44:58 · 129 阅读 · 0 评论 -
把数字翻译成字符串(Java实现)
public class E46Translation { //计算数字的翻译方式,1-'a'... 25-'z' public static int getTranslationCount(int number) { if (number < 0) return -1; char[] numbers = Stri...原创 2019-10-10 20:45:52 · 361 阅读 · 0 评论 -
礼物的最大价值(Java实现)
public class E47MaxValueOfGifts { //礼物的最大价值 public static int getMaxValue_Solution1(int[] value, int rows, int cols){ if (value == null || (rows <= 0 && cols <= 0)) ...原创 2019-10-10 20:46:29 · 700 阅读 · 0 评论 -
二叉搜索树的后序遍历序列(Java实现)
public class E33VerifySequenceOfBST { //判断某一序列是否是二叉搜索树的后序遍历序列 private class BinaryTreeNode{ int value; BinaryTreeNode left; BinaryTreeNode right; } public sta...原创 2019-09-28 20:48:36 · 110 阅读 · 0 评论 -
从上到下打印二叉树(Java实现)
问题描述从上到下打印如下结构的二叉树:private static class BinaryTreeNode{ int value; BinaryTreeNode left; BinaryTreeNode right;}问题一: 不分行从上到下打印二叉树public static void printFromTopToButton(BinaryTreeNode r...原创 2019-09-27 21:59:00 · 277 阅读 · 0 评论 -
栈的压入、弹出序列(Java实现)
import java.util.Stack;public class E31PushPopSequence { //根据入栈序列判断出栈序列是否正确 public static boolean isPopOrder(int[] push, int[] pop, int length){ if (push == null || pop == null || l...原创 2019-09-25 20:37:35 · 199 阅读 · 0 评论 -
二进制中1的个数(Java实现)
public class E15BitOperation { //计算一个整数的二进制表示中有多少个1 //Java中只有 有符号数 public static int numberOfOne_Solution1(int number){ //常规解法:左移1 int count = 0; int n = 1; ...原创 2019-09-16 19:42:42 · 199 阅读 · 0 评论 -
剪绳子(Java实现)
public class E14CuttingRope { //切割绳子,使得分段长度乘积最大 public static int maxResultsAfterCutting_Solution1(int length) { //基于动态规划 if (length < 2) return -1; if...原创 2019-09-10 21:57:41 · 776 阅读 · 0 评论 -
机器人的运动范围(Java实现)
public class E13RobotMovingRange { //机器人从坐标(0,0)开始运动,计算坐标位数不大于k的可到达格数 public static int getMovingCount(int threshold, int rows, int cols) { if (threshold < 0 || rows <= 0 || co...原创 2019-09-10 21:56:27 · 373 阅读 · 0 评论 -
矩阵中的路径(Java实现)
public class E12FindPath { //与C语言不同,此处用全局静态变量用作递归中比较字符的指针 private static int pathLength = 0; public static boolean hasPath(char[] matrix, int rows, int cols, char[] str){ if(matr...原创 2019-09-10 21:55:11 · 249 阅读 · 0 评论 -
旋转数组的最小数字(Java)
public class E11RotateInOrderNumbers { //寻找某数组旋转数组中最小数字 //特殊情况:移动0个元素;数组中存在重复的数字 public static int getMin(int[] numbers, int length){ if (numbers == null || length <= 0) ...原创 2019-09-10 21:53:53 · 88 阅读 · 0 评论 -
斐波那契数列(Java实现)
public class E10Fibonacci { //计算斐波那契数列的f(n),可涉及台阶问题、正方形覆盖问题等 public static long getFibonacciN(int n){ //自底向上求解 if (n == 0) return 0; if (n == 1) return 1; lo...原创 2019-09-05 21:44:52 · 108 阅读 · 0 评论 -
二叉树的下一个节点(Java实现)
public class E08FindNextInorderNode { //找到中序遍历序列中某个节点的下一个节点 private static class BinaryTreeNode{ //二叉树节点,有三个指针 private int value; BinaryTreeNode parentNode; B...原创 2019-09-04 21:50:05 · 162 阅读 · 0 评论 -
重构二叉树(Java实现)
问题描述本题需要根据二叉树的前序遍历序列和中序遍历序列重构二叉树,我用Java写了两种实现,但个人觉得第一种实现更为简洁。实现一public class BinaryTreeConstruction1 { //根据二叉树前序遍历及中序遍历结果重构二叉树 private static class BinaryTreeNode{ //二叉树节点 ...原创 2019-09-04 21:04:54 · 569 阅读 · 0 评论