Algorithm
文章平均质量分 54
ImproveJin
大龄失业程序员
展开
-
Redis笔记
Redids笔记原创 2022-11-19 16:54:27 · 529 阅读 · 0 评论 -
动态规划之最大正方形
最大的以 1 为边界的正方形给你一个由若干 0 和 1 组成的二维网格 grid,请你找出边界全部由 1 组成的最大 正方形 子网格,并返回该子网格中的元素数量。如果不存在,则返回 0。[leetcode] public int largest1BorderedSquare(int[][] grid) { int n = grid.length; int[] dp = new int[n]; for(int i=0;i<n;++i) {原创 2020-07-09 18:05:36 · 285 阅读 · 0 评论 -
动态规划之最低票价
在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行。在接下来的一年里,你要旅行的日子将以一个名为 days 的数组给出。每一项是一个从 1 到 365 的整数。火车票有三种不同的销售方式:一张为期一天的通行证售价为 costs[0] 美元;一张为期七天的通行证售价为 costs[1] 美元;一张为期三十天的通行证售价为 costs[2] 美元。通行证允许数天无限制的旅行。 例如,如果我们在第 2 天获得一张为期 7 天的通行证,那么我们可以连着旅行 7 天:第 2 天、第 3 天、第 4原创 2020-07-09 17:59:54 · 282 阅读 · 0 评论 -
快排应用-第k大元素
在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。leetcode public int findKthLargest(int[] nums, int k) { return findKthLargest(nums, 0, nums.length,k); } private int findKthLargest(int[] nums, int start, int end, int k) {原创 2020-06-18 17:32:34 · 217 阅读 · 0 评论 -
链表插入排序
时间复杂度O(n2)leetcode public ListNode insertionSortList(ListNode head) { ListNode res = null; while (head != null) { if (res == null) { res = head; head = head.next; res.next = n原创 2020-06-17 14:27:26 · 119 阅读 · 0 评论 -
二叉树遍历
深度优先前序遍历 Leetcode public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); if (root != null) { res.add(root.val); res.addAll(preorderTraversal(root.left));原创 2020-06-17 14:20:05 · 113 阅读 · 0 评论 -
递归应用-链表快速排序
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。leetcode public ListNode quickSortList(ListNode head) { if (head == null) return null; ListNode pivot = head; ListNode left = null, right = null; ListNode cur = head.next; wh原创 2020-06-17 14:08:58 · 168 阅读 · 0 评论 -
递归应用-根据遍历顺序构造二叉树
已知前序遍历和中序遍历,可以构造唯一二叉树同样,已知后序和中序遍历,可以构造唯一二叉树但是已知前序和后序,并不能构造唯一二叉树,因为对于单个子节点无法确定到底是左子树还是右子树已知前序中序 Leetcode public TreeNode buildTree(int[] preorder, int[] inorder) { return buildTree(preorder, 0, preorder.length, inorder, 0, inorder.length);原创 2020-06-16 16:19:58 · 230 阅读 · 0 评论 -
动态规划-不同路径
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。问总共有多少条不同的路径?leetcode public int uniquePaths(int m, int n) { int[][] a = new int[m][n]; for(int i=0; i<m; ++i) for(int j=0; j原创 2020-06-12 18:38:49 · 170 阅读 · 0 评论 -
递归&动态规划-解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码:‘A’ -> 1‘B’ -> 2…‘Z’ -> 26给定一个只包含数字的非空字符串,请计算解码方法的总数。leetcode递归 public int numDecodings(String s) { int total = 0; if (s.isEmpty()) { return total; } String原创 2020-06-12 18:23:44 · 186 阅读 · 0 评论 -
递归应用-回溯复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。有效的 IP 地址正好由四个整数(每个整数位于 0 到 255 之间组成),整数之间用 ‘.’ 分隔。leetcode public List<String> restoreIpAddresses(String s) { return restoreIpAddresses(s, 4); } private List<String> restoreIpAddresses(Str原创 2020-06-12 18:17:36 · 246 阅读 · 0 评论 -
递归应用-n个数中选取k个数
给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。leetcode public List<List<Integer>> combine(int n, int k) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if (n==k) { List<Integer>原创 2020-06-11 11:00:27 · 861 阅读 · 0 评论 -
递归应用-生成子集
给定一个数组,生成子集leetcode public List<List<Integer>> subsets(int[] nums) { return subsets(nums, nums.length); } private List<List<Integer>> subsets(int[] nums, int len) { List<List<Integer>> res = n原创 2020-06-10 18:45:09 · 206 阅读 · 0 评论 -
相对路径转换成绝对路径
根据Linux相对路径生成绝对路径leetcode public String simplifyPath(String path) { String[] a = path.split("/"); int[] flag = new int[a.length]; for(int i=0;i<a.length;i++) { String s = a[i]; if (s.isEmpty() || s.eq原创 2020-06-10 18:40:09 · 784 阅读 · 0 评论 -
递归应用-回溯生成括号对
递归可以理解为深度优先遍历穷举+回溯剪枝leetcodeclass Solution { public List<String> generateParenthesis(int n) { List<String> result = new ArrayList<>(); generateParenthesis(n, ""...原创 2020-01-08 14:48:04 · 152 阅读 · 0 评论 -
递归应用-组合总和
给定一个正数数组和target, 求所有组合为target的解。leetcode直观解决方案是蛮力搜索,保存搜索路径,对于无法确定搜索次数的,只能使用递归搜索,准确描述搜索停止条件即可。class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target)...原创 2020-01-07 17:38:55 · 154 阅读 · 0 评论 -
递归应用-全排列
leetcode题class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<>(); if (nums.length == 0) retu...原创 2020-01-06 17:43:29 · 175 阅读 · 0 评论 -
大数乘法
输入两个代表整数的字符串,不使用标准库,实现大数乘法LeetCode//手动乘法public String multiply(String num1, String num2) { if(num1.equals("0") || num2.equals("0")) return "0"; String result = ""; for(int i=num1.length...原创 2020-01-06 17:12:21 · 167 阅读 · 0 评论 -
三数之和为0的解题过程
leetcode上有个三数之和的题目:给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:[ ...原创 2020-01-02 18:33:50 · 741 阅读 · 0 评论 -
那些年我们学过的算法
同我们学过的数据结构一样,大部分时候也不需我们发明算法,主要理解其原理和时间复杂度以及使用场景为主,大部分优化算法是以空间换时间。查找二分查找,查找数据需有序并且能够根据位置索引直接定位,比如ArrayList, java.util.Arrays.binarySearchKMP,用于字符串高效查找排序选择排序冒泡排序希尔排序快排,JDK中使用了优化的快排,java.util.Array原创 2017-11-07 10:13:24 · 389 阅读 · 0 评论 -
好的算法blog收藏
算法学习blog: http://blog.csdn.net/v_JULY_v快速排序: 刘伟鹏版 http://blog.csdn.net/pongba/archive/2008/06/13/2544933.aspxJuly版 http://blog.csdn.net/v_JULY_v/archive/2011/02/27/6211155.aspx线性时间复杂度排序:http://blog.csdn.net/zheng80037/archive/2007/06/02/16351原创 2011-03-30 23:00:00 · 972 阅读 · 0 评论