算法
每天进步一点点
Mr.Gonlando
这个作者很懒,什么都没留下…
展开
-
【经典】盛最多水的容器
一、题目力扣原题:https://leetcode-cn.com/problems/container-with-most-water/submissions/二、暴力class Solution { public int maxArea(int[] height) { if (null == height || 0 == height.length) { return 0; } int max = 0;原创 2020-05-23 15:25:47 · 291 阅读 · 0 评论 -
【链表】链表排序
一、题目力扣原题:https://leetcode-cn.com/problems/sort-list/二、归并排序/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode so原创 2020-05-23 13:11:17 · 373 阅读 · 0 评论 -
【经典】实现一个阻塞队列
import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class BlockingQueue<T> { /** * 对象池 */ private Object[] objs; /** * 容量 */.原创 2020-05-20 23:49:57 · 270 阅读 · 0 评论 -
【多线程】连续打印ABC
一、内置锁同步:synchronized 协作:Object # wait/notify/notifyAllpublic class PrintABC { /** * 打印锁,同一时刻仅有一个任务可以持有此锁 */ private static Object lock = new Object(); private static int state = 1; public static void main(String args[]) {原创 2020-05-20 23:08:55 · 216 阅读 · 0 评论 -
【排序】归并排序
import java.util.Arrays;import java.util.Random;public class MergeSort { public static void main(String args[]) { int[] array = fillArray(); split(array, 0, array.length - 1); System.out.println(Arrays.toString(array)); .原创 2020-05-20 21:44:08 · 115 阅读 · 0 评论 -
【多线程】双线程交替打印1至100
一、内置锁同步:synchronized 协作:Object # wait/notify/notifyAllpublic class PrintNumber { /** * 打印锁,同一时刻仅有一个任务可以持有此锁 */ private static Object lock = new Object(); /** * 计数器 */ private static int counter = 1; /**原创 2020-05-17 22:40:58 · 2418 阅读 · 0 评论 -
【排序】快速排序
public class QuickSort { private static int[] fillArray() { Random random = new Random(); int[] array = new int[100]; for (int i = 0; i < array.length; i++) { array[i] = random.nextInt(10000); } .原创 2020-05-17 19:40:32 · 177 阅读 · 0 评论 -
【动态规划】最长上升子序列
一、题目力扣原题:https://leetcode-cn.com/problems/longest-increasing-subsequence/二、动态规划class Solution { public int lengthOfLIS(int[] nums) { if (null == nums || 0 == nums.length) { return 0; } int[] dp = new int[nums原创 2020-05-17 19:24:54 · 134 阅读 · 0 评论 -
【链表】奇偶链表
一、题目力扣原题:https://leetcode-cn.com/problems/odd-even-linked-list/二、额外开辟空间/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { publi原创 2020-05-17 18:36:27 · 196 阅读 · 0 评论 -
【二叉树】中序遍历
一、题目力扣原题:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/前序遍历:https://blog.csdn.net/sinat_34596644/article/details/106130854后序遍历:https://blog.csdn.net/sinat_34596644/article/details/106131335二、递归/** * Definition for a binary tree原创 2020-05-15 00:50:44 · 164 阅读 · 0 评论 -
【二叉树】后序遍历
一、题目力扣原题:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/前序遍历:https://blog.csdn.net/sinat_34596644/article/details/106130854二、递归/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode l原创 2020-05-15 00:06:23 · 233 阅读 · 0 评论 -
【二叉树】前序遍历(先序遍历)
一、题目力扣原题:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/二、递归/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }原创 2020-05-15 00:06:32 · 273 阅读 · 0 评论 -
【数组】搜索旋转排序数组(含重复元素)
一、题目力扣原题:https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/搜索旋转排序数组(不含重复元素):https://blog.csdn.net/sinat_34596644/article/details/106118330二、二分查找class Solution { public boolean search(int[] nums, int target) { int left原创 2020-05-14 20:09:00 · 205 阅读 · 0 评论 -
【数组】搜索旋转排序数组
一、题目力扣原题:https://leetcode-cn.com/problems/search-in-rotated-sorted-array/二、暴力时间复杂度:O(n) 空间复杂度:O(1)三、二分查找class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length - 1; while (le原创 2020-05-14 13:57:24 · 107 阅读 · 0 评论 -
【矩阵】搜索二维矩阵
一、题目力扣原题:https://leetcode-cn.com/problems/search-a-2d-matrix-ii/二、暴力class Solution { public boolean searchMatrix(int[][] matrix, int target) { if (null == matrix || 0 == matrix.length || 0 == matrix[0].length) { return false;原创 2020-05-14 12:17:26 · 292 阅读 · 0 评论 -
【二叉树】二叉树的右视图
一、题目力扣原题:https://leetcode-cn.com/problems/binary-tree-right-side-view/二、BFS搜索/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }原创 2020-05-14 10:57:54 · 1156 阅读 · 0 评论 -
【字符串】翻转字符串里的单词
一、题目力扣原题:https://leetcode-cn.com/problems/reverse-words-in-a-string/二、APIclass Solution { public String reverseWords(String s) { if (null == s) { return s; } s = s.trim(); if (0 == s.length()) {原创 2020-05-14 09:48:58 · 116 阅读 · 0 评论 -
【经典】接雨水
一、题目力扣原题:https://leetcode-cn.com/problems/trapping-rain-water/submissions/二、暴力class Solution { public int trap(int[] height) { if (null == height || 0 == height.length) { return 0; } int result = 0; f原创 2020-05-14 09:08:21 · 174 阅读 · 0 评论 -
【二叉树】蛇形遍历
一、题目力扣原题:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/二、BFS搜索/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val原创 2020-05-14 09:10:28 · 2086 阅读 · 0 评论 -
【链表】链表求和
一、题目力扣原题:https://leetcode-cn.com/problems/sum-lists-lcci/submissions/二、双指针模拟/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution {原创 2020-05-14 07:04:36 · 400 阅读 · 0 评论 -
【二叉树】路径总和(含路径)
一、题目力扣原题:https://leetcode-cn.com/problems/path-sum-ii/【二叉树】路径总和:https://blog.csdn.net/sinat_34596644/article/details/106109952二、DFS+回溯/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; *原创 2020-05-14 06:43:06 · 500 阅读 · 0 评论 -
【二叉树】路径总和
一、题目力扣原题:https://leetcode-cn.com/problems/path-sum/二、递归/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution {原创 2020-05-14 00:03:50 · 275 阅读 · 0 评论 -
【模拟】螺旋矩阵
一、题目力扣原题:https://leetcode-cn.com/problems/spiral-matrix-ii/二、模拟法class Solution { public int[][] generateMatrix(int n) { // 初始化二维矩阵 int[][] result = new int[n][n]; for (int i = 0; i < n; i++) { for (int j =原创 2020-05-13 23:31:58 · 122 阅读 · 0 评论 -
【经典】跳跃游戏
一、题目力扣原题:https://leetcode-cn.com/problems/jump-game/submissions/二、深度优先搜索classSolution{privateint[]nums;privateboolean[]visited;publicbooleancanJump(int[]nums){if(null==nums||0==nums.length){r...原创 2020-05-12 22:54:55 · 224 阅读 · 0 评论 -
【经典】买卖股票的最佳时机(多次交易)
一、题目力扣原题:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/二、峰谷法class Solution { public int maxProfit(int[] prices) { int result = 0; int i = 0; while (i < prices.length - 1) { // 找到下一个谷原创 2020-05-12 20:14:54 · 303 阅读 · 0 评论 -
【经典】买卖股票的最佳时机(单次交易)
一、题目力扣原题:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/二、暴力时间复杂度:O(n^2) 空间复杂度:O(1)三、一次遍历class Solution { public int maxProfit(int[] prices) { if (null == prices || 0 == prices.length) { return 0;原创 2020-05-12 13:20:38 · 131 阅读 · 0 评论 -
【数组】寻找旋转排序数组中的最小值
一、题目力扣原题:https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/二、排序class Solution { public int findMin(int[] nums) { if (null == nums || 0 == nums.length) { return -1; } Arrays.sort(nums);原创 2020-05-11 22:04:15 · 160 阅读 · 0 评论 -
【二叉树】二叉树的重建
一、题目力扣原题《从前序与中序遍历序列构造二叉树》:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/力扣原题《从中序与后序遍历序列构造二叉树》:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/二、递归/** * Defin原创 2020-05-11 20:42:18 · 173 阅读 · 0 评论 -
【二叉树】二叉搜索树的最近公共祖先
一、题目力扣原题:https://leetcode-cn.com/problemset/all/?listId=ex0k24j类似题目:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/二叉搜索树是特殊的二叉树,因此二叉树查找“最近公共祖先”的方法同样适用于二叉搜索树,https://blog.csdn.net/sinat_34596644/article/details/106029076。本文只针原创 2020-05-10 22:38:43 · 297 阅读 · 0 评论 -
【链表】反转链表
一、题目力扣原题:https://leetcode-cn.com/problems/reverse-linked-list/二、辅助空间/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public L原创 2020-05-10 18:23:42 · 79 阅读 · 0 评论 -
【链表】旋转链表
一、题目力扣原题:https://leetcode-cn.com/problems/rotate-list/二、模拟法/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(intx){val=x;}*}*/classSolution{publicListNoder...原创 2020-05-10 13:55:42 · 226 阅读 · 0 评论 -
【链表】排序链表
一、题目乐扣原题:https://leetcode-cn.com/problems/sort-list/submissions/二、转化为数组/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(intx){val=x;}*}*/classSolution{publi...原创 2020-05-10 11:10:11 · 178 阅读 · 0 评论 -
【数组】最接近的三数之和
一、题目力扣原题:https://leetcode-cn.com/problems/3sum-closest/二、暴力classSolution{publicintthreeSumClosest(int[]nums,inttarget){intresult=-1;intdelta=Integer.MAX_VALUE;for(inti=0;i<nums.length;i++){...原创 2020-05-10 09:49:19 · 225 阅读 · 0 评论 -
【二叉树】二叉树的最近公共祖先
一、题目力扣原题:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/二、递归/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x;原创 2020-05-09 23:41:45 · 394 阅读 · 0 评论 -
【二叉树】验证二叉搜索树
一、题目二叉树定义:节点的左子树只包含小于当前节点的数。 节点的右子树只包含大于当前节点的数。 所有左子树和右子树自身必须也是二叉搜索树。力扣原题:https://leetcode-cn.com/problems/validate-binary-search-tree/二、递归/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left原创 2020-05-08 23:32:05 · 133 阅读 · 0 评论