剑指offer
gongdileidechouzhu
这个作者很懒,什么都没留下…
展开
-
40、最小的k个数
import java.util.ArrayList; public class Solution { public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { ArrayList<Integer> dui = new ArrayList<Integer&...原创 2018-04-28 16:15:35 · 123 阅读 · 0 评论 -
8、二叉树的下一个节点
/* public class TreeLinkNode { int val; TreeLinkNode left = null; TreeLinkNode right = null; //父节点 TreeLinkNode next = null; TreeLinkNode(int val) { this.val = val; }原创 2018-04-03 17:05:46 · 123 阅读 · 0 评论 -
27、二叉树的镜像
1、递归的方法 将根节点的左右子树对调,再将左右子树的左右子树对调,依次递归 /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val;...原创 2018-04-03 16:27:10 · 115 阅读 · 0 评论 -
51、数组中的逆序对
思路: 利用归并排序的思想,先对前一半数组统计逆序对后从小到大排序,再对后一半数组统计逆序对后排序,再统计前后两个数组之间的逆序对。用空间换时间。 import java.util.Arrays; public class Solution { public int InversePairs(int [] array) { if((array==null) ||(ar...原创 2018-04-16 10:53:01 · 235 阅读 · 0 评论 -
39、数组中出现次数超过一半的数字
1、利用map,遍历数组记录下每个数字出现的次数import java.util.Map; import java.util.HashMap; public class Solution { public int MoreThanHalfNum_Solution(int [] array) { Map<Integer,Integer> m = new HashMap();原创 2018-03-28 10:11:17 · 102 阅读 · 0 评论 -
42、连续子数组的最大和
1、用数组保存起来p220的f(i)public class Solution { public int FindGreatestSumOfSubArray(int[] array) { if(array.length<1) return 0; int max = array[0]; int[] endWithIndexISum = new i原创 2018-03-28 09:31:37 · 143 阅读 · 0 评论 -
35、复杂链表的赋值
public RandomListNode Clone(RandomListNode pHead) { if(pHead==null) return null; //将新链表每个节点插在旧链表对应的每个节点后面 RandomListNode oldNode = pHead; while(oldNode!=null)原创 2018-04-16 09:46:10 · 172 阅读 · 0 评论 -
29、顺时针打印矩阵
import java.util.ArrayList; public class Solution { public ArrayList<Integer> result = new ArrayList<Integer>(); public ArrayList<Integer> printMatrix(int [][] matrix) { if(matrix.lengt原创 2018-03-27 21:13:08 · 113 阅读 · 0 评论 -
15、二进制中1的个数
public class Solution { public int NumberOf1(int n) { int result = n; int count = 0; while(result!=0) { result = ((result-1) & (result)); cou原创 2018-03-27 19:54:26 · 334 阅读 · 0 评论 -
31、栈的压入、弹出序列
import java.util.ArrayDeque; public class Solution { public boolean IsPopOrder(int [] pushA,int [] popA) { if((pushA.length<=0) || (popA.length<=0)) return false; ArrayDeque<Integer原创 2018-04-10 10:32:43 · 101 阅读 · 0 评论 -
10、(二)青蛙跳台阶
1、递归的方式 public class Solution { public int JumpFloor(int target) { if(target<1) return 0; if(target==1) return 1; if(target==2) return 2; return JumpFloor(tar...原创 2018-03-27 19:22:49 · 193 阅读 · 0 评论 -
10、斐波那契数列
1、用递归的方式,很慢,重复结点求的太多public class Solution { public int Fibonacci(int n) { if(n<1 || n>39) return 0; else if(n==1) return 1; else return Fibonacci(n-1)+Fibonacci原创 2018-03-27 19:05:06 · 186 阅读 · 0 评论 -
5、替换空格
public class Solution { public String replaceSpace(StringBuffer str) { if(str.length()<=0) return str.toString(); int spaceCount = 0; String result; StringBuffer...原创 2018-04-09 20:53:05 · 95 阅读 · 0 评论 -
62、圆圈中最后剩下的数字
1、简便方法,通过删去第一个数找递归关系 public class Solution { public int LastRemaining_Solution(int n, int m) { if((n<1) || (m<1)) return -1; int result = 0; for(int i=2;i<=n;i++)...原创 2018-04-09 20:16:03 · 175 阅读 · 0 评论 -
28、对称的二叉树
/* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { boolea...原创 2018-04-04 10:18:10 · 111 阅读 · 0 评论 -
32、从上到下打印二叉树
1、题目一:不分行从上到下打印二叉树 import java.util.ArrayList; import java.util.ArrayDeque; /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(in...原创 2018-04-04 15:08:30 · 107 阅读 · 0 评论 -
43、1-n整数中1出现的次数
整体思路: 该位为0,该位大于1,该位等于1,分开处理 public class Solution { public int NumberOf1Between1AndN_Solution(int n) { int result = 0; int m = 0; int s = 0; while(n!=0) {...原创 2018-04-28 15:43:41 · 126 阅读 · 0 评论 -
59、滑动窗口的最大值
1、滑动窗口的最大值 思路: deque中只保存可能会是最大值的数,每新加入一个数,就把队列中比该数大的数全部删除,因为前面的数已经不可能成为最大数import java.util.ArrayDeque; import java.util.ArrayList; public class Solution { public ArrayList<Integer> maxInWindows(i原创 2018-04-20 16:47:32 · 138 阅读 · 0 评论 -
18、删除链表中重复的节点
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } } */ public class Solution { public ListNode deleteDuplication(ListNode pHead原创 2018-05-02 15:26:09 · 247 阅读 · 0 评论 -
20、表示数值的字符串
public class Solution { public boolean isNumeric(char[] str) { String s=String.valueOf(str); //? 出现一次或零次 //+ 至少出现一次 //* 出现任意多次 //\\ 后面接特殊字符不转义 //[原创 2018-05-02 14:42:36 · 253 阅读 · 0 评论 -
19、正则表达式匹配
整体思路:当模式中的第二个字符不是“*”时: 1、如果字符串第一个字符和模式中的第一个字符相匹配,那么字符串和模式都后移一个字符,然后匹配剩余的。 2、如果 字符串第一个字符和模式中的第一个字符相不匹配,直接返回false。而当模式中的第二个字符是“*”时: 如果字符串第一个字符跟模式第一个字符不匹配,则模式后移2个字符,继续匹配。如果字符串第一个字符跟模式第一个字符匹配,可以有3种匹配方式:原创 2018-05-02 11:07:49 · 285 阅读 · 0 评论 -
36、二叉搜索树与双向链表
整体思路: 函数f对该树排序,返回值为链表的头节点 f(左子树) 将左子树最后一个结点和root连接 f(右子树) 将右子树头结点和root连接 public class Solution { public TreeNode Convert(TreeNode pRootOfTree) { if(pRootOfTree==null) return pRootOfT...原创 2018-05-01 16:47:44 · 143 阅读 · 0 评论 -
38、字符串的排列
import java.util.ArrayList; import java.util.HashSet; public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<String> result = new ArrayList<Str...原创 2018-05-01 16:44:10 · 215 阅读 · 0 评论 -
50、第一个只出现一次的字符
题目一:字符串中第一个只出现一次的字符import java.util.ArrayList; import java.util.HashMap; import java.util.Map;public class Solution { public char f(String s) { if(s.length()==0) return ' ';原创 2018-04-12 21:10:20 · 109 阅读 · 0 评论 -
45、把数组排成最小的数
import java.util.Arrays; import java.util.Comparator; public class Solution { public String PrintMinNumber(int [] numbers) { if(numbers.length==0) return ""; String[] num = new Stri原创 2018-04-12 15:48:40 · 145 阅读 · 0 评论 -
41、数据流中的中位数
思路: 前一半数用最大堆保存,后一半数用最小堆保存,并且最大堆的所有数小于最小堆的所有数,这样求中位数就是当最大堆最小堆堆顶的和的一半。import java.util.ArrayList; public class Solution { ArrayList<Integer> max = new ArrayList<Integer>(); ArrayList<Integer> mi原创 2018-04-18 10:09:50 · 223 阅读 · 1 评论 -
9、两个栈实现队列
题目一:用两个栈实现一个队列 解法一:stack1永远用作存放元素,stack2在pop时作为转换工具import java.util.Stack;public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>();原创 2018-04-12 10:15:33 · 113 阅读 · 0 评论 -
11、旋转数组的最小数字
1、遇到的第一个比前一个数字小的数就是result,不存在就是array[0]是resultpublic class Solution { public int minNumberInRotateArray(int [] array) { if(array.length==0) return 0; int num = array[0]; for原创 2018-04-11 23:50:15 · 145 阅读 · 0 评论 -
37、序列化二叉树
/* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { int i=...原创 2018-04-04 16:48:02 · 120 阅读 · 0 评论 -
61、扑克牌中的顺子
import java.util.Arrays; public class Solution { public boolean isContinuous(int [] numbers) { //考虑输入为空的情况 if(numbers.length==0) return false; Arrays.sort(numbers); ...原创 2018-04-09 19:30:07 · 182 阅读 · 0 评论 -
合并两个排序的链表
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode Merge(ListNode list1,ListNode list原创 2018-03-27 11:26:41 · 67 阅读 · 0 评论 -
24、反转链表
1、非递归,利用三个指针分别指向当前节点前一个,当前节点,当前节点下一个/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNod原创 2018-03-27 10:59:54 · 110 阅读 · 0 评论 -
58、左旋转字符串
public class Solution { public String LeftRotateString(String str,int n) { if(str.length() == 0) return ""; int k=n%(str.length()); String s1=str.substring(k); Strin原创 2018-03-20 17:37:34 · 127 阅读 · 0 评论 -
58、翻转字符串
1、不利用java自带的函数import java.util.ArrayDeque; public class Solution { public String ReverseSentence(String str) { if(str.length()==0) return ""; String result; StringBuffer s=n原创 2018-03-20 17:18:38 · 126 阅读 · 0 评论 -
57、输出和为S的连续正数序列
import java.util.ArrayList; public class Solution { public ArrayList<ArrayList<Integer> > FindContinuousSequence(int sum) { ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Inte原创 2018-03-20 15:39:34 · 157 阅读 · 0 评论 -
67、把字符串转换成整数
此处没有考虑到大整数和溢出问题,但是考虑了第一个字符是正负号,非数字的全部返回0public class Solution { public int StrToInt(String str) { char[] value = str.toCharArray(); int result=0; boolean sign=true; //用来判断原创 2018-03-20 14:41:37 · 157 阅读 · 0 评论 -
52、两个链表的第一个公共结点
1、第一种方法用栈,但是需要辅助空间 2、第二种方法先遍历两个链表求出长度,让一个指针先走长度差的步数,另一个指针再走,相遇即第一个公共结点/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public原创 2018-03-20 11:06:04 · 93 阅读 · 0 评论 -
21、调整数组顺序使奇数位于偶数前面
1、能保证原奇数之间的顺序不变,原偶数之间的顺序不变import java.util.ArrayList; public class Solution { public void reOrderArray(int [] array) { if(array.length<2) return; ArrayList<Integer> odd = new ArrayL原创 2018-03-23 21:51:40 · 121 阅读 · 0 评论 -
23、链表是否有环、链表环的入口
1、判断链表是否有环 设置快慢指针,快指针每次走两步,慢指针每次走一步,追上就有环class ListNode1 { Integer data; ListNode1 next=null; public ListNode1(Integer data) { this.data=data; } } public class testCycleNod原创 2018-03-20 09:09:58 · 117 阅读 · 0 评论 -
16、数值的整数次方
public class Solution { //将无符号exponent传入 public double Power1(double base, int exponent) { double result; if(exponent == 1) return base; if((exponent & 1) == 1)原创 2018-03-22 21:26:37 · 139 阅读 · 0 评论