- 博客(117)
- 资源 (6)
- 收藏
- 关注
原创 Java中PriorityQueue优先队列
PriorityQueue使用跟普通队列一样,唯一区别是PriorityQueue会根据排序规则决定谁在队头,谁在队尾。PriorityQueue 通过add方法添加,通过poll方法一次获得一个最小元素,实现原理小顶堆,也就是说元素按照小顶堆结构存放。public static void main(String[] args) { PriorityQueue<Integer> q = new PriorityQueue<>(); //入列 q.of
2021-02-23 10:59:24
204
原创 剑指Offer-40最小的k个数
public int[] getLeastNumbers(int[] arr, int k) { Arrays.sort(arr); return Arrays.copyOf(arr, k);}当然不能直接调API复习一下快排吧public int[] getLeastNumbers(int[] arr, int k) { quickSort(arr, 0, arr.length - 1); return Arrays.copyOf(arr, k);}.
2021-02-22 13:37:17
95
原创 剑指Offer-39数组中出现次数超过一半的数字
public static int majorityElement(int[] nums) { // HashMap nums作为key 出现次数作为value HashMap<Integer, Integer> hashMap = new HashMap<>(); // res保存key int res = 0; // max保存出现最多次数的value int max = 0; for (int i = 0; i <.
2021-02-22 11:18:11
83
原创 剑指Offer-38字符串的排列
List<String> res = new LinkedList<>();char[] c;public String[] permutation(String s) { c = s.toCharArray(); dfs(0); return res.toArray(new String[res.size()]);}public void dfs(int x){ if(x == c.length - 1) { res.a.
2021-02-22 10:09:34
108
原创 剑指Offer-37序列化二叉树
public static String serialize(TreeNode root) { if (root == null){ return "[]"; } // 不涉及线程安全 StringBuilder更快 StringBuilder res = new StringBuilder("["); Queue<TreeNode> queue = new LinkedList<>(); queue.add(root.
2021-02-20 13:21:38
100
2
原创 剑指Offer-36二叉搜索树与双向链表
Node pre, head;public Node treeToDoublyList(Node root) { if (root == null){ return null; } dfs(root); // 处理头节点和尾节点指针 head.left = pre; pre.right = head; return head;}public void dfs(Node cur){ // 中序遍历 if (cur.
2021-02-20 10:19:50
84
原创 剑指Offer-35复杂链表的复制
public Node copyRandomList(Node head) { if(head == null) { return null; } Node cur = head; Map<Node, Node> map = new HashMap<>(); // 复制各节点 while(cur != null) { map.put(cur, new Node(cur.val)); cu.
2021-02-19 11:14:52
67
原创 剑指Offer-34二叉树中和为某一值的路径
// 记录结果LinkedList<List<Integer>> res = new LinkedList<>();// 记录路径LinkedList<Integer> path = new LinkedList<>();public List<List<Integer>> pathSum(TreeNode root, int sum) { recur(root, sum); return res;.
2021-02-19 10:33:30
91
原创 剑指Offer-33二叉搜索树的后序遍历序列
public static boolean verifyPostorder(int[] postorder) { return recur(postorder, 0, postorder.length - 1);}public static boolean recur(int[] postorder, int i, int j) { if(i >= j) { return true; } int p = i; // 搜索二叉树找到当前根节.
2021-02-19 09:48:40
63
原创 剑指Offer-32从上到下打印二叉树 III
public List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); List<List<Integer>> res = new ArrayList<>(); if (root != null){ queue.add(root); } whi.
2021-02-18 12:55:16
72
1
原创 剑指Offer-31从上到下打印二叉树 II
public List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); List<List<Integer>> res = new ArrayList<>(); if (root != null) { queue.add(root); } wh.
2021-02-18 11:54:35
74
原创 剑指Offer-30从上到下打印二叉树
public int[] levelOrder(TreeNode root) { // 广度优先借助队列 Queue<TreeNode> queue = new LinkedList<>(); ArrayList<Integer> ans = new ArrayList<>(); while (root != null){ ans.add(root.val); // 判断是否有左右子树,有则进队.
2021-02-18 11:06:24
56
原创 剑指Offer-29栈的压入、弹出序列
public static boolean validateStackSequences(int[] pushed, int[] popped) { // 双指针模拟 int p1 = 0; int p2 = 0; Stack<Integer> stack = new Stack<>(); while (p2 != popped.length){ // 符合栈顶跟p2指针相等就出栈并且指针后移 if (!sta.
2021-02-18 10:49:53
80
原创 剑指Offer-28包含min函数的栈
Stack<Integer> A;Stack<Integer> B;public MinStack28() { // 用两个栈实现 A为所有数据 B为递减 A = new Stack<>(); B = new Stack<>();}public void push(int x) { A.push(x); // 插入操作时 判断B的栈顶比当前插入数据大才插入 这样就能时刻取出最小值了 if (B.em.
2021-02-18 10:06:20
76
原创 剑指Offer-27顺时针打印矩阵
public static int[] spiralOrder(int[][] matrix) { // 如果二维数组为空 直接返回一个空数组 if(matrix == null || matrix.length == 0 || matrix[0].length == 0) { return new int[0]; } // 四边界分别代表左右上下、x一维数组索引 int l = 0, r = matrix[0].length - 1, t = 0,.
2021-02-10 11:02:33
76
原创 剑指Offer-26对称的二叉树
public static boolean isSymmetric(TreeNode root) { if (root == null){ return true; } // 子问题判断每对节点是否对称 return symmetric(root.left, root.right);}public static boolean symmetric(TreeNode left, TreeNode right){ if (left == null .
2021-02-09 17:40:31
78
原创 剑指Offer-25二叉树的镜像
public static TreeNode mirrorTree(TreeNode root) { if (root == null){ return null; } // 就像数组中两个数交换位置 用t变量暂时保存一个值 然后交换位置 TreeNode t = root.left; root.left = root.right; root.right = t; // 递归实现子问题 左右子树也看做根节点 mirrorTre.
2021-02-09 11:49:19
90
原创 剑指Offer-24树的子结构
/** * 对A先序遍历 B不动 * @author Lucas * @date 2021/02/08 23:31 * @param A 给定树 * @param B 子结构 * @return boolean */public static boolean isSubStructure(TreeNode A, TreeNode B) { // 递归出口 if (A == null || B == null) { return false; } .
2021-02-09 10:54:03
68
原创 剑指Offer-23合并两个排序的链表
public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { // 伪节点 ListNode node = new ListNode(-1); ListNode head = node; ListNode p; // 尾插法实现 while (l1 != null && l2 != null){ if (l1.val < l2.val){ .
2021-02-06 17:13:53
66
原创 剑指Offer-22反转链表
public static ListNode reverseList(ListNode head) { // 思路是遍历当前链表 实现头插法创建新链表 // 伪节点 ListNode node = new ListNode(-1); ListNode p; // 头插法 while (head != null){ p = new ListNode(head.val); p.next = node.next; n.
2021-02-06 14:19:45
1779
原创 剑指Offer-21链表中倒数第k个节点
public static ListNode getKthFromEnd(ListNode head, int k) { // 思路双指针 ListNode p = head; // 让p先走k步 while (k > 0){ p = p.next; k--; } // 然后p和head一起走 head永远落后p有k步 直到p走完 head就定位到倒数第k了 while (p != null){ .
2021-02-06 14:01:23
85
原创 剑指Offer-20调整数组顺序使奇数位于偶数前面
public static int[] exchange(int[] nums) { // 双指针 int p1 = 0; int p2 = nums.length - 1; // 结束条件 两个指针碰头了 while (p1 <= p2){ // 前面遇到偶数了 if (nums[p1] % 2 == 0){ // 而且后面遇到奇数了 if (nums[p2] % 2 != 0).
2021-02-05 16:44:00
63
原创 剑指Offer-19表示数值的字符串
public static boolean isNumber(String s) { s = s.trim(); try { Double.parseDouble(s); }catch (NumberFormatException e){ return false; } // 排除特殊情况 char last = s.charAt(s.length()-1); return (last >= '0' &&.
2021-02-04 18:20:18
103
原创 剑指Offer-18正则表达式匹配
public static boolean isMatch(String s, String p) { //如果模式串为空,则只能匹配空的匹配串。 if (p.isEmpty()){ return s.isEmpty(); } //记录匹配串的第一个字符是否被模式串匹配。 boolean firstMatch = (!s.isEmpty() && (p.charAt(0) == s.charAt(0) || p.charAt(0) =.
2021-02-04 13:51:30
108
原创 剑指Offer-17删除链表的节点
public static ListNode deleteNode(ListNode head, int val) { //保留头节点 ListNode p = head; // 如果第一个就匹配 直接返回head的下一个节点 if (p.val == val){ return head.next; } // 循环判断 while (p.next != null){ if (p.next.val == val){ .
2021-02-04 11:16:32
77
原创 剑指Offer-16打印从1到最大的n位数
public static int[] printNumbers(int n) { // 规律 计算出数组的长度 int count = (int) Math.pow(10,n) - 1; int[] res = new int[count]; // 只能是循环插入数据了 for (int i = 0; i < count; i++){ res[i] = i + 1; } return res;}...
2021-02-04 10:44:33
85
原创 剑指Offer-15数值的整数次方
public static double myPow(double x, int n) { if (x == 0){ return 0; } // 当n = -2147483648时执行 // n = -n会因越界而赋值出错 // 解决方法是先将n存入long变量l 后面用l操作即可 long l = n; double res = 1; // 指数是负数 则把x置为倒数 指数变为正数用于循环 if(l < 0).
2021-02-03 16:10:54
83
原创 剑指Offer-14二进制中1的个数
ublic static int hammingWeight(int n) { int count = 0; while (n != 0){ // 与运算 // 若 n & 1 = 0,则 n 二进制 最右一位 为 0 // 若 n & 1 = 1,则 n 二进制 最右一位 为 1 count += n & 1; // 无符号右移 用 0 补充前面 n >>&.
2021-02-03 13:49:36
72
原创 剑指Offer-13剪绳子
import java.math.BigInteger;/** * @author Lucas * @version 1.0.0 * @ClassName CuttingRope13.java * @Description 剪绳子 * @createTime 2021年02月02日 21:56:00 */public class CuttingRope13 { /** * @author: Lucas * @createTime: 2021/2/2 21:.
2021-02-03 00:17:35
91
原创 剑指Offer-12剪绳子
public static int cuttingRope(int n) { int max = 0; for (int i = 2; i<= n; i++){ max = Math.max(cutting(n, i), max); } return max;}public static int cutting(int n, int m){ // 每段的长度 只能是整数 int length = n / m; // 剩余的.
2021-02-02 21:52:53
79
原创 剑指Offer-11机器人的运动范围
public static int movingCount(int m, int n, int k) { boolean[][] visited = new boolean[m][n]; // 机器人从[0,0]坐标开始移动 return search(m, n, k, visited, 0, 0);}public static int search(int m, int n, int k, boolean[][] visited, int x, int y){ //.
2021-02-02 14:16:10
88
原创 剑指Offer-10矩阵中的路径
/** * @author: Lucas * @createTime: 2021/2/1 22:31 * @Description 典型的回溯 * @param: [board, word] * @retrun: boolean */public static boolean exist(char[][] board, String word) { int h = board.length; int w = board[0].length; //经过矩阵的某个格子不.
2021-02-01 22:52:13
75
原创 剑指Offer-09旋转数组的最小数字
/** * 啊这 * 我干了啥 * 按题目意思就是输出数组的最小值啊 */public static int minArray(int[] numbers) { //排序就完事了 Arrays.sort(numbers); return numbers[0];}性能不行 sort的底层用的是快排 题目还有条件可以利用上 让我来优化public static int minArray(int[] numbers){ //因为循环里有i+1 所以注意.
2021-01-31 11:57:17
84
原创 剑指Offer-08青蛙跳台阶问题
/**其实就是斐波拉契数列的思路 * 走一台阶一种方式 * 走两台阶两种方式 * 走三台阶即可拆分为走一台阶+走两台阶*/public static int numWays(int n) { int a = 1; int b = 1; int sum = a + b; for(int i = 0; i < n; i++){ a = b; b = sum; sum = (a + b) % 1000000007; .
2021-01-30 15:37:10
106
原创 剑指Offer-07斐波那契数列
public static int fib(int n) { if (n < 2){ return n; } int a = 0; int b = 1; int sum = a + b; //a b sum 每次循环用前两个变量保留b和sum for(int i = 2; i < n ; i++){ a = b; b = sum; sum = (a + b) % 100000.
2021-01-30 14:35:53
96
原创 剑指Offer-06用两个栈实现队列
Stack stack1;Stack stack2;public CQueue06() { //题目要求两个栈实现队列 stack1 = new Stack(); stack2 = new Stack();}public void appendTail(int value) { //stack1用来存所有的值 stack1.push(value);}public int deleteHead() { if(stack1.empty()){.
2021-01-30 12:38:43
79
原创 剑指Offer-05重建二叉树
public static TreeNode buildTree(int[] preorder, int[] inorder) { if (preorder == null || preorder.length == 0 || inorder == null || inorder.length == 0 ){ return null; } //前序遍历第一个元素必定是根结点 TreeNode root = new TreeNode(preorder[0]);.
2021-01-30 00:02:50
88
原创 剑指Offer-04从尾到头打印链表
public static int[] reversePrint(ListNode head){ int len = 0; //用hold保留头结点 ListNode hold = head; //计算链表长度 while (head != null){ len++; head = head.next; } int[] res = new int[len]; //数组倒序存储链表正序 for (int .
2021-01-27 09:58:37
71
原创 剑指Offer-03替换空格
public static String replaceSpace(String s) { //单线程就不用考虑线程安全 当然选择性能更高的StringBuilder StringBuilder res = new StringBuilder(); StringBuilder stringBuffer = new StringBuilder(s); for (int i = 0; i < stringBuffer.length(); i++){ //截.
2021-01-26 23:33:55
72
原创 剑指Offer-01数组中重复的数字
public static int findRepeatNumber(int[] nums) { //由于HashMap的key不重复 HashMap<Integer,Integer> map = new HashMap<>(); for (int temp : nums) { if (map.containsKey(temp)) {//判断map是否包含这个值 直接返回 return temp; }.
2021-01-26 22:00:59
83
考勤管理系统 数据结构 C语言
2020-07-15
android即时通讯IM基于环信SDK.zip
2020-07-15
雷电游戏java.zip
2020-07-15
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅