复习
雄狮少年
落霞与孤鹜齐飞,秋水共长天一色。渔舟唱晚,响穷彭蠡之滨,雁阵惊寒,声断衡阳之浦
展开
-
Java基础 : 锁
一些实例一些实例原创 2022-06-25 22:04:50 · 150 阅读 · 0 评论 -
179.最大数 leetcode31
public class P179LargestNumber{ public static void main(String[] args) { Solution solution = new P179LargestNumber().new Solution(); // TO TEST } //leetcode submit region begin(Prohibit modification and deletion)class Solution原创 2022-02-17 13:56:39 · 301 阅读 · 0 评论 -
215 力扣数组中对的第k大 手写小根堆 class myheap(int capacity){ int size = 0;int[] heapArray= new int[capacity+1] }
文章目录codecode//给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。 //// 请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。 //// //// 示例 1: //// //输入: [3,2,1,5,6,4] 和 k = 2//输出: 5// //// 示例 2: //// //输入: [3,2,3,1,2,4,5,5,6] 和 k = 4//输出: 4 //// //// 提示: ////原创 2022-04-09 00:32:33 · 335 阅读 · 0 评论 -
字符串相加(数字太大,题目都提示了最起码是BigInt级别的数)(只能用StringBuffer类来按位存储数字,从最右边依次想最左边处理,因为最右边是最低位,就是处理本位上的数result%10)
文章目录思路code思路就是处理本位上的数result%10 和 进位 add = result/10codeclass Solution { public String addStrings(String num1, String num2) { StringBuffer ans = new StringBuffer(); int i= num1.length()-1,j = num2.length()-1; // 初始化的时候是第一个下标,即最后边的一原创 2022-03-21 10:13:37 · 105 阅读 · 0 评论 -
比较版本号(i,j 两个指针,遍历两个版本号字符串,解析出dot前面的一对版本号,转化成数字,然后比较,相等则还在while循环里继续比较)
文章目录题目代码(i和j 法)题目代码(i和j 法)class Solution { public int compareVersion(String v1, String v2) { int i = 0, j = 0; int n = v1.length(), m = v2.length(); while(i < n || j < m) { int num1 = 0, num2 = 0;原创 2022-03-19 11:20:55 · 143 阅读 · 0 评论 -
(递归法直接全局res,然后复用主函数)(迭代法“while(非stack.isEmpty())”,“利用栈的后入先出的特性,可以最后再将左子树压入栈,先将其他子树压入)二叉树前序遍历(后序中序同理)
文章目录题目递归法(先写好三条主逻辑,在判断root是否为空,返回res)while循环结合Stack s =newStack<>() , “利用栈的后入先出的特性,可以最后再将左子树压入栈,先将其他子树压入栈”题目递归法(先写好三条主逻辑,在判断root是否为空,返回res)class Solution {List res= new LinkedList<>();public List<Integer> preorderTraversal(TreeNode原创 2022-03-19 11:08:44 · 172 阅读 · 0 评论 -
(用栈实现队列) (妙啊。只有当pop的时候outStack为空,才从inStack中倒元素进来,否则直接就从outStack中pop即可)
题目代码class MyQueue { Deque<Integer> inStack; Deque<Integer> outStack; public MyQueue() { inStack = new LinkedList<Integer>(); outStack = new LinkedList<Integer>(); } public void push(int x原创 2022-03-16 16:01:00 · 399 阅读 · 0 评论 -
两数之和(用map做)
文章目录questioncodequestioncodeclass Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> map = new HashMap<>(); for(int i =0 ;i< nums.length; i++){ map.put(nums[i],i); }原创 2022-03-16 15:27:13 · 303 阅读 · 0 评论 -
(最长重复子数组)(两个数组)(动态规划)(dp[i][j] =dp[i-1][j-1])
文章目录questioncodequestioncodeclass Solution { public int findLength(int[] A, int[] B) { if (A.length == 0 || B.length == 0) { return 0; } int[][] dp = new int[A.length+1][B.length+1]; int result = 0;原创 2022-03-16 14:54:20 · 772 阅读 · 0 评论 -
二叉树中的最大路径和 (有点生疏了)
文章目录timudaimatimudaimaclass Solution { int maxSum = Integer.MIN_VALUE; // 初始化也有坑。 本题最大路径和有可能是负数,所以maxsum初始不能为0 public int maxPathSum(TreeNode root) { if(root == null){ return 0; } dfs(root);原创 2022-03-16 14:29:59 · 890 阅读 · 0 评论 -
LRU缓存机制(利用ArrayDeque和map不会超时)(只能自己实现一个能够双向链表)
文章目录题目我的代码(利用ArrayDeque实现的queue,不会超时)更快的代码(自己定义一个双端结点Node,形成一个LRU式的链表来实现头插尾删(有两个假头head和假尾结点tail),并且可以删除给定key的结点,用map辅助get查找)题目我的代码(利用ArrayDeque实现的queue,不会超时)class LRUCache { int capacity; Map<Integer, Integer> map; // 队头的元素是上次访问距今最久的,原创 2022-03-16 14:19:43 · 1060 阅读 · 0 评论 -
(重排链表)(利用栈实现链表的从后往前遍历)(同时注意要切断尾部的next域,避免形成环形链表)
文章目录题目思路代码及注释题目思路/*思路:如果这是数组的话,相信大多数人都会做,但是本题是链表,,没办法从尾节点向前遍历,所以利用一个栈的pop到pos里面,实现从后往前遍历,并且用一个从前往后的指针pre和 pos相互呼应,只要pre 与 pos不重合 并且 pre.next != pos ,就把pos插在pre的前面*/代码及注释class Solution { /** 思路:* 如果这是数组的话,相信大多数人都会做,* 但是本题是链表,,没办法从尾节原创 2022-03-16 09:32:54 · 909 阅读 · 0 评论 -
合并两个有序链表
文章目录题目思路代码题目思路代码class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { // 递归解法 if (l1 == null) return l2; // 递归出口 if (l2 == null) return l1; // 递归出口 ListNode res = l1.val < l2.val ? l1 : l原创 2022-03-15 10:27:04 · 71 阅读 · 0 评论 -
(合并两个有序数组)(从后往前赋值,不断取两个数组的最大值,因为后面开始就留好空位了,所以不需要额外的空间)(而且nums1 ,nums2中数组本来就是有序的,如果nums2没转移完就接着转移)
文章目录题目绝妙代码题目绝妙代码 int i = m - 1; int j = n - 1; int k = nums1.length - 1; // 倒序遍历两个数组,将大的数从后往前插入nums1,并将指针向前移动一位,直至其中一个数组遍历完,将另一个未遍历完的数组全部插入到nums1 while (i >= 0 && j >= 0){ nums1[k--]=nums1[原创 2022-03-15 10:00:13 · 110 阅读 · 0 评论 -
反转链表II(观看天秀解法:定位后用头插法,思路和我一致,但是操作简单)
文章目录题目天秀解法题目天秀解法class Solution { public ListNode reverseBetween(ListNode head, int m, int n) { // 定义一个dummyHead, 这是为了防止从第一个结点就开始反转,最易要先实际的创建出来一个dummyHead结点 ListNode dummyHead = new ListNode(0); dummyHead.next = head;原创 2022-03-14 13:48:21 · 644 阅读 · 0 评论 -
环形链表 待完成
```cclass Solution { public boolean hasCycle(ListNode head) { ListNode slow = head; ListNode fast = head; while(fast!=null && fast.next!=null){ slow = slow.next; fast = fast.next.next; ...原创 2022-03-12 09:25:56 · 222 阅读 · 0 评论 -
(log(M+N) 二分查找(要利用好原数组有序这个条件)) 寻找两个有序数组的中位数 Leetcode104
文章目录思路题目代码时间复杂度 O((M+N)/2))时间复杂度 O(Log(M+N)思路题目代码时间复杂度 O((M+N)/2))class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int i=0; int j =0; int tol =-1; double midTwoSum = 0; int len ;原创 2022-03-11 14:58:45 · 242 阅读 · 0 评论 -
(Map)(用Map实现的滑动窗口更新滑动窗口左端点的时候用Math.max(left,map.get(charAt(i))避免之前已经存入map的元素的影响)无重复字符的最长子串Leetcode
文章目录思路代码及注释思路滑动窗口:#题目代码及注释 public int lengthOfLongestSubstring(String s) { HashMap<Character, Integer> map = new HashMap<>(); int maxLen = 0;//用于记录最大不重复子串的长度 int left = 0;//滑动窗口左指针 for (int i = 0; i < s.原创 2022-03-11 11:01:09 · 182 阅读 · 0 评论 -
(回文子串 高效率的 中心扩散法)(共有 2 * len - 1 个中心,从下标0 到 下标 len-1)回文子串的个数 Leetcode102
文章目录思路题目代码思路题目代码class Solution { public int countSubstrings(String s) { int result = 0; for(int center = 0; center < s.length()* 2 - 1; center ++){ int left = center/2; int right = left + center%2;原创 2022-03-11 09:54:50 · 1369 阅读 · 0 评论 -
复习(数组 循环 双指针) (利用有序数组 将 三层循环,变成两层循环)(处理重复元素)三数之和 Leetcode90
文章目录题目代码及详细注释我的代码及注释题目(这里面最重要的就是对重复元素的跳过处理)代码及详细注释class Solution { public static List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> ans = new ArrayList(); int len = nums.length; if(nums =原创 2022-03-06 11:27:56 · 347 阅读 · 0 评论 -
(二叉树复习) (DFS)(三道类似的题)二叉树的直径、最长同值路径、二叉树最大路径和 Leetcode90
二叉树的直径 题目描述思路遍历每一个节点,以每一个节点为中心点计算最长路径(左子树边长+右子树边长),更新全局变量max。代码及注释class Solution { int max = 0; public int diameterOfBinaryTree(TreeNode root) { if(root == null){ return 0; } dfs(root); return max原创 2022-03-05 14:31:39 · 291 阅读 · 0 评论