![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指offer
chasein
这个作者很懒,什么都没留下…
展开
-
30 包含min函数的栈
Stack<Integer> dataStk = new Stack<>(); Stack<Integer> minStk = new Stack<>(); public void push(int node) { dataStk.push(node); if (minStk.isEmpty()) { minStk.push(node); }else if (min...原创 2020-09-24 11:05:21 · 128 阅读 · 0 评论 -
37 序列化二叉树
/** * 序列化二叉树 * 1 * / \ * 2 3 * / / \ * 4 5 6 */public class Solution_37 { static int index = -1; public static void main(String[] args) { TreeNode node1 = new TreeNode(1); TreeNode node2 = new原创 2020-09-24 10:31:32 · 103 阅读 · 0 评论 -
32 从上到下打印二叉树
题目一:不分行从上到下打印二叉树 public ArrayList<Integer> printTree(TreeNode root){ ArrayList<Integer> res = new ArrayList<>(); Queue<TreeNode> queue = new LinkedList<>(); if (root == null){ return res原创 2020-09-23 14:18:07 · 100 阅读 · 0 评论 -
14 剪绳子
public int maxCut(int length){ if (length < 2){ return 0; } if (length == 2){ return 1; } if (length == 3){ return 2; } int[] memo = new int[length + 1]; ...原创 2020-09-22 17:47:15 · 60 阅读 · 0 评论 -
8 二叉树的下一个节点
public static TreeNode getNext(TreeNode node){ if(node == null){ return null; } TreeNode temp = node.right; TreeNode nextNode; //如果一个节点有右子树 if(temp != null){ while(temp.left != nul...原创 2020-09-22 16:59:55 · 84 阅读 · 0 评论 -
22 链表中倒数第k个节点
public ListNode FindKthToTail(ListNode head,int k) { int num = 0; ListNode node = head; while(node != null){ node = node.next; ++num; } if(k > num || k == 0) return null; ListNo...原创 2020-09-19 17:32:56 · 53 阅读 · 0 评论 -
25 合并两个排序的链表
public ListNode Merge(ListNode list1,ListNode list2) { if(list1 == null || list2 == null){ return list1 = (list1 != null ? list1 : list2); } ListNode newNode; if(list1.val > list2.val){ newNode...原创 2020-09-19 17:11:03 · 58 阅读 · 0 评论 -
42 连续子数组的最大和
public int FindGreatestSumOfSubArray(int[] array) { int sum = 0; int maxSum = Integer.MIN_VALUE; for(int i = 0; i < array.length; ++i){ sum += array[i]; if(sum > maxSum){ ...原创 2020-09-19 16:48:09 · 65 阅读 · 0 评论 -
38 字符串的排列
public class Permutation { private static int num = 0; public static void main(String[] args) { char[] chars = {'a', 'l', 'i', 'b', 'a', 'b', 'a'}; char[] chas = {'a', 'b', 'a'}; permutation(chars, 0); System.out.pri原创 2020-09-19 15:25:06 · 109 阅读 · 0 评论 -
6 从尾到头打印链表
/** * 方法0,使用栈 * @param head * @return */ public static ArrayList<Integer> printReverse(ListNode head){ ArrayList<Integer> list = new ArrayList<>(); if (head == null) { return list; ...原创 2020-09-15 17:31:05 · 55 阅读 · 0 评论 -
4 二维数组中的查找
public static boolean find(int target, int[][] array){ if (array == null || array.length == 0) { return false; } int rows = array.length; int cols = array[0].length; int row = 0; int col = col...原创 2020-09-15 17:12:00 · 61 阅读 · 0 评论 -
3 数组中重复的数字
题目描述在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 public boolean duplicate(int numbers[],int length,int [] duplication) { if (numbers == null || numbers.length == 0) { return false; }原创 2020-09-15 16:55:05 · 76 阅读 · 0 评论 -
15 二进制中1的个数
static int numOf1InBinary(int n){ int count = 0; int flag = 1; // 循环32次 while (flag != 0){ if ((n & flag) != 0){ ++count; } flag = flag << 1; } ...原创 2020-09-15 14:33:43 · 186 阅读 · 0 评论 -
9 两个栈实现队列
import java.util.Stack;public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } pub原创 2020-09-15 10:35:54 · 51 阅读 · 0 评论 -
5 替换空格
方法1 空间复杂度O(n) public static String replaceSpace(StringBuffer str){ String s = str.toString(); char[] arr = s.toCharArray(); int spaceNum = 0; for (char c : arr){ if (c == ' '){ ++spaceNum;原创 2020-09-12 16:45:50 · 67 阅读 · 0 评论 -
55 二叉树的深度
public static int treeDepth(TreeNode root) { if (root == null){ return 0; } int leftDepth = treeDepth(root.left); int rightDepth = treeDepth(root.right); return Math.max(leftDepth, rightDepth) + 1; ...原创 2020-09-12 16:00:59 · 73 阅读 · 0 评论 -
18 删除链表的节点
删除一个节点 public static void delNode(ListNode head, ListNode delete){ // 不是尾节点 if (delete.next != null){ ListNode nxt = delete.next; delete.val = nxt.val; delete.next = nxt.next; }else if (delete原创 2020-09-12 15:19:07 · 81 阅读 · 0 评论 -
58 翻转字符串
题目描述把字符串 student. a am I变成正常的顺序题解方法1// 这个方法会把原始字符串两端的空格去掉,字符串中间的超过一个空格还会保持 public static String reverse(String str){ if (str.length() == 0) return str; str = str.trim(); String[] strs = str.split(" "); StringBuilder原创 2020-09-10 13:55:30 · 87 阅读 · 0 评论 -
24 反转链表
非递归实现(java)public class Solution { public ListNode ReverseList(ListNode head) { if(head == null){ return head; } ListNode before = null; ListNode cur = head; ListNode memo = head.next; while(原创 2020-09-08 09:14:03 · 189 阅读 · 0 评论 -
数字在排序数组中出现的次数
#include&lt;iostream&gt;#include&lt;vector&gt;using namespace std;int binarySearch(vector&lt;int&gt; data, int k, int low, int high){ while(low &lt;= high){ //必须加等号,否则此例中5将找不到 int mid原创 2018-12-17 22:50:09 · 75 阅读 · 0 评论 -
翻转单词顺序列
想了一种与书上不一样的解法,调了好几个小时,终于在牛客AC了。代码示例:#include<iostream>#include<vector>#include<string>#include<sstream>using namespace std;string ReverseSentence(string str) { stri...原创 2018-12-22 17:02:57 · 140 阅读 · 0 评论 -
统计数组中的逆序对
在归并排序的基础上添加几行代码就可以解决了,对比另一篇归并排序的代码:https://blog.csdn.net/weixin_40804971/article/details/84889575区别:为了统计逆序对,需要先将大值放进temp数组,另一篇里的归并排序是先将小值放进temp数组#include &amp;lt;iostream&amp;gt;using namespace std;int i...原创 2018-12-09 09:13:59 · 209 阅读 · 0 评论