![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
wmtmw
之前的博客:https://wmtmw.github.io/
展开
-
leetcode2.两数相加(java)
思路这道题最开始遇到的问题是题目理解错了。使用链表逆序存数,也就是头节点就是个位,开始以为要遍历到尾节点开始算(可能正常人都不会这么想)补零法如果节点为空,则该节点值为0int x = l1 == null ? 0 : l1.val;int y = l2 == null ? 0 : l2.val;进位用carry表示进位int result = x + y + carry;carry = result / 10;注意,最后一个进位不要忘记,如果有的话。if(carry == 1)原创 2021-07-21 00:07:41 · 159 阅读 · 0 评论 -
leetcode1.两数之和
题目时间复杂度O(N^2)class Solution { public int[] twoSum(int[] nums, int target) { int len = nums.length; for(int i = 0;i < len;i++){ for(int j = i+1;j < len;j++){ if(nums[i] + nums[j] == target){原创 2021-07-20 00:13:17 · 119 阅读 · 0 评论 -
leetcode382.链表随机节点:蓄水池抽样问题
具体代码/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { private ListNode head; private Random rdm; /** @param head The linked.原创 2020-09-17 17:07:05 · 66 阅读 · 0 评论 -
leetcode398.随机数索引:蓄水池抽样问题
假设当前正要读取第n个数据,则我们以1/n的概率留下该数据,否则留下前n-1个数据中的一个,以这种方式选择,所有数据流中数据被选择的概率一样。具体代码class Solution { int[] nums; public Solution(int[] nums) { this.nums = nums; } public int pick(int target) { Random r = new Random(); .原创 2020-09-17 16:32:34 · 101 阅读 · 0 评论 -
概率题.抽签问题
题目题库中有X道简单题和Y道难题,现在要增加题库里的题目,由各位出题老师依次出题。轮到某位出题老师的时候,他先从题库中随机任选一道题,如果选的是简单题,就出Z道简单题并录入题库,否则就出Z道难题并录入题库。则第m位老师出简单题的概率为?第一位老师出简单题的概率为:X/(X+Y)第二位老师出简单题的概率为:所以无论是第几位老师,最后出简单题的概率都是原始的X/(X+Y)...原创 2020-09-12 16:24:53 · 2019 阅读 · 0 评论 -
leetcode787.k站中转内最便宜的航班
题目思路dp[i][k]表示经过k个中转站后到达站i的最低费用初始化:dp[src][0]~dp[src][k] = 0,其余值为无穷大状态转移方程:dp[flight[1]][k] = Math.min(dp[flight[1]][k],dp[flight[0]][k-1]+flight[2]);具体代码class Solution { public int findCheapestPrice(int n, int[][] flights, int src, int dst, int原创 2020-09-09 09:44:46 · 134 阅读 · 0 评论 -
leetcode123.买卖股票的最佳时机III:一维动态规划
题目思路一:三维动态规划dp[i][j][k]:i表示第几天,j表示是否持有股票,0表示不持有,1表示持有k表示第几次交易,取值0,1,2具体代码class Solution { public int maxProfit(int[] prices) { if(prices.length < 2) return 0; int len = prices.length; int[][][] dp = new int[len][2][3]原创 2020-09-08 15:04:37 · 123 阅读 · 0 评论 -
leetcode120.三角形最小路径和:动态规划
题目思路从下往上走,dp[j] =Math.min(dp[j],dp[j+1])+triangle.get(i).get(j);具体实现class Solution { public int minimumTotal(List<List<Integer>> triangle) { int len = triangle.size(); int[] dp = new int[len+1]; for(int i = len-原创 2020-09-08 10:34:26 · 79 阅读 · 0 评论 -
leetcode139.单词拆分:动态规划
题目思路具体实现class Solution { public boolean wordBreak(String s, List<String> wordDict) { Set<String> set = new HashSet<String>(wordDict); boolean[] dp = new boolean[s.length()+1]; dp[0] = true; for(int原创 2020-09-07 16:13:39 · 43 阅读 · 0 评论 -
leetcode22.括号生成:深度优先遍历
题目方法一:深度优先遍历总共有n个左括号和n个右括号left(right)表示使用了多少个左(右)括号当左右括号的使用数量为n时,把结果字符串加入结果集当左括号使用数量小于右括号时,不符合题意,return若当前仍有左括号可以使用,产生左分支if (left < n) dfs(curStr + "(", left + 1, right, n, res);当仍有右括号可以使用,且右括号剩余数量>左括号(不满足的情况已经在4时return了),产生右分支if (right &l原创 2020-09-07 15:48:17 · 76 阅读 · 0 评论 -
leetcode96.不同的二叉搜索树:动态规划
题目思路dp[i]表示以i为根节点的二叉搜索树种数初始化dp[0] = 1; dp[1]=1状态转移方程: dp[i] += dp[i-1]*dp[n-i];具体实现class Solution { public int numTrees(int n) { int[] dp = new int[n+1]; dp[0] = 1; dp[1] = 1; for(int i = 2;i < n+1;i++){原创 2020-09-07 15:11:20 · 60 阅读 · 0 评论 -
leetcode76.最小覆盖子串:滑动窗口
题目思路:滑动窗口使用new int[128]数组保存t中每个字符出现的次数使用new int[128]数组保存窗口中每个字符出现的次数使用minLength表示最小子串长度。使用count记录目前窗口中的有效字符数,当count == t.length()时,窗口长度为一个包含t中所有字符的子串的长度,更新minLength窗口右移,直到窗口中包含有效字符数量小于t.length()class Solution { public String minWindow(String原创 2020-09-07 11:30:46 · 104 阅读 · 0 评论 -
一个二维乱序数组,寻找不在数组中的最小自然数(java)。
思路对二维数组进行排序假设数组为M行N列,if (nums[i][j] != i*N+j) return i*N+jClass Solution{ public int findNum(int[][] nums){ Arrays.sort(nums, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { if (o1[0] == o2[0]) retu原创 2020-09-07 10:38:34 · 292 阅读 · 0 评论 -
leetcode128.最长连续序列
题目方法一:集合用set去重当前元素为nums[i],如果set中包含nums[i]-1的数,则跳过(这是一种优化,从连续子序列的起点开始记录,可以减少遍历次数)如果set中不包含nums[i]-1的数,则遍历记录连续增长序列长度。class Solution { public int longestConsecutive(int[] nums) { Set<Integer> num_set = new HashSet<Integer>();原创 2020-09-07 10:31:09 · 92 阅读 · 0 评论 -
leetcode.字典序问题汇总
leetcode386题目具体实现class Solution { List<Integer> res = new ArrayList<>(); public List<Integer> lexicalOrder(int n) { for(int i = 1;i < 10;i++){ dfs(n,i); } return res; } public voi原创 2020-09-06 09:36:49 · 176 阅读 · 0 评论 -
leetcode199.二叉树的右视图(java):BFS和DFS
题目思路一:BFS层次遍历,记录每层最右侧元素具体实现class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> res = new ArrayList<>(); if(root == null){ return res; } Queue<TreeNode&原创 2020-09-01 12:11:25 · 178 阅读 · 0 评论 -
leetcode1044.最长重复子串(java)
题目思路1.二分法确定子串长度2.Rabin-Karp字符串编码因为均是小写字母,所以系数a = 26假设abcde 分别编码为01234“abcd”编码为h = 0 * (26 ^ 3) + 1 * (26 ^ 2) + 2 * (26 ^ 1) + 3 * ( 26 ^ 0)窗口右移“bcde”编码为h = (h - 0 * (26 ^ 3)) * 26 + 4还要注意a^L的值可能非常大,可能导致整数上溢出,所以通常会对编码值取模,h = h % modulus具体代码cl原创 2020-09-01 11:33:05 · 257 阅读 · 0 评论 -
leetcode.两个二进制数相加(java)
题目“1001”+“1” = “1010”具体实现写的极其复杂,自己都看不下去了。import java.util.Scanner;public class Main { public static void main(String[] args) { String s1 = "1001"; String s2 = "1"; StringBuilder sb = new StringBuilder(); int i = s1.原创 2020-08-11 16:59:22 · 770 阅读 · 0 评论 -
leetcode121.买卖股票的最佳时机(java)
题目思路一次遍历具体实现class Solution { public int maxProfit(int[] prices) { int len = prices.length; int cha = 0; int maxProfit = 0; for(int i = 0;i < len-1;i++){ cha = Math.max(0,cha + prices[i+1]-prices[i]);原创 2020-08-11 00:20:03 · 61 阅读 · 0 评论 -
leetcode15.三数之和(java)
题目当年面试为了安全起见,写了三重循环,这次可不能这样了。具体代码class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); int len = nums.length; if(nums == null || len <原创 2020-08-09 23:30:15 · 59 阅读 · 0 评论 -
leetcode160.相交链表(java)
老网抑云了题目就是剑指offer里两个链表的公共节点思路a+c+b = b + c + a具体代码/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */publi.原创 2020-08-09 20:13:53 · 55 阅读 · 0 评论 -
leetcode206.翻转链表(java)
题目具体代码/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode reverseList(ListNode head) { ListNode pre = null;原创 2020-08-09 11:37:42 · 56 阅读 · 0 评论 -
leetcode25.K个一组翻转链表(java)
题目思路分组,每k个一组,断链可以实现分组翻转链表具体实现/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode reverseKGroup(ListNode head, i原创 2020-08-09 10:40:41 · 87 阅读 · 0 评论 -
笔试题.万万没想到之抓捕孔连顺
这道题一开场就觉得是组合数,怎么写出来不对呢!!原来是很多个组合数相加i表示区间起始,j表示区间末尾,若该(j和i之间的距离<=D),那么假设第一个人站j的位置,剩下的人在(i,j-1)之间任选两个位置(num = j-i,result += num*(num-1)/2),j++如果(i,j)之间距离>D,i++大概是这么个意思具体代码import java.util.Scanner;public class Main{ public static void main(St.原创 2020-07-18 16:49:25 · 129 阅读 · 0 评论 -
笔试题.自动校对机
看了评论,正则表达式实在是妙啊!(最近写了个大小端转换的程序,也是想用正则)import java.util.Scanner;public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); for(int i = 0;i < .原创 2020-07-18 15:52:59 · 77 阅读 · 0 评论 -
leetcode114.二叉树展开为链表(java)
题目官方题解好有意思/** * 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 left, TreeNod原创 2020-07-15 00:06:07 · 150 阅读 · 0 评论 -
leetcode605.种花(java)
题目思路:就是最简单的思路如果i = 0,判断右边是否为0如果i = length-1,判断左边是否为0否则,左右都需要判断。这个题解的代码写的很妙啊if(flowerbed[i] == 0 && (i == 0 || flowerbed[i-1]==0) && (i == flowerbed.length-1 || flowerbed[i+1]==0))具体代码class Solution { public boolean canPlaceFl原创 2020-06-08 10:47:42 · 147 阅读 · 0 评论 -
leetcode621.任务调度器(java)
题目这道题也挺有意思的。如果题目有提到使用大写字母,或者小写字母表示balabala的,都可以考虑用一个new int[26]的数组来存。class Solution { public int leastInterval(char[] tasks, int n) { int[] map = new int[26]; for(char ch : tasks){ map[ch-'A']++; } Arrays原创 2020-06-07 11:27:09 · 110 阅读 · 0 评论 -
leetcode31.下一个排序
题目思路从后往前遍历数组,直到找到nums[i] < nums[j],i < j, 那么j到end为降序从j往后遍历,遍历到nums[i] < nums[k],nums[i] >= nums[k+1],交换nums[i]和nums[k]将j到end进行升序排序。具体代码class Solution { public void nextPermutation(int[] nums) { if(nums.length <= 1){原创 2020-06-07 10:23:11 · 78 阅读 · 0 评论 -
leetcode546.移除盒子(java)
题目一看题目以为和戳气球一个套路,然后难哭了,思路之后补吧具体代码class Solution { public int removeBoxes(int[] boxes) { int dp[][][] = new int[100][100][100]; return calculate(boxes,dp,0,boxes.length-1,0); } public int calculate(int[] boxes,int[][][] dp,in原创 2020-06-06 22:57:21 · 217 阅读 · 0 评论 -
leetcode134.加油站(java)
题目具体代码class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int len = gas.length; for(int i = 0;i < len;i++){ if(gas[i] >= cost[i]){ boolean flag = can(gas,cost,i,0);原创 2020-06-03 12:02:07 · 134 阅读 · 0 评论 -
面试题04.06.后继者(找出二叉树中指定节点的下一个节点)(java)
题目具体代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode inorderSuccessor(TreeNode root, Tre原创 2020-06-03 11:31:01 · 216 阅读 · 0 评论 -
leetcode226.翻转二叉树(java):递归+迭代
题目思路递归交换具体代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode invertTree(TreeNode root) {原创 2020-06-02 22:48:32 · 104 阅读 · 0 评论 -
leetcode662.二叉树最大宽度(java)
题目思路1.二叉树的层序遍历(队列),在往队列中存值的时候,将节点的val值改掉,比如root.val = 1; if root.left != null root.left.val = 2 * root.val; if root.right != null root.right.val = 2*root.val;2.如果队列不为空,则获取队列长度size,即为该层所有节点,记下第一个节点的值v1,和最后一个节点的值v2,该层宽度为v2-v1+1代码/** * Definition for a原创 2020-06-02 14:57:46 · 337 阅读 · 0 评论 -
leetcode106/105.从中序与后序(/前序)遍历构造二叉树(java):分治
题目代码/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode buildTree(int[] inorder, int[] postord原创 2020-06-02 11:35:34 · 75 阅读 · 0 评论 -
leetcode1257.最小公共区域(java)
题目思路:这道题和二叉树最近公共祖先类似代码class Solution { public String findSmallestRegion(List<List<String>> regions, String region1, String region2) { Map<String,String> map = new HashMap<>(); for(List<String> list : r原创 2020-06-02 10:08:02 · 233 阅读 · 0 评论 -
leetcode208.实现前缀树(java)
题目思路1.因为假设输入时小写字母,共有26种可能,用数组保存,能用数组就别用hashmap。其他之后再补。具体代码class Trie { /** Initialize your data structure here. */ class TrieNode{ private TrieNode[] link; private final int R = 26; private boolean isEnd; publi原创 2020-06-01 23:03:38 · 122 阅读 · 0 评论 -
leetcode212.单词搜索II(java)
题目回溯+深度优先遍历代码一class Solution { List<String> res = new ArrayList<>(); boolean flag = false; public List<String> findWords(char[][] board, String[] words) { int row = board.length; int col = board[0].length;原创 2020-06-01 22:22:05 · 141 阅读 · 0 评论 -
leetcode79.单词搜索(java):回溯+深度优先遍历
题目思路1.一个重要的点:找到单词后,结束遍历,返回true。这里可以设置一个全局变量flag,如果深度优先遍历找到这个单词后,flag=true,检测到flag为true后,立刻返回true,不再继续遍历。对应测试用例:board中有多个满足word的路径。[["a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"原创 2020-06-01 20:16:43 · 175 阅读 · 0 评论 -
leetcode576.判断一个字符串是否包含另一个字符串的全排列(java)
具体代码public class Main{ public static boolean IsContatined(String s1,String s2){ if(s2.length()<s1.length()){ return false; } //因为全是小写字母,共有26种可能,用数组存储比用map存储方便。 int[] nums1 = new int[26]; int[] num原创 2020-06-01 16:38:12 · 1188 阅读 · 1 评论