- 博客(45)
- 资源 (6)
- 收藏
- 关注
原创 剑指Offer-53在排序数组中查找数字 I
public int search(int[] nums, int target) { // 找target的右边界 - target-1的右边界 return helper(nums, target) - helper(nums, target - 1);}int helper(int[] nums, int tar) { int i = 0; int j = nums.length - 1; // 二分法查找 while(i <= j) { .
2021-02-26 16:56:26 49
原创 剑指Offer-52两个链表的第一个公共节点
if (headA == null || headB == null) { return null;}// 双指针 消除两个链表的长度差ListNode pA = headA;ListNode pB = headB;// 直到A、B相遇while (pA != pB){ // A先自己遍历完 // 然后A就变成了B 继续遍历 if(pA == null){ pA = headB; }else{ pA = pA.next;.
2021-02-26 15:32:24 72
原创 剑指Offer-51数组中的逆序对
public int reversePairs(int[] nums) { // 用于合并阶段存放数组 int[] tmp = new int[nums.length]; return mergeSort(0, nums.length - 1, tmp, nums);}private int mergeSort(int l, int r, int[] tmp, int[] nums) { // 终止条件 if (l >= r) { ret.
2021-02-26 14:35:59 74
原创 剑指Offer-50第一个只出现一次的字符
public char firstUniqChar(String s) { // 遍历 for (int i = 0; i < s.length(); i++){ char ch = s.charAt(i); // 某个字符出现的位置就是当前i的位置 且 i之前没有出现过这个字符 if (i == s.lastIndexOf(ch) && !s.substring(0, i).contains(String.valueO.
2021-02-25 18:02:27 92
原创 剑指Offer-49丑数
/** * 动态规划 * 有规律后面的丑数都是由前面的丑数*2或*3或*5得来的 * 求得的丑数记录dp数组中 * 三个指针p2,p3,p5 * p2指向的数字下一次永远*2,p3指向的数字下一次永远*3,p5指向的数字永远*5 * 从2*p2 3*p3 5*p5取最小的数,作为第k个丑数 * 返回第n个丑数 */public int nthUglyNumber(int n) { int p2 = 0; int p3 = 0; int p5 = 0; in.
2021-02-25 17:17:30 65
原创 剑指Offer-48最长不含重复字符的子字符串
public int lengthOfLongestSubstring(String s) { Map<Character, Integer> dic = new HashMap<>(); int res = 0; int tmp = 0; for(int j = 0; j < s.length(); j++) { // 获取索引 i int i = dic.getOrDefault(s.charAt(j), -.
2021-02-25 13:32:16 97
原创 剑指Offer-47礼物的最大价值
public int maxValue(int[][] grid) { int m = grid.length; int n = grid[0].length; // 初始化矩阵第一行 for(int j = 1; j < n; j++) { grid[0][j] += grid[0][j - 1]; } // 初始化矩阵第一列 for(int i = 1; i < m; i++) { grid[i][0].
2021-02-25 11:36:04 74
原创 剑指Offer-46把数字翻译成字符串
public int translateNum(int num) { // 动态规划法 String s = String.valueOf(num); int[] dp = new int[s.length()+1]; // 初始化dp[0]代表空数字 dp[1]代表第一个数字 的组合翻译方式有一种 dp[0] = 1; dp[1] = 1; for (int i = 2; i <= s.length(); i++){ Stri.
2021-02-25 10:34:17 69
原创 剑指Offer-45把数组排成最小的数
public String minNumber(int[] nums) { // 想到排序的时候对字符串排序是根据首位ASCII码 符合题目意愿 String[] array = new String[nums.length]; // 转成String数组 for (int i = 0; i < nums.length ; i++){ array[i] = Integer.toString(nums[i]); } // Lambda表达.
2021-02-24 13:15:20 72
原创 剑指Offer-44数字序列中某一位的数字
public int findNthDigit(int n) { // 表示几位数 int digit = 1; // 几位数开始的数字 long start = 1; // 几位数一共有多少个 long count = 9; // 确定所在数字的是多少位的 while (n > count) { n -= count; digit += 1; // 总结出来的规律 sta.
2021-02-24 11:10:13 58
原创 剑指Offer-43 1~n 整数中 1 出现的次数
public int countDigitOne(int n) { int res = 0; // 标记当前位数 个位为1 十位为10 百位为100 。。。。 int digit = 1; // 初始化 // 当前位置前面的数字 int high = n / 10; // 当前位置的数字 int cur = n % 10; // 当前位置后面的数字 int low = 0; while (high != 0 || cu.
2021-02-24 10:34:26 67
原创 剑指Offer-42连续子数组的最大和
public int maxSubArray(int[] nums) { // 动态规划法 // 因为不需要输出具体是哪一段得出的最大和 直接在原数组上操作 int res = nums[0]; for(int i = 1; i < nums.length; i++) { // 以元素 nums[i] 为结尾的连续子数组最大和 // 如果它前面是个负数就不要加进来拖后腿了 加个0 最大和就是它本身 nums[i] += .
2021-02-23 13:01:35 62
原创 剑指Offer-41数据流中的中位数
PriorityQueue<Integer> A, B;public MedianFinder41() { //规定 小顶堆 保存较大的一半 长度为N/2 或 (N+1)/2 A = new PriorityQueue<>(); //规定 大顶堆 保存较小的一半 长度为N/2 或 (N-1)/2 B = new PriorityQueue<>((x, y) -> (y - x));}public void addNum(int.
2021-02-23 11:36:32 69
原创 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 157
原创 剑指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 59
原创 剑指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 52
原创 剑指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 63
原创 剑指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 57 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 56
原创 剑指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 48
原创 剑指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 64
原创 剑指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 44
原创 剑指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 48 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 50
原创 剑指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 41
原创 剑指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 55
原创 剑指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 54
原创 剑指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 56
原创 剑指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 51
原创 剑指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 66
原创 剑指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 49
原创 剑指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 46
原创 剑指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 1670
原创 剑指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 61
原创 剑指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 43
原创 剑指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 69
原创 剑指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 75
原创 剑指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 54
原创 剑指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 60
原创 剑指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 55
android即时通讯IM基于环信SDK.zip
2020-07-15
考勤管理系统 数据结构 C语言
2020-07-15
雷电游戏java.zip
2020-07-15
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人