![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指offer
zz7549094
这个作者很懒,什么都没留下…
展开
-
剑指offer7:用两个栈实现队列
static Stack stack1 = new Stack<>(); static Stack stack2 = new Stack<>(); public static void push(int x){ stack1.push(x); } public static Integer pop(){ int temp = 0;原创 2017-03-30 16:37:30 · 243 阅读 · 0 评论 -
剑指offer22:栈的压入、弹出序列
import java.util.Stack;/** * Created by WHZ on 2017/4/1. */public class offer22 { boolean IsPopOrder(int[] pPush,int[] pPop,int length){ boolean bPossible = false; if(pPush!=原创 2017-04-01 11:20:10 · 163 阅读 · 0 评论 -
剑指offer23:从上到下打印二叉树
import java.util.LinkedList;import java.util.Queue;/** * Created by WHZ on 2017/4/1. */public class offer23 { private class TreeNode{ int val; TreeNode left; TreeNod原创 2017-04-01 11:35:20 · 167 阅读 · 0 评论 -
offer21:包含min函数的栈
import java.util.Stack;/** * Created by WHZ on 2017/4/1. */public class offer21 { Stack data = new Stack<>(); Stack min = new Stack<>(); void push(int val){ data.push(val);原创 2017-04-01 10:31:56 · 216 阅读 · 0 评论 -
剑指offer20:顺时针打印矩阵
/** * Created by WHZ on 2017/3/31. */public class offer20 { void PrintMatrixClockwisely(int[][] numbers){ if(numbers==null||numbers.length<=0) return; int start = 0; int原创 2017-03-31 17:16:22 · 179 阅读 · 0 评论 -
剑指offer19:二叉树的镜像
/** * Created by WHZ on 2017/3/31. */public class offer19 { private class TreeNode{ TreeNode left; TreeNode right; int val; } void MirrorRecursively(TreeNode pN原创 2017-03-31 16:10:35 · 231 阅读 · 0 评论 -
剑指offer18:树的子结构
/** * Created by WHZ on 2017/3/31. */public class offer18 { private class TreeNode{ TreeNode left; TreeNode right; int val; } boolean HasSubtree(TreeNode pRoot原创 2017-03-31 15:31:56 · 195 阅读 · 0 评论 -
剑指offer17:合并两个排序的链表
/** * Created by WHZ on 2017/3/31. */public class offer17 { private class ListNode{ int val; ListNode next; } ListNode Merge(ListNode pHead1,ListNode pHead2){原创 2017-03-31 15:20:02 · 444 阅读 · 0 评论 -
剑指offer15:链表中倒数第k个节点
/** * Created by WHZ on 2017/3/31. */public class offer15 { private class ListNode{ int val; ListNode next; } ListNode FindKthToTail(ListNode pHead,int k){ if(p原创 2017-03-31 15:00:16 · 286 阅读 · 0 评论 -
剑指offer24:二叉搜索树的后序遍历序列
/** * Created by WHZ on 2017/4/1. */public class offer24 { boolean VerifySequenceOfBST(int sequence[],int start,int end){ if(sequence==null||sequence.length<=0) return false; in原创 2017-04-01 15:28:14 · 189 阅读 · 0 评论 -
剑指offer25:二叉树中和为某一值得路径
import java.util.LinkedList;import java.util.Stack;/** * Created by WHZ on 2017/4/1. */public class offer25 { private class TreeNode{ int val; TreeNode left; TreeNod原创 2017-04-01 15:51:26 · 188 阅读 · 0 评论 -
剑指offer33:把数组排成最小的数
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;/** * Created by WHZ on 2017/4/6. */public class offer33 { public static String PrintMinNumber(int [] nu原创 2017-04-06 21:30:49 · 231 阅读 · 0 评论 -
剑指offer32:从1到n整数1出现的次数
/** * Created by WHZ on 2017/4/6. */public class offer32 { static int NumberOf1Between1AndN(int n){ if(n<=0) return 0; String str = String.valueOf(n); char[原创 2017-04-06 17:20:38 · 231 阅读 · 0 评论 -
剑指offer31:连续子数组的最大和
/** * Created by WHZ on 2017/4/6. */public class offer31 { static int FindGreatestSumOfSubArray(int[] data){ if(data==null||data.length<=0){ return 0; } int原创 2017-04-06 16:38:49 · 171 阅读 · 0 评论 -
剑指offer30:最小的k个数
解法1:static int Partion(int[] r,int first,int end){ int i = first; int j = end; int pivot = r[i]; while(i<j){ while((i=pivot) j--;原创 2017-04-06 16:13:17 · 214 阅读 · 0 评论 -
剑指offer29:数组中出现次数超过一半的数字
/** * Created by WHZ on 2017/4/6. */public class offer29 { //超过一半的数字为排序数组的中位数 static int Partion(int[] r,int first,int end){ int i = first; int j = end; int pivot =原创 2017-04-06 15:26:55 · 241 阅读 · 0 评论 -
剑指offer28:字符串的排列
import java.util.ArrayList;/** * Created by WHZ on 2017/4/6. */public class offer28 { static ArrayList res = new ArrayList(); String cur = new String(); public ArrayList Permutation原创 2017-04-06 09:56:06 · 280 阅读 · 0 评论 -
剑指offer27:二叉搜索树与双向链表
/** * Created by WHZ on 2017/4/5. */public class offer27 { private class BinaryTreeNode{ int val; BinaryTreeNode left; BinaryTreeNode right; } BinaryTreeNode C原创 2017-04-05 22:35:23 · 168 阅读 · 0 评论 -
剑指offer26:复杂链表复制
/** * Created by WHZ on 2017/4/5. */public class offer26 { private class ComplexListNode{ int val; ComplexListNode next; ComplexListNode sibling; } void CloneN原创 2017-04-05 11:38:54 · 201 阅读 · 0 评论 -
剑指offer14:将数组奇数移到偶数前面
/** * Created by WHZ on 2017/3/31. */public class offer14 { void ReorderOddEven(int[] data){ if(data==null||data.length==0) return; int begin = 0; int end = data.length原创 2017-03-31 11:41:08 · 385 阅读 · 1 评论 -
剑指offer13:O(1)时间删除链表节点
/** * Created by WHZ on 2017/3/31. */public class offer13 { private class ListNode{ int val; ListNode next; } void DeleteNode(ListNode pHead, ListNode pDelete){原创 2017-03-31 11:31:05 · 172 阅读 · 0 评论 -
剑指offer12:打印1到最大的n位数
void Print1ToMaxOfNDigits(int n){ if(n<=0) return; char[] number = new char[n]; for(int i=0;i<10;i++){ number[0] = (char)('0'+i); Print1ToMaxOfNDigitsRe原创 2017-03-31 11:13:25 · 159 阅读 · 0 评论 -
剑指offer43:n个骰子的点数
import java.util.Properties;/** * Created by WHZ on 2017/4/7. */public class offer42 { static int maxValue = 6; static void PrintProbability(int number){ if(number<1)原创 2017-04-07 16:03:48 · 259 阅读 · 0 评论 -
剑指offer41:和为s的两个数字VS和为s的连续正数序列
/** * Created by WHZ on 2017/4/7. */public class offer41 { boolean FindNumbersWithSum(int[] data,int length,int sum,int[] num1,int[] num2){ boolean found = false; if(length<1||原创 2017-04-07 14:51:53 · 202 阅读 · 0 评论 -
剑指offer40:数组中只出现一次的数字
/** * Created by WHZ on 2017/4/7. */public class offer40 { static void FindNumsAppearOnce(int[] data, int[] num1, int[] num2){ if(data==null||data.length<2) return;原创 2017-04-07 14:32:21 · 173 阅读 · 0 评论 -
剑指offer39:二叉树的深度
/** * Created by WHZ on 2017/4/7. */public class offer39 { private class TreeNode{ TreeNode left; TreeNode right; int val; } int TreeDepth(TreeNode root){原创 2017-04-07 14:18:07 · 193 阅读 · 0 评论 -
剑指offer38:数字在排序数组出现的次数
/** * Created by WHZ on 2017/4/7. */public class offer38 { int getUpper(int arr[], int key){//获取某个元素最后出现位置 int low = 0, high = arr.length - 1; //其实是一个递归迭代 while(low < h原创 2017-04-07 11:09:11 · 333 阅读 · 0 评论 -
offer37:两个链表的第一个公共节点
/** * Created by WHZ on 2017/4/7. */public class offer37 { private class ListNode{ ListNode next; int val; } ListNode FindFirstCommonNode(ListNode p1,ListNode p2){原创 2017-04-07 10:39:41 · 146 阅读 · 0 评论 -
剑指offer36:数组中的逆序对
/** * Created by WHZ on 2017/4/7. */public class offer36 { static int InverseParis(int[] data){ if(data==null||data.length<0) return 0; int length = data.length;原创 2017-04-07 10:30:50 · 192 阅读 · 0 评论 -
剑指offer35:第一个只出现一次的字符
/** * Created by WHZ on 2017/4/6. */public class offer35 { static char FirstNotRepeatingChar(String a){ if(a==null) return '\0'; int[] hashTable = new int[256];原创 2017-04-06 22:43:58 · 241 阅读 · 0 评论 -
offer44
import java.util.Arrays;/** * Created by WHZ on 2017/4/7. */public class offer44 { boolean IsContinuous(int[] numbers,int length){ if(numbers==null||length<1) return fal原创 2017-04-07 16:58:17 · 202 阅读 · 0 评论 -
offer45
import java.util.ArrayList;/** * Created by WHZ on 2017/4/7. */public class offer45 { static int LastRemaining(int n,int m){ if(n<1||m<1) return -1; int i=0;原创 2017-04-07 17:07:47 · 216 阅读 · 0 评论 -
剑指offer11:数值的整数次方
double Power(double base,int exponent){ if(base==0&&exponent<0){ return 0.0; } int absExponent = exponent; if(exponent<0) absExponent = -exponen原创 2017-03-31 10:51:24 · 157 阅读 · 0 评论 -
剑指offer10:二进制1的个数
int NumberOf1(int n){ int count = 0; while(n!=0){ ++count; n = (n-1)&n; } return count; }原创 2017-03-31 10:27:42 · 168 阅读 · 0 评论 -
剑指offer9:Fibonacci数列
long Fibonacci(int n){ if(n<=0) return 0; if(n==1) return 1; return Fibonacci(n-2)+Fibonacci(n-1); } long Fibonacci2(int n){ if(n<=0) return 0; if(n==1原创 2017-03-31 10:19:24 · 169 阅读 · 0 评论 -
剑指offer8:旋转数组的最小数字
题目:把一个数组最开始的若干元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。public static int Min(int[] number){ if(number==null|| number.length==0){ return 0; } int index1 =原创 2017-03-31 09:59:19 · 185 阅读 · 0 评论 -
剑指offer5:从尾到头打印链表
public void PrintListReversing(ListNode pHead){ Stack nodes = new Stack(); ListNode p = pHead; while(p!=null){ nodes.push(p); p=p.next; } while原创 2017-03-30 15:43:30 · 125 阅读 · 0 评论 -
剑指offer6:重建二叉树
static private class TreeNode{ int val; TreeNode left; TreeNode right; } static TreeNode Construct(int[] pre,int[] in){ if (pre == null || in == null || pre.le原创 2017-03-30 16:16:55 · 154 阅读 · 0 评论 -
剑指offer3:二维数组中的查找
思路:可以从右上角开始查找,若大于,列数减一,若小于,行数加一。public static boolean find(int[][] matrix,int num){ boolean found = false; if(matrix!=null){ int rows = matrix.length; int col原创 2017-03-30 15:24:19 · 178 阅读 · 0 评论 -
剑指offer50:求两个节点的最低公共祖先
该题可转化为求俩个链表的公共节点,求出根节点到两个节点的路径,将路径转化为链表,最近的公共节点就是最低公共祖先。public class offer50 { class TreeNode{ int val; TreeNode left; TreeNode right; } boolean getNodePath(TreeN原创 2017-03-23 15:13:33 · 394 阅读 · 0 评论