摘要: 用简单的暴力法或者一次遍历,用minprice和maxprofit记录最小买入价格以及最大收益即可。 public int maxProfit(int prices[]) { int minprice = Integer.MAX_VALUE; int maxprofit = 0; for (int 阅读全文
posted @ 2021-08-20 09:41 毅毅毅毅毅 阅读(60) 评论(0) 推荐(0)
摘要: 说道回溯算法其实也就是我们常说的DFS深度遍历算法,而一个回溯的过程,其实也就是一个决策树遍历的过程。 代码大致框架为: result = [] def backtrack(路径, 选择列表): if 满足结束条件: result.add(路径) return for 选择 in 选择列表: 做选择 阅读全文
posted @ 2021-08-18 10:15 毅毅毅毅毅 阅读(534) 评论(0) 推荐(0)
摘要: 很正常的DFS深度遍历或者暴力递归也可以。 class Solution { List<List<Integer>> res=new LinkedList<>(); public List<List<Integer>> permute(int[] nums) { LinkedList<Integer 阅读全文
posted @ 2021-08-18 10:10 毅毅毅毅毅 阅读(33) 评论(0) 推荐(0)
摘要: easy题不多赘述,直接暴力一遍即可。 public boolean checkRecord(String s) { int asum=0; int index=0; for(int i=0;i<s.length();i++) { if(s.charAt(i)=='L') { if(++index> 阅读全文
posted @ 2021-08-17 10:08 毅毅毅毅毅 阅读(36) 评论(0) 推荐(0)
摘要: 滑动窗口通用解法。不赘述。贴代码: public int lengthOfLongestSubstring(String s) { Map<Character,Integer> window=new HashMap<>(); int left=0,right=0; int length=0; whi 阅读全文
posted @ 2021-08-14 14:56 毅毅毅毅毅 阅读(21) 评论(0) 推荐(0)
摘要: 滑动窗口的经典用法,左右指针维持窗口,当找到合适子串就返回。 public boolean checkInclusion(String s1, String s2) { Map<Character,Integer> need=new HashMap<>(); Map<Character,Intege 阅读全文
posted @ 2021-08-13 17:10 毅毅毅毅毅 阅读(42) 评论(0) 推荐(0)
摘要: 滑动窗口算法的应用,通过两个while循环分别控制右指针和收缩条件 public List<Integer> findAnagrams(String s, String p) { List<Integer> res=new ArrayList<>(); Map<Character,Integer> 阅读全文
posted @ 2021-08-12 11:14 毅毅毅毅毅 阅读(29) 评论(0) 推荐(0)
摘要: 滑动窗口的经典范例,具体解法已在算法思想的滑动窗口中阐述,贴个解答代码。 public String minWindow(String s, String t) { Map<Character,Integer> need=new HashMap<>(); //子串窗口 Map<Character,I 阅读全文
posted @ 2021-08-11 15:39 毅毅毅毅毅 阅读(31) 评论(0) 推荐(0)
摘要: 关于双指针的方法,可能大家并不陌生,而滑动窗口算法,其实算是双指针的一种实现方式,主要用于解决子串问题。并且在leetCode上起码有10道以上的滑动窗口应用题目,难度均在middle和hard。因此,我本文也致力于阐述自己的想法,供大家互相学习。 首先滑动窗口算法,顾名思义,就是要维护一个窗口,然 阅读全文
posted @ 2021-08-11 15:34 毅毅毅毅毅 阅读(279) 评论(0) 推荐(0)
摘要: 相对来说属于middle中的easy了,五分钟ac,暴力,双指针等都可以,暴力的话因为只要求三个数组,所以只比较后一个和前一个和最初的d即可,反而速度是最快的。 再贴个官方的解法,很巧妙,将暴力的复杂度降到了O(n) 链接:https://leetcode-cn.com/problems/arith 阅读全文
posted @ 2021-08-10 10:06 毅毅毅毅毅 阅读(43) 评论(0) 推荐(0)