算法
文章平均质量分 59
dogndaxiaodong
东北大学软件学院大二学生
展开
-
关于单调栈的使用
表现良好的最长时间段--这道题的关键就是前缀和,以及单调栈。前缀和意思是说如果大于8就是1否则就是-1,这样可以形成新的数组,然后新的数组中如果两个数中后面的数大于前面则这个区间就是有效区间。求有效区间的最大值,使用单调栈,先遍历这个数组获取递减值,然后反向遍历数组,和单调栈pop()出来的元素进行比对,并要注意i的相对大小。这用到的关键思想就是递减区间中一定包含最小值。每日温度也用到了单调栈...原创 2020-02-05 15:13:42 · 188 阅读 · 0 评论 -
leetcode-二叉树的中序遍历
class Solution { private List<Integer> retlist = new LinkedList<>(); public List<Integer> inorderTraversal(TreeNode root) { //中序遍历,左臂入栈 TreeNode cur = root;...原创 2020-01-26 21:50:31 · 178 阅读 · 0 评论 -
leetcode-两两交换链表中的节点
class Solution { public ListNode swapPairs(ListNode head) { return swap(head); } public ListNode swap(ListNode leftnode){ if(leftnode==null || leftnode.next == null){ ...原创 2020-01-26 20:06:03 · 97 阅读 · 0 评论 -
二分查找java模板
public static int binarySearch(int[] array,int first,int last,int value){ while(first<last){ int mid = first+(last-first)/2; if(array[mid]<value){ ...原创 2020-01-07 17:12:37 · 170 阅读 · 0 评论 -
leetcode-两数之和
class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer,Integer> map = new HashMap<>(); for(int i=0;i<nums.length;i+=1){ int num ...原创 2019-12-31 20:17:36 · 91 阅读 · 0 评论 -
leetcode-螺旋矩阵
class Solution { private List<Integer> retlist = new LinkedList<>(); private int rowlength; private int columnlength; public List<Integer> spiralOrder(int[][] matri...原创 2019-12-31 20:14:37 · 123 阅读 · 0 评论 -
leetcode-岛屿的最大面积
class Solution { private int[][] dp; private int maxcount = 0; public int maxAreaOfIsland(int[][] grid) { if(grid.length==0){ return 0; } this.dp = new...原创 2019-12-31 20:04:33 · 147 阅读 · 0 评论 -
leetcode-排序链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode sortList(L...原创 2019-12-31 20:01:40 · 91 阅读 · 0 评论 -
leetcode-缺失的第一个正整数
class Solution { public int firstMissingPositive(int[] nums) { Arrays.sort(nums); int likenum = 1; for(int i=0;i<nums.length;i+=1){ int num = nums[i]; ...原创 2019-12-31 19:49:09 · 179 阅读 · 0 评论 -
leetcode-矩阵中的最长递增路径
class Solution { private HashMap<String,Integer> maxmap = new HashMap<>(); private int[][] matrix; private int maxcount = 0; private int rowlength; private int columnl...原创 2019-12-31 19:40:26 · 217 阅读 · 0 评论 -
leetcode-根据身高重建队列
class Solution { public int[][] reconstructQueue(int[][] people) { if(people.length==0){ return people; } Arrays.sort(people, (o1, o2) -> (o1[0]==o2[0])?o1[...原创 2019-12-31 20:16:20 · 170 阅读 · 0 评论 -
leetcode-比特位计数
class Solution { public int[] countBits(int num) { int[] ret = new int[num+1]; ret[0] = 0; for(int i=1;i<=num;i+=1){ ret[i] = ret[i&(i-1)]+1; }...原创 2019-12-29 15:31:31 · 108 阅读 · 0 评论 -
leetcode-二叉树的最近公共祖先
class Solution { private TreeNode ans; public Solution() { // Variable to store LCA node. this.ans = null; } private boolean recurseTree(TreeNode currentNode, TreeN...原创 2019-12-29 15:05:53 · 94 阅读 · 0 评论 -
leetcode-买卖股票的最佳时机
class Solution { public int maxProfit(int[] prices) { if(prices.length==0){ return 0; } if(prices.length==1){ return 0; } int nowmo...原创 2019-12-29 15:02:25 · 77 阅读 · 0 评论 -
leetcode-二叉树的右视图
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...原创 2019-12-29 14:47:54 · 206 阅读 · 0 评论 -
leetcode-重排链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public void reorderList(Li...原创 2019-12-29 14:40:15 · 184 阅读 · 1 评论 -
leetcode-将每个元素替换为右侧最大元素
class Solution { public int[] replaceElements(int[] arr) { LinkedList<Integer> list = new LinkedList<>(); if(arr.length==1){ return new int[]{-1}; ...原创 2019-12-29 14:34:13 · 125 阅读 · 0 评论 -
leetcode-转变数组后最接近目标值的数组和
class Solution { public int findBestValue(int[] arr, int target) { int length = arr.length; Arrays.sort(arr); int minindex = -1; int mincount = -1; for(int...原创 2019-12-29 14:32:01 · 262 阅读 · 0 评论 -
leetcode-层数最深叶子节点的和
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pri...原创 2019-12-29 14:28:58 · 280 阅读 · 0 评论 -
leetcode-子串的最大出现次数
class Solution { public int maxFreq(String s, int maxLetters, int minSize, int maxSize) { HashMap<String,Integer> map = new HashMap<>(); for(int i=0;i+minSize<=s.le...原创 2019-12-27 10:55:53 · 297 阅读 · 0 评论 -
leetcode-划分数组为连续数字的集合
class Solution { public boolean isPossibleDivide(int[] nums, int k) { int len = nums.length; if (len % k != 0) { return false; } PriorityQueue<Int...原创 2019-12-26 21:53:52 · 297 阅读 · 0 评论 -
leetcode-最后一块石头的重量
class Solution { public int lastStoneWeight(int[] stones) { PriorityQueue<Integer> queue = new PriorityQueue<>((o1,o2)->o2-o1); for(int i=0;i<stones.length;i+=1){ ...原创 2019-12-26 21:35:15 · 96 阅读 · 0 评论 -
leetcode-乘积最大子序列
class Solution { public int maxProduct(int[] nums) { if(nums.length==0){ return 0; } if(nums.length==1){ return nums[0]; } i...原创 2019-12-26 21:22:45 · 125 阅读 · 0 评论 -
leetcode-单词拆分
class Solution { public boolean wordBreak(String s, List<String> wordDict) { if(wordDict.size()==0){ return false; } int[] dp = new int[s.length()]; ...原创 2019-12-26 20:54:19 · 91 阅读 · 0 评论 -
leetcode-旋转图像
class Solution { public void rotate(int[][] matrix) { if(matrix==null){ return; } if(matrix.length==1){ return ; } //先转置,再镜像对称 ...原创 2019-12-26 20:47:28 · 99 阅读 · 0 评论 -
leetcode-分发糖果
class Solution { public int candy(int[] ratings) { if(ratings==null){ return 0; } if(ratings.length==1){ return 1; } int count =0;...原创 2019-12-26 20:42:11 · 141 阅读 · 1 评论 -
leetcode-合并两个有序链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode mergeTwoLi...原创 2019-12-26 20:34:56 · 136 阅读 · 0 评论 -
leetcode-买卖股票的最佳时机 II
class Solution { public int maxProfit(int[] prices) { if(prices==null){ return 0; } if(prices.length==0){ return 0; } if(prices.len...原创 2019-12-26 20:30:26 · 63 阅读 · 0 评论 -
leetcode-买卖股票的最佳时机 |||
class Solution { public int maxProfit(int[] prices) { if(prices.length==0){ return 0; } int[][][] dp = new int[prices.length][2][3]; dp[0][0][1] = -pri...原创 2019-12-26 20:26:23 · 79 阅读 · 0 评论 -
leetcode-二叉树的层次遍历
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...原创 2019-12-25 21:28:15 · 115 阅读 · 0 评论 -
leetcode-验证二叉搜索树
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { p...原创 2019-12-25 21:23:29 · 115 阅读 · 0 评论 -
leetcode-二叉树的最大深度
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pri...原创 2019-12-25 21:02:20 · 111 阅读 · 0 评论 -
python列表元素全排列代码解析
今天做算法题,作为一个没有系统学习过算法的小白,碰到了一道需要将列表元素去全排列的题。思考良久,也没有想出来。于是在网上搜索答案(附链接:https://blog.csdn.net/zhoufen12345/article/details/53560099)现将该大神的代码贴出来研究一二。1 COUNT = 02 def perm(n, begin, end):3 global COU...原创 2018-04-23 21:16:25 · 1903 阅读 · 1 评论