
算法
渣一个
这个作者很懒,什么都没留下…
展开
-
85. 最大矩形
/** * 动态柱形图法 * @param matrix * @return */ public static int maximalRectangle(String[][] matrix) { //先算出每一行 最大连续1的个数 //如数组是 "1","0","1","1","1" //则转换成 1,0,1,2,3 if(matrix.length == 0 || matrix...原创 2021-03-02 18:21:58 · 178 阅读 · 0 评论 -
LeetCode 746. 使用最小花费爬楼梯
public int minCostClimbingStairs(int[] cost) { int len = cost.length; for (int i = 2; i < len; i++) { cost[i]=Math.min(cost[i-1]+cost[i],cost[i-2]+cost[i]); } return Math.min(cost[len-1],cost[len-2]); ...原创 2020-12-21 11:01:05 · 275 阅读 · 0 评论 -
LeetCode 209. 长度最小的子数组
思路:评论区的大佬太强了,搬运。len表示结果;j表示此片段的最左索引;sum表示片段和先遍历,逐个递加,直到大于s时,再去缩短长度。public int minSubArrayLen(int s, int[] nums) { int len = 0; int j = 0; int sum = 0; for (int i = 0; i < nums.length; i++) { sum+...原创 2020-12-15 20:29:03 · 94 阅读 · 0 评论 -
LeetCode 201. 数字范围按位与
思路:暴力会超时!引用大佬的思路:【笔记】当一个数+1时,总会有这么一个规律“某一位后的数字,全部被置为相反数”。举个例子:010111 + 1 = 011000,则010111 & 011000 = 010000。那么,x & (x+1) 后几位相反数的“与操作”,结果总为0。所以,当(m,m+1,...n-1,n)进行连续“与操作”时,会按照上述规律被抵消很大一部分,而只剩下n的前缀部分,最后只需将n归位。举个例子:m = 5(0101), n = 7 (011..原创 2020-12-14 16:31:03 · 161 阅读 · 0 评论 -
LeetCode 200. 岛屿数量
思路:定义变量ans记录结果二维数组v记录被访问的位置,逐个遍历数组,当有未被访问或者字符为 1时,进入BFS递归递归条件很简单,控制好边界,并且对应位置字符为 1且未被访问即可。从上下左右四个位置递归。最后每递归结束一次,结果ans+1。返回结果int ans = 0; public int numIslands(char[][] grid) { if(grid.length == 0 || grid[0].length == 0)return...原创 2020-12-14 16:10:35 · 112 阅读 · 0 评论 -
LeetCode 187. 重复的DNA序列
思路:先用Set存储每个以10为单位的字符串切片,若存在重复则加入List中,时间复杂度O(N)空间复杂度O(N)public List<String> findRepeatedDnaSequences(String s) { List<String> l = new ArrayList<>(); if(s.length() < 10)return l; int len = s.length();..原创 2020-12-14 15:28:05 · 258 阅读 · 0 评论 -
LeetCode 166. 分数到小数
思路:循环体出现的依据是,产生重复的余数,即每次mod*10后再和除数b计算出新余数,和之前的比较即可。先对int型数据进行转换为long以免int负数最小值化正越界判定正负数,结果是否加负号求余数,若余数为0则直接返回余数不为0,加入到map中,供下次检测。每次将余数和对应sb的位置 存到map中将mod*10和b计算出来的商追加到sb中,并mod*10 % b再次计算新余数。循环直到mod==0public static String...原创 2020-12-03 11:02:58 · 162 阅读 · 0 评论 -
LeetCode 162. 寻找峰值
为什么二分查找大的那一半一定会有峰值呢?(即nums[mid]<nums[mid+1]时,mid+1~N一定存在峰值) 我的理解是,首先已知 nums[mid+1]>nums[mid],那么mid+2只有两种可能,一个是大于mid+1,一个是小于mid+1,小于mid+1的情况,那么mid+1就是峰值,大于mid+1的情况,继续向右推,如果一直到数组的末尾都是大于的,那么可以肯定最后一个元素是峰值,因为nums[nums.length]=负无穷public int findPea...原创 2020-11-30 20:59:11 · 90 阅读 · 0 评论 -
LeetCode 767. 重构字符串
思路:贪心+大顶堆1.统计每个字符出现次数,找到最多出现此处若大于(len+1)/2,则直接返回空。2.按照出现次数直接建立大顶堆。3.每次从大顶堆取两个字符,加入StringBuffer,以此类推。4.若队列剩下最后一个元素,则直接加入,否则返回参考:官方解法一public String reorganizeString(String S) { if (S.length() < 2) { return S; }..原创 2020-11-30 19:05:42 · 156 阅读 · 0 评论 -
LeetCode 617. 合并二叉树
自闭了,这简单题没做出。class Solution { public TreeNode mergeTrees_1(TreeNode t1, TreeNode t2) { if (t1 == null) { return t2; } if (t2 == null) { return t1; } // 先合并根节点 t1.val += t2.val...原创 2020-11-29 11:34:49 · 86 阅读 · 0 评论 -
三道题套路解决递归问题
转自:https://lyl0724.github.io/2020/01/25/1/递归解题三部曲何为递归?程序反复调用自身即是递归。我自己在刚开始解决递归问题的时候,总是会去纠结这一层函数做了什么,它调用自身后的下一层函数又做了什么…然后就会觉得实现一个递归解法十分复杂,根本就无从下手。相信很多初学者和我一样,这是一个思维误区,一定要走出来。既然递归是一个反复调用自身的过程,这就说明它每一级的功能都是一样的,因此我们只需要关注一级递归的解决过程即可。如上图所示,我们需要关心的.原创 2020-11-27 20:58:52 · 281 阅读 · 0 评论 -
LeetCode1143. 最长公共子序列
找到了一个优质的解答这里附上Java版本递归和非递归方式(说明递归方式会超时)public static int longestCommonSubsequence(String text1, String text2) { return dp(text1.length()-1,text2.length()-1,text1,text2); } private static int dp(int i, int j,String s1,String s2) { ..原创 2020-11-27 19:07:57 · 272 阅读 · 0 评论 -
LeetCode 148. 排序链表
思路:归并排序1.先快慢指针将链表分为左右两部分,2.递归将半部分链表再分两部分,3.将两部分有序的链表合并为有序链表返回。public static ListNode sortList(ListNode head) { return head == null ? head : megsort(head); } public static ListNode megsort(ListNode head) { if(head.next == ..原创 2020-11-26 14:25:43 · 82 阅读 · 0 评论 -
LeetCode 146. LRU 缓存机制
思路:运用LinkedHashMap保存键值对,并把最近使用的键值对放在链最后。最旧的放在最前。static HashMap<Integer,Integer> map = new LinkedHashMap<>(); static int c = -1; public LRUCache(int capacity) { c = capacity; } public static int get(int key) { ...原创 2020-11-26 11:43:13 · 177 阅读 · 0 评论 -
LeetCode 164. 最大间距
思路:先对数组进行基数排序(或桶排序)时间复杂度为N,再遍历比较获取最大间距。public static int maximumGap(int[] nums) { if(nums.length < 2)return 0; int[] buf = new int[nums.length]; long dep = 1; int n = nums.length; int maxVal = Arrays.stream(n..原创 2020-11-26 10:55:36 · 209 阅读 · 0 评论 -
LeetCode 137. 只出现一次的数字 II
思路:讨论组有大佬想到了设计电路问题,即用两位二进制来表示某个数出现的次数,当出现3次时,置为0;但没理解透彻 还不还扩展,这里介绍另一位大佬的思路。由32位二进制数表示结果,从第一位开始计算,所有数在该位置出现的1的个数要么为3N或者3N+1,当为3N时,不起作用;当为3N+1时起作用,并把当前位的值 |=result 逐位统计数值,public static int singleNumber(int[] nums) { int len = nums.leng...原创 2020-11-25 11:27:19 · 134 阅读 · 0 评论 -
LeetCode 1497. 检查数组对是否可以被 k 整除
public static boolean canArrange(int[] arr, int k) { int[] h = new int[k]; for(int num : arr){ int mod = num%k; if(mod < 0)mod+=k;//将负余数 变为正余数 h[mod]++;//对应位置+1 } if((h[0]&1) ...原创 2020-11-24 10:35:53 · 128 阅读 · 0 评论 -
LeetCode 1663. 具有给定数值的最小字符串
public static String getSmallestString(int n, int k) { /** * 此处分析 为何使用布尔变量is * 当遇到案例 24 552时 * 经第一次for循环,得到count为 20 即20个z,k为9+23=32,n=4 * 此时叠加20个z,但是下次循环 k为32 还能追加一个z,所以 控制变量i 还是从26开始遍历。(即当进入嵌套for循环时 才...原创 2020-11-24 08:52:19 · 162 阅读 · 0 评论 -
LeetCode 1578. 避免重复字母的最小删除成本
public static int minCost(String s, int[] cost) { if(cost.length < 2) return 0; int len = s.length(); int r = 0; for (int i = 1; i < len; i++) { if(s.charAt(i) == s.charAt(i-1)){//当相邻字母相同时 ...原创 2020-11-23 22:04:00 · 105 阅读 · 0 评论 -
LeetCode 646. 最长数对链
思路:贪心算法可行。public static int findLongestChain(int[][] pairs) { if(pairs.length < 2)return pairs.length; Arrays.sort(pairs,(a,b)->(a[1] - b[1])); int r = 1; int temp = pairs[0][1]; for (int i = 1; i < pai..原创 2020-11-23 21:37:17 · 149 阅读 · 0 评论 -
LeetCode 452. 用最少数量的箭引爆气球
思路:先把二维数组按右边界排序,先取右边界为一支箭,去掉射爆的气球,并计数;然后循环此过程。另外,学习了Arrays.sort(points,(a,b)->(a[1]-b[1]));对二位数组排序,新方式public static int findMinArrowShots(int[][] points) { if(points.length < 1)return 0; Arrays.sort(points,(a,b)->(a[1...原创 2020-11-23 19:21:33 · 179 阅读 · 0 评论 -
LeetCode 131. 分割回文串
经典溯源题目:public static List<List<String>> partition(String s) { List<List<String>> ll = new ArrayList<>(); List<String> l = new ArrayList<>(); if(s == null || s.length() == 1){ ..原创 2020-11-22 22:09:46 · 92 阅读 · 0 评论 -
LeetCode 130. 被围绕的区域
思路:由于直接找被X包围的O区域较困难,所以先遍历最外围的一层(dfs深度遍历),找到不被X包围的O,并把其位置替换为 -标记。最后再把里面的O变为X(里面的O表示被X包围的区域),再把被标记 -的位置重新赋值为 O。public static void solve(char[][] board) { if(board.length <= 1 || board[0].length <= 1)return; int row = board...原创 2020-11-18 20:33:10 · 141 阅读 · 0 评论 -
LeetCode 134. 加油站
思路:首先从那个加油站开始需要逐个遍历,当从第i个开始时,定义:remaingas 表示车还剩多少油,count总共走了多少站了,index目前到了哪一站,当油量小于0时break。直到找到走完一遍油量大于等于0时的 i。否则返回-1。public static int canCompleteCircuit(int[] gas, int[] cost) { int len = gas.length; for (int i = 0; i < ...原创 2020-11-18 20:07:48 · 204 阅读 · 0 评论 -
LeetCode 120. 三角形最小路径和
思路:从下(倒数第二行)往上开始计算,找到最先和之后,每一行的值重新赋值,直到最顶层就是最小值了。public int minimumTotal(List<List<Integer>> triangle) {List<List<Integer>> ll = new ArrayList<>(triangle); for (int i = triangle.size() - 2; i >= 0 ; i--) ...原创 2020-11-17 20:22:55 · 291 阅读 · 0 评论 -
LeetCode 119. 杨辉三角 II
/** * 获取杨辉三角的指定行 * 直接使用组合公式C(n,i) = n!/(i!*(n-i)!) * 则第(i+1)项是第i项的倍数=(n-i)/(i+1); */ public List<Integer> getRow(int rowIndex) { List<Integer> l = new ArrayList<>(); long cur = 1; for(int i=0;i<=rowIn...原创 2020-10-21 22:22:57 · 278 阅读 · 0 评论 -
LeetCode 109. 有序链表转换二叉搜索树
思路:快慢指针找到中间节点,中间节点为根节点,将链表分为左右两部分(注意前半部分节点next置null),左部分为左子树,右部分为右子树递归实现左右子树,返回根节点public static TreeNode sortedListToBST(ListNode head) { if(head == null)return null; else if(head.next==null)return new TreeNode(head.val); ..原创 2020-10-21 20:29:46 · 123 阅读 · 0 评论 -
LeetCode 93. 复原IP地址
思路:回溯+剪枝遍历字符串,先计算第一网段,分别取1 2 3位数字,并组合判断是否符合IP规范,若符合跳至下一网段,并且索引start挪位,temp+本网段值+“.”递归List<String> l = new ArrayList<>(); public List<String> restoreIpAddresses(String s) { if(s.length()<4 || s.length() > ...原创 2020-10-21 17:03:20 · 263 阅读 · 1 评论 -
LeetCode 143. 重排链表
思路:1.快慢指针找到中间节点2.翻转后半部分链表3.遍历前半部分链表,将后半部分链表插入前半部分间隙中if(head==null || head.next==null)return; ListNode r = head; ListNode fast = head; ListNode slow = head; //快慢指针找到中间节点 while (fast!=null && fast.ne..原创 2020-10-20 23:12:29 · 90 阅读 · 0 评论 -
LeetCode 92. 反转链表 II
思路:以题目链表示例1.先头插如节点02.定位到节点2 前驱节点13.遍历从m到n翻转这一区域4.最后将节点5追加到节点t(2)的后面5.连接节点1和4public static ListNode reverseBetween(ListNode head, int m, int n) { if(head==null)return head; ListNode insertNode = new ListNode(0); ...原创 2020-10-19 22:51:25 · 103 阅读 · 0 评论 -
LeetCode 91. 解码方法
/** 上楼梯的复杂版? 如果连续的两位数符合条件,就相当于一个上楼梯的题目,可以有两种选法: 1.一位数决定一个字母 2.两位数决定一个字母 就相当于dp(i) = dp[i-1] + dp[i-2]; 如果不符合条件,又有两种情况 1.当前数字是0: 不好意思,这阶楼梯不能单独走, dp[i] = dp[i-2] 2.当前数字不是0 ...原创 2020-10-18 22:37:13 · 139 阅读 · 0 评论 -
LeetCode 89. 格雷编码
思路:要说厉害 还是评论区里面的大佬厉害,格雷码生成公式G(i)=i ^ (i/2)另外 Math.pow(2,n) 和 1 << n速率还是差不少的,前者1ms超83% 后者 0ms超100%/** 关键是搞清楚格雷编码的生成过程, G(i) = i ^ (i/2); 如 n = 3: G(0) = 000, G(1) = 1 ^ 0 = 001 ^ 000 = 001 G...原创 2020-10-15 22:09:49 · 248 阅读 · 0 评论 -
LeetCode 86. 分隔链表
思路:和官方思路类似:创建两头指针before和end,遍历链表,把大于等于x的节点加到end后面,小于x的节点加到before后面,最后合并两链表,end加在before后面。public static ListNode partition(ListNode head, int x) { if(head == null || head.next == null)return head; ListNode r1 = new ListNode(-1); ...原创 2020-10-15 21:39:16 · 119 阅读 · 0 评论 -
LeetCode 82. 删除排序链表中的重复元素 II
public static ListNode deleteDuplicates(ListNode head) { if(head==null ||head.next==null)return head; ListNode l = new ListNode(0); l.next=head; ListNode pre = l; ListNode cur = head; while (cur!=null &...原创 2020-10-14 11:00:44 · 218 阅读 · 0 评论 -
LeetCode 1002. 查找常用字符
思路:找出各个字符串的交集,public static List<String> commonChars(String[] A) { if(A.length < 1)return null; List<String> l = new ArrayList<>(); int[] a = new int[26]; for(char c : A[0].toCharArray()) {//记录第一个字符..原创 2020-10-14 10:10:37 · 350 阅读 · 0 评论 -
LeetCode 81. 搜索旋转排序数组 II
//C++最简洁的二分法分类讨论//每次二分,左半部分和右半部分至少有一边是有序的,以此为条件可以分成两种情况://1、左半边是有序的//(1) target落在左半边//(2) otherwise//2、右半边是有序的//(1) target落在右半边//(2) otherwise//综上所述,一共两种可能性,这两种情况各自又有两种可能性,代码如下:public static boolean search(int[] nums, int target) { ...原创 2020-10-12 23:22:09 · 272 阅读 · 0 评论 -
LeetCode 80. 删除排序数组中的重复项 II
膜拜评论区的大佬public static int removeDuplicates(int[] nums) { int i = 0; for (int n:nums) { if(i<2 || n>nums[i-2]) nums[i++]=n; } return i; }原创 2020-10-10 23:15:27 · 124 阅读 · 0 评论 -
LeetCode 77. 组合
和之前 40.组合总数||类似 记得去重public static List<List<Integer>> combine(int n, int k) { if(n==0 || k==0 || n<k)return null; List<List<Integer>> ll = new ArrayList<>(); int[] a = new int[n+1]; ...原创 2020-10-10 22:45:44 · 101 阅读 · 0 评论 -
LeetCode 74. 搜索二维矩阵
二维数组当成一维数组二分查找public static boolean searchMatrix(int[][] matrix, int target) { if(matrix.length < 1 || matrix[0].length < 1) return false; int row = matrix.length; int col = matrix[0].length; int l = 0,r = col*ro...原创 2020-10-10 20:41:06 · 107 阅读 · 0 评论 -
LeetCode 142. 环形链表 II
思路:先用快慢指针判断是否有环,当有环时,让慢指针指向头结点,快慢一起走,相遇点就是环起点可参考链表环路public static ListNode detectCycle(ListNode head) { if(head==null || head.next==null) return null; boolean exit = false; ListNode fast = head,slow=head; while (slo...原创 2020-10-10 20:04:17 · 108 阅读 · 0 评论