![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 79
业精勤而荒嬉
知其然知其所以然
展开
-
数组中个数大于N/k的数
题一:数组arr,打印出数组中个数大于一半的数 public int solve(int[] arr) { int num = 0, time = 0; for (int i = 0; i < arr.length; i++) { if (time == 0) { num = arr[i];...原创 2018-06-12 18:29:00 · 258 阅读 · 0 评论 -
二分法相关题目
一般用法:public int binarySearch(int[] nums, int key) { int l = 0, h = nums.length - 1; while (l <= h) { int m = l + (h - l) / 2; // 防止l+h越界 if (nums[m] == key) { r...原创 2019-04-05 07:58:35 · 149 阅读 · 0 评论 -
LeetCode动态规划题(二)
股票交易309,Best Time to Buy and Sell Stock with Cooldown包含冷却期 public int maxProfit(int[] prices) { if(prices.length&lt;2) return 0; int[] buy = new int[prices.length]; int[]...原创 2019-03-16 02:10:06 · 179 阅读 · 0 评论 -
由入栈/出栈序列求所有出栈/入栈序列
给出一个数组,代表入栈顺序,求所有出栈可能性?给出一个数组,代表出栈顺序,求所有入栈可能性?这两题解法相同,可利用 全排列 求出所有组合,再进行可能性分析。全排列代码如下: public List<List<Integer>> permute(int[] nums) { List<List<Integer>> resul...原创 2019-02-21 00:34:11 · 627 阅读 · 0 评论 -
LeetCode动态规划题
斐波那契数列类LeetCode70. Climbing Stairs题目描述:有 N 阶楼梯,每次可以上一阶或者两阶,求有多少种上楼梯的方法。dp[ i ] = dp[ i-1 ] + dp[ i-2 ];public int climbStairs(int n) { if (n &amp;amp;amp;amp;lt;= 2) { return n; } int pre2 = ...原创 2019-02-12 17:12:36 · 334 阅读 · 0 评论 -
《剑指offer第二版》JAVA代码
/** * 字符串匹配问题 */public class Offer19 { public boolean solve(String s, String pattern) { if (s == null || pattern == null) return false; char[] sArr = s.toCharArray(); c...原创 2018-10-24 23:16:33 · 1152 阅读 · 0 评论 -
二叉查找树
定义:一个二叉查找树是一棵二叉树,其中每个节点都含有一个Comparable键(以及相关联的值),且每个节点中的键都大于其左子树中的任意节点的键,而小于右子树任意节点的键。下面是完整的代码:public class BinarySearchTree<Key extends Comparable<Key>, Value> { private Node...原创 2017-09-03 14:01:00 · 156 阅读 · 0 评论 -
二叉树相关算法:二
LeetCode 437.:Path Sum III You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at th...原创 2018-09-19 02:21:17 · 261 阅读 · 0 评论 -
数组相关算法题
题一:128.:Longest Consecutive Sequence 要求是O(n),所以不能使用排序,那么就得考虑辅助。利用Map,key为num,value为其连续长度,每次之更新这个连续序列的首尾两个位置的value值。 public int longestConsecutive(int[] nums) { Map&amp;amp;amp;amp;lt;Integer, Intege...原创 2019-04-06 06:19:39 · 215 阅读 · 0 评论 -
String类的各种方法
本篇是记录在刷题时遇到的自己不常用的String类的方法, 持续记录中…startsWith(String prefix, int toffset) public boolean startsWith(String prefix, int toffset) { char ta[] = value; int to = toffset; ...原创 2018-09-06 02:01:57 · 209 阅读 · 0 评论 -
Kadane's Algorithm
Kadane是卡内基梅隆大学的教授,这个算法是为了解决最大子数组的和(maximum subarray)提出的。更多的资料可以参考:https://en.wikipedia.org/wiki/Maximum_subarray_problemKadane's algorithm begins with a simple inductive question: if we know the m...原创 2017-11-24 20:49:00 · 1668 阅读 · 0 评论 -
HashMap的computeIfAbsent与computeIfPresent
LeetCode:49 Given an array of strings, group anagrams together.Example:Input: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Output: [ [“ate”,”eat”,”tea”], [“nat”,”tan”], [“bat”] ] Note...原创 2018-08-25 20:58:12 · 5511 阅读 · 0 评论 -
二叉树相关算法
校验一颗二叉树是否是合格的二叉查找树,这是LeetCode的第98题:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with...原创 2018-08-30 23:50:34 · 173 阅读 · 0 评论 -
买卖股票的最佳时机
public class LeetCode122 { // 动态规划解法;buy[i]:代表到i天为止,以buy行为结尾的最大利润;sell[i]与rest[i]类似 // buy[i] = max{ buy[i-1], sell[i-1]-price} // sell[i] = max{ sell[i-1], buy[i-1]+price public in...原创 2018-08-02 19:07:25 · 399 阅读 · 0 评论 -
《算法》中的红黑树实现
有别于上一篇文章介绍的红黑树,在《算法<第四版>》一书中用另一套规则实现了红黑树,主要手段是递归,实现思路来自于2-3树,这本书中有详细的解读,在这里我谈谈自己对它的理解。首先,在之前文章中介绍的红黑树,我们把节点看成红,黑两色,而这里红节点指的是它指向父亲的链接是红色的,有什么不同?当我们介绍左旋,右旋你就会看到。来看看这套定义红黑树的规则:红链接均为左链接 没有任何一个...原创 2017-09-18 10:27:00 · 1423 阅读 · 0 评论 -
红黑树
之前介绍过二叉查找数,平均情况下查找和插入时间复杂需为1.39logn;最坏情况下为n。算法的改进就引出了红黑树,在插入,删除操作中能够保持树本身高度平衡(红黑树并不是标准平衡二叉树,它以下面的性质 5 作为一种平衡方法,使自己的性能得到了提升)的数据结构。红黑树本质上是一种二叉查找树,但它在二叉查找树的基础上额外添加了一个标记(颜色),同时具有一定的规则。这些规则使红黑树保证了一种平衡,插入、删...原创 2017-09-07 11:55:00 · 539 阅读 · 0 评论 -
矩阵
题一:顺时针转圈打印矩阵 public void solve(int[][] matrix) { int line = matrix.length; int row = matrix[0].length; //其实把for改成while看起来更干净些,下面的print里也是 //PS...原创 2018-06-09 19:38:00 · 126 阅读 · 0 评论 -
记录些有意思的算法题
将自己刷题中遇到的有意思的,有巧妙想法的题记录在这,持续更新…LeetCode 338. Counting Bits给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。很容易就想到遍历然后每个数分别进行统计。 public int[] countBits(int num) { int[] ...原创 2019-05-18 14:30:49 · 376 阅读 · 0 评论