
Leetcode题解
文章平均质量分 71
忧伤的肚腩
华中科技大学计算机专业在读
展开
-
Leetcode 470
Leetcode 470解法一:7进制数据(等概率即可)rand7() + rand7 不是等概率,转换为7进制时候,就转换成为了等概率事件,class Solution {public: int rand10() { int result=0; do{ int rand0= rand7(); int rand2= rand7(); result = (rand2-1) * 7 + ran原创 2020-09-13 13:14:37 · 356 阅读 · 0 评论 -
LeetCode91. Decode Ways
描述A message containing letters from A-Z is being encoded to numbers using the following mapping:‘A’ -> 1‘B’ -> 2…‘Z’ -> 26Given a non-empty string containing only digits, determine the ...原创 2018-09-21 15:38:05 · 126 阅读 · 0 评论 -
一维动态规划总结
题目列表给一个N(输入),求某种情况的最大值或者最小值情况,279. Perfect Squares思路最差情况下,总体是定义一个dp[N+1], 或者初始化前面dp[0]或者dp[1],#279. Perfect Squares解析Given a positive integer n, find the least number of perfect square numbers ...原创 2018-09-26 23:39:52 · 742 阅读 · 0 评论 -
Edit distance(二维动态规划题目)
题目1 Edit Distance传统动态规划问题,两个字符串不一样,对第一个字符每一个位置可以进行删除,修改或者增加,将第一个字符串改成第二个字符串,求最小的操作数a) Insert a characterb) Delete a characterc) Replace a character第一字符串长度为m, 长度为n;方法:可见要求DP[i+1][j+1],必须要知道二维矩阵中...原创 2018-09-22 11:45:22 · 149 阅读 · 0 评论 -
字符串匹配算法
基础问题leetcode 28. Implement strStr()类似于c++中的strstr()和Java中的indexOf()Java中字符串中子串的查找共有四种方法1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次...原创 2018-09-22 15:43:49 · 136 阅读 · 0 评论 -
235. Lowest Common Ancestor of a Binary Search Tree
二叉搜索树节点的公共祖先还是老套路,将树分为三部分代码class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { //二分查找方法,首先是根节点处判断,两个节点是在根节点的两边,还是同样在左边,还是右边的情况 if(roo...原创 2018-10-17 20:59:02 · 195 阅读 · 0 评论 -
Leetcode解题技巧总结
递归的出口和和参数不满足条件参数不满函数处理的条件,直接返回错误代码判断出口条件优先级队列//使用PriorityQueue实现大顶堆//PriorityQueue默认是一个小顶堆,然而可以通过传入自定义的Comparator函数来实现大顶堆。如下代码:private static final int DEFAULT_INITIAL_CAPACITY = 11;Priority...原创 2018-10-15 23:58:31 · 2556 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
查找缺失的数据相似的题目查看如下链接的基本情况448 查找缺失的数据442. Find All Duplicates in an Array先解决查找数组当中相同的元素这道题目是442的,如何查找出数组当中出现多次的元素, 这就是桶排序算法数组当中的每个元素大小都是1<<x<<n ,只要注意这两个地方即可桶排序,归位处理遍历一遍, 将没有归位处理的元素进...原创 2018-12-18 15:34:45 · 160 阅读 · 0 评论 -
全排列、子集合subset、目标和combation、树的路径和问题
主要的方法深度优先搜索,回溯算法宽度优先搜索是否有相同元素需要考虑等问题针对所给问题,确定问题的解空间:首先应明确定义问题的解空间,问题的解空间应至少包含问题的一个(最优)解。确定结点的扩展搜索范围 for等一系列循环等问题以深度优先方式搜索解空间,并在搜索过程中用剪枝函数避免无效搜索。 判断减支情况int a[n]; try(int i){...原创 2019-03-03 16:33:47 · 364 阅读 · 0 评论 -
LeetCode之最值问题系列问题求解
最系列题目解析这类型题目的特点就是一个数组,或者字符串, 给的条件是连续或者不连续,解题的关键采用两个变量一个变量记录前面的条件,或者最后一个不满足题意的index,或者最小值, 比如股票题目当中j ,或者最大不重复字符串一个变量代表截止到处理到该位置处的最大结果值动态规划保存前面的计算结果利益最大买卖股票只能进行一次买卖的基本情况class Solution { ...原创 2019-03-03 21:50:59 · 365 阅读 · 0 评论 -
树的四种遍历方式
树的中序遍历非递归思路使用stack替换掉递归while(两种情况) (1)一直走到树的最左边结点,把左边的结点全部压入stack, (2)走完左边的结点后,出stack, 继判断是否最左边的结点是否有右结点 如果有右结点,则对这个子树执行(1)中同样的操作,回到步骤一 2.出stack的同时访问结点代码(Java) // Def...原创 2018-07-29 11:48:55 · 21905 阅读 · 2 评论 -
Ugly Number II
丑数的定义: 因素分解后因子只包含2,3,5(由这三个数的组成,不一定要三个都用上,也可以使用多个相同的情况)的数 比如 10= 2*5, 给定一个数,找到第n个丑数比如输入10,找到第10个丑数, Input: n = 10 Output: 12 Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the ...原创 2018-09-07 15:09:41 · 107 阅读 · 0 评论 -
Leetcode95. Unique Binary Search Trees II and I
分析这道题目采用二分法和递归方法·解决, 1&amp;lt;=i&amp;lt;=n, i代表root, [1,i-1] 为有节点 [i, n] 为右节点,然后左右两边又不断分治的方法解决 分而治之用于归并排序类似于这种方法就是 代码书写流程递归递归判断条件,递归的出口文件对原始问题进行处理,处理左边,处理右边合并两边结果从宏观上把握这种情况归并排序对算法: ...原创 2018-09-04 17:33:50 · 339 阅读 · 0 评论 -
leetcode解题300.最长递增序列长度
题目连接解题思路一维动态规划,两次循环dp[i] = max{dp[j]+1} if(nums[j]import java.util.Arrays;public class LongestIncreasingSubsequence { public int solution(int [] nums) { int n = nums.length; ...原创 2018-07-10 23:32:04 · 154 阅读 · 0 评论 -
leetcode 322. Coin Change硬币交换问题
题目详细描述You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amou...原创 2018-07-11 23:03:26 · 650 阅读 · 0 评论 -
Leetcode 460. LFU Cache
题目链接题目描述Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) ...原创 2018-07-14 21:06:18 · 320 阅读 · 0 评论 -
找出数组中重复数字
描述查找数组中的重复元素情况,时间复杂度为o(n), 空间复杂度为o(1), 数组的大小为n, 数组元素值大小为0到n-1, 比如 n=4,[2,3,,1,2,-3]思路一采用记录的思路访问 ,如果array[i]代表一个位置,如果array[array[i]]&gt;=0代表是第一次访问,让起取负,做一个标志,如果array[array[i]]&lt;0 代表array[i] 这...原创 2018-07-20 14:52:14 · 413 阅读 · 0 评论 -
LeetCode 62. Unique Paths
题目链接题目描述A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying t...原创 2018-07-15 13:26:48 · 90 阅读 · 0 评论 -
Leetcode347. Top K Frequent Elements
思路一hashmap + maxheaphashmap统计频率,maxheap从中选择k个元素,代码(java)public class Solution { public List&amp;lt;Integer&amp;gt; topKFrequent(int[] nums, int k) { Map&amp;lt;Integer, Integer&amp;gt; map = ne...原创 2018-07-26 11:49:01 · 136 阅读 · 0 评论 -
LeetCode 63. Unique Paths II
题目描述A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach ...原创 2018-07-15 17:07:07 · 128 阅读 · 0 评论 -
Leetcode 122:买卖股票二
题目描述https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/与第一次的题目相比,可以多次买卖等情况,只要求出最大利润即可, 可以将一次买卖分为过个过程即可; [1,2,3,4,5] 比如正常情况下是第一天买进,第五天卖出,但这个过程可以分解为多次买进卖出 在第一天买进,第二天卖出,获得利...原创 2018-08-31 14:57:59 · 165 阅读 · 0 评论 -
Leetcode 121. Best Time to Buy and Sell Stock
题目描述一个一维数组代表着股票的价值,可以执行两个操作,一个是买操作,一个是卖操作,如何能让利润最大化, 说白了就是如何让差价最大话,在卖股票之前必须进行股票的购买操作,每个操作只最多只能执行一次。 Example 1:Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on d...原创 2018-08-24 14:52:38 · 109 阅读 · 0 评论 -
Leetcode解题
3.Longest Substring Without Repeating Characters [题目连接]https://leetcode.com/problems/longest-substring-without-repeating-characters/hints/ - 解题思路 - 代码块高亮 - 图片链接和图片上传 - LaTex数学公式 - UML序列图和流程...原创 2018-07-09 21:48:28 · 335 阅读 · 0 评论