- 博客(123)
- 收藏
- 关注
原创 105 从前序与中序遍历序列构造二叉树
105 从前序与中序遍历序列构造二叉树01/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode l
2021-08-30 21:34:50
201
原创 300 最长递增子序列
300 最长递增子序列动态规划class Solution { public int lengthOfLIS(int[] nums) { int n = nums.length; int[] dp = new int[n]; Arrays.fill(dp,1); dp[0] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j &l
2021-08-26 16:03:32
184
原创 435 无重叠区间
435 无重叠区间动态规划(超时)class Solution { public int eraseOverlapIntervals(int[][] intervals) { int n = intervals.length; if (n==0){ return 0; } int[] f = new int[n]; Arrays.fill(f, 1); //对区间按照首节
2021-08-26 14:58:33
254
原创 209 长度最小的子数组
209 长度最小的子数组滑动窗口+队列class Solution { public int minSubArrayLen(int target, int[] nums) { Queue<Integer> que = new LinkedList<>(); int len = nums.length; int min_len = Integer.MAX_VALUE; int sum = 0;
2021-08-26 14:07:33
171
原创 208 实现Trie
208 实现Trie字典树/** * @author zona * @create 2021-08-18 2:48 下午 */class Trie { private Trie[] children; private boolean isEnd; /** * Initialize your data structure here. */ public Trie() { children = new Trie[26];
2021-08-18 15:16:47
144
原创 124 二叉树中的最大路径和
124 二叉树中的最大路径和递归首先,考虑实现一个简化的函数dfs()dfs()dfs(),该函数计算二叉树中的一个节点的最大贡献值,具体而言,就是以该节点为根节点的子树中寻找以该节点为起点的一条路径,使得该路径上的节点值之和最大。该函数的计算如下:空节点的最大贡献值等于000;非空节点的最大贡献值等于节点值与其子节点中的最大贡献值之和(对于叶结点而言,最大贡献等于节点值)。/** * Definition for a binary tree node. * public class
2021-08-17 11:07:47
137
原创 437 路径总和-02
437 路径总和-02前缀和,dfs,回溯/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode le
2021-08-15 21:40:07
129
原创 55 跳跃游戏
55 跳跃游戏动态规划class Solution { public boolean canJump(int[] nums) { int[] dp=new int[nums.length]; dp[0]=1;//1代表可到达,0代表不可到达 for(int i=0;i<nums.length;i++){ if(dp[i]==1){ for(int k=1;k<=nums[i] &
2021-08-12 10:31:04
73
原创 43 字符串相乘
43 字符串相乘模拟竖式class Solution { public String multiply(String num1, String num2) { if ("0".equals(num1) || "0".equals(num2)){ return "0"; } int len1=num1.length(); int len2=num2.length(); List<St
2021-08-12 10:05:26
78
原创 5 最长回文子串
5 最长回文子串动态规划class Solution { public String longestPalindrome(String s) { int len=s.length(); if (s.length()<2){ return s; } //dp[i][j]表示s[i,...j]是否是回文串 boolean[][] dp=new boolean[len][len];
2021-08-06 13:37:07
83
原创 332 重新安排行程
332 重新安排行程求解欧拉回路问题,不是拓扑排序问题!!!Hierholzer算法对于每一个节点,它最多只有一个“死胡同”分支,只有那个入度与出度差为1的节点会导致死胡同。顺序的考虑该问题的时候,也许很难解决该问题,因为无法判断当前节点的哪一个分支是“死胡同分支”。不妨倒过来考虑该问题,只有那个入度与出度差为1的节点会导致死胡同。该该节点必然是最后一个遍历到的节点。因此可以改变入栈的规则,当我们遍历完一个节点所连的所有节点后,我们才将该节点入栈(即逆序入栈)。对于当前节点而言,从它的每一
2021-08-04 20:07:38
125
原创 207课程表
207课程表BFSclass Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { Queue<Integer> que=new LinkedList<>(); int[] inDgrees=new int[numCourses]; int counts=0; for (int[] p:prerequis
2021-08-04 16:03:38
99
原创 210 课程表2
210 课程表2BFS首先建立入度表,入度为0的节点先入队当队列不为空,节点出队,标记学完课程数量的变量加1,并记录该课程将课程的邻居入度减1若邻居课程入度为0,加入队列用一个变量记录学完的课程数量,一个数组记录最终结果,简洁好理解。class Solution { //bfs public int[] findOrder(int numCourses, int[][] prerequisites) { if (numCourses == 0) {
2021-08-04 15:36:04
131
原创 934 最短的桥
934 最短的桥01超时!首先随机搜索,找到一个岛,然后使用DFS将这个岛的所有1变成2,并将1旁边的0都收集起来,为下面的步骤做准备。随后,对每一个收集来的0进行BFS,直到找到第二个岛。class Solution { Queue<int[]> que=new LinkedList<>(); int [][] dir=new int[][]{{-1,0},{1,0},{0,-1},{0,1}}; public int shortestBrid
2021-08-03 00:17:10
107
原创 695 岛屿的最大面积
695 岛屿的最大面积dfs为了确保每个土地访问不超过一次,每经过一块土地,将这块土地的值置为000,这样就不会多次访问同一土地。class Solution { public int maxAreaOfIsland(int[][] grid) { int m = grid.length; int n = grid[0].length; int max_area = 0; for (int i = 0; i < m; i
2021-08-02 21:17:13
79
原创 529 扫雷游戏
529 扫雷游戏深度优先搜索class Solution { int[][] dire=new int[][]{ {-1,-1},{-1,0},{-1,1}, {0,-1},{0,1}, {1,-1},{1,0},{1,1} }; public char[][] updateBoard(char[][] board, int[] click) { //如果一个地雷('M')被挖出,游戏就结束了- 把它改为
2021-08-01 16:19:03
105
原创 130 被围绕的区域
130 被围绕的区域题目中提到:任何边界上的OOO都不会被填充为XXX。可以想到,所有的不被包围的OOO都直接或间接与边界上的OOO相连。因此可以利用这个性质判断OOO是否在边界上,具体地说:对于每一个边界上的OOO,以它为起点,标记所有与它直接或间接相连的字符OOO;最后遍历这个矩阵,对于每一个字母:(1)如果该字母被标记过,则该字母为没有被X包围的字母OOO,将其还原为OOO;(2)如果该字母没有被标记过,则该字母为被字母XXX包围的字母OOO,将其修改为字母XXX。DFSclas
2021-08-01 14:33:15
108
原创 139 单词拆分
139 单词拆分动态规划class Solution { public boolean wordBreak(String s, List<String> wordDict) { int len_s=s.length(); boolean[] dp = new boolean[len_s+1]; dp[0]=true; for (int i=1;i<=len_s;i++){ for (in
2021-08-01 10:08:16
90
原创 200 岛屿数量
200 岛屿数量深度优先搜索class Solution { public int numIslands(char[][] grid) { int m=grid.length; int n=grid[0].length; int counts=0; boolean[][] visited = new boolean[m][n]; for(int i=0;i<m;i++){ Arrays
2021-07-27 19:54:41
101
原创 684 冗余连接
684 冗余连接并查集-01class Solution { public int[] findRedundantConnection(int[][] edges) { int n=edges.length; int[] father=new int[n+1]; int i=n-1; for(;i>0;i--){ if(findNum(edges,father,i,n)==1){
2021-07-27 16:51:34
148
原创 547省份数量
547省份数量深度优先搜索class Solution { public int findCircleNum(int[][] isConnected) { int provinces=isConnected.length; boolean[] visited=new boolean[provinces]; Arrays.fill(visited,false); int circles=0; for (int i=0
2021-07-24 22:08:47
111
原创 85 最大矩形
85 最大矩形使用柱状图的优化暴力解法class Solution { public int maximalRectangle(char[][] matrix) { int rows=matrix.length; if (rows==0){ return 0; } int columns=matrix[0].length; int maxArea=0; int[][] le
2021-07-24 21:04:33
87
原创 84 柱状图中最大的矩形
84 柱状图中最大的矩形暴力解法(超时)枚举宽class Solution { public int largestRectangleArea(int[] heights) { if(heights.length==1){ return heights[0]; } int max_area=0; for (int i = 0; i < heights.length; i++) {
2021-07-23 21:49:21
67
原创 556 下一个更大元素-03
556 下一个更大元素-03线性解法class Solution { public int nextGreaterElement(int n) { int[] nums = new int[10]; int tmp = n; int i = 0; while (tmp > 0) { nums[i++] = tmp % 10; tmp /= 10; }
2021-07-21 16:14:33
67
原创 503 下一个更大元素-02
503 下一个更大元素-02单调栈+循环数组class Solution { public int[] nextGreaterElements(int[] nums) { int[] ans=new int[nums.length]; Arrays.fill(ans,-1); Deque<Integer> s=new LinkedList<>(); s.addLast(0); for (
2021-07-21 11:02:57
63
原创 496 下一个更大元素-01
496 下一个更大元素-01暴力class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int len1=nums1.length; int len2=nums2.length; int[] ans=new int[len1]; for(int i=0;i<len1;i++){ //先找到nums[i]
2021-07-21 09:47:10
70
原创 93 复原IP地址
93 复原IP地址回溯class Solution { static final int SEG_COUNT=4; List<String> ans=new ArrayList<>(); int[] segments = new int[SEG_COUNT]; public List<String> restoreIpAddresses(String s) { dfs(s,0,0); return a
2021-07-20 22:01:13
79
原创 23 合并k个升序链表
23 合并k个升序链表顺序合并01/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this
2021-07-17 21:34:34
87
原创 169多数元素
169多数元素分治使用经典的分治算法递归求解,直到所有的子问题都是长度为111的数组。长度为111的子数组中唯一的数显然是众数,直接返回即可。如果回溯后某区间的长度大于111,必须将左右子区间的值合并。如果他们的众数相同,那么显然这一段区间的众数是他们相同的值。否则,需要比较两个众数在整个区间内出现的次数来决定该区间的众数。class Solution { public int countInRange(int[] nums,int num,int l,int r){ int
2021-07-15 10:00:50
75
原创 63 不同路径-02
63 不同路径-02动态规划class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int rows = obstacleGrid.length; int colums = obstacleGrid[0].length; int[][] f = new int[rows][colums]; for(int i=0;i<row
2021-07-13 20:03:14
92
原创 63不同路径
63不同路径动态规划01class Solution { public int uniquePaths(int m, int n) { int[][] f = new int[m][n]; for(int i=0;i<m;i++){ Arrays.fill(f[i],0); } f[0][0] = 1; for(int i=0;i<m;i++){ for
2021-07-13 19:07:28
73
原创 123买卖股票的最佳时机-03
123买卖股票的最佳时机-03动态规划class Solution { public int maxProfit(int[] prices) { int buy_1 = -prices[0]; int sell_1 = 0; int buy_2 = -prices[0]; int sell_2 = 0; for(int i=1;i<prices.length;i++){ buy_1
2021-07-13 16:36:03
59
原创 122 买卖股票的最佳时机-02
122 买卖股票的最佳时机-02动态规划class Solution { public int maxProfit(int[] prices) { int dp_0 = 0; int dp_1 = -prices[0]; for(int i=1;i<prices.length;i++){ int tmp_dp_0 = Math.max(dp_0,dp_1+prices[i]);
2021-07-10 20:00:33
140
1
原创 322 零钱兑换
322 零钱兑换递归版(超时)class Solution { public int coinChange(int[] coins, int amount) { if(amount==0){ return 0; } if(amount<0){ return -1; } int res =Integer.MAX_VALUE; for(int co
2021-07-08 23:48:36
110
1
原创 剑指 offer 68-02 二叉树的最近公共祖先
剑指 offer 68-02 二叉树的最近公共祖先递归/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { LinkedList<TreeNode> pa
2021-07-05 18:53:33
73
原创 剑指 offer 68-01 二叉搜索树的最近公共祖先
剑指 offer 68-01 二叉搜索树的最近公共祖先两次遍历/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode lowestCom
2021-07-05 16:44:16
94
原创 剑指 offer 67 把字符串转换成整数
剑指 offer 67 把字符串转换成整数代码01class Solution { public int strToInt(String str) { int res = 0; //去除开头结尾的空格 str = str.trim(); if (str.length()==0){ return 0; }// 正负号 正为真 boolean flag =
2021-07-03 19:15:27
76
原创 剑指 offer 66 构建乘积数组
剑指 offer 66 构建乘积数组class Solution { public int[] constructArr(int[] a) { if(a.length==0){ return new int[0]; } int[] b = new int[a.length]; b[0] = 1; for(int i=1;i<a.length;i++){ b[i]
2021-07-03 17:34:41
151
原创 剑指 offer 65 不用加减乘除做加法
剑指 offer 65 不用加减乘除做加法异或 与 左移把a+ba+ba+b转换成非进位+进位,由于不能使用加法,因此要一直转换,直到第二个数变为000。class Solution { public int add(int a, int b) { while (b!=0){ int c = a^b; b = (a&b)<<1; a = c; } ret
2021-07-03 17:01:28
69
原创 剑指 offer 64 求1+2+...+n
剑指 offer 64 求1+2+…+n递归class Solution { public int sumNums(int n) { boolean flag = n>0 && (n+=sumNums(n-1))>0; return n; }}时间复杂度O(n)O(n)O(n)。空间复杂度O(n)O(n)O(n),递归函数的空间复杂度取决于递归调用栈的深度,这里递归函数调用栈深度为O(n)O(n)O(n),因此空间
2021-07-02 16:56:08
61
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人