leetcode

LeetCode: Distinct Subsequence

对着背影说爱祢 提交于 2019-11-30 16:16:58
问题描述: <pre> Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not). Here is an example: S = "rabbbit", T = "rabbit" Return 3. </pre> 问题反复读了几遍感觉才理解。首先想到的是用递归的方法试试: public class DistinctSubsequences { private int numDS = 0; public int numDistinct(String S, String T) { numDS = 0; searchSequence(S, T); return

Leetcode 297. Serialize and Deserialize Binary Tree

北城以北 提交于 2019-11-30 15:45:42
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to

LeetCode-动态规划

一个人想着一个人 提交于 2019-11-30 15:14:53
72. Edit Distance 最短编辑距离 https://leetcode.com/problems/edit-distance/ 题目:给定两个单词Word 1和Word2,找到将word 1转换为Word2所需的最小操作数。允许对一个单词执行以下3种操作:插入字符、删除字符、替换字符。 思路: class Solution { public int minDistance(String word1, String word2) { int a = word1.length(); int b = word2.length(); int[][] dp = new int[b+1][a+1]; dp[0][0] = 0; for(int i = 1; i < a+1; i++) { dp[0][i] = i; } for(int i = 1; i < b+1; i++) { dp[i][0] = i; } for(int j = 1; j < b+1; j++) { for(int i = 1; i < a+1; i++) { if(word1.charAt(i-1) == word2.charAt(j-1)){ dp[j][i] = dp[j-1][i-1]; } else { int temp = Math.min(dp[j-1][i], dp[j-1][i-1]);

Baozi Leetcode Solution 199: Binary Tree Right Side View

半城伤御伤魂 提交于 2019-11-30 14:23:24
Problem Statement Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. Example: Input: [1,2,3,null,5,null,4] Output: [1, 3, 4] Explanation: 1 <--- / \ 2 3 <--- \ \ 5 4 <--- Problem link Video Tutorial You can find the detailed video tutorial here Youtube B站 Thought Process This is a great problem and I highly recommend we solve it in both DFS and BFS for level order traversal because it covers everything we need to know about trees in interviews. This would be a better test case because right subtree

LeetCode.1189-balloon实例数最大值(Maximum Number of Balloons)

我与影子孤独终老i 提交于 2019-11-30 14:23:07
这是小川的第 416 次更新,第 449 篇原创 看题和准备 今天介绍的是 LeetCode 算法题中 Easy 级别的第 267 题(顺位题号是 1189 )。给定一个字符串文本,使用文本字符来构成单词 "balloon" 的尽可能多的实例。每个字符最多可以在文本中使用一次。返回可以形成的最大实例数。 例如: 输入:text = "nlaebolko" 输出:1 输入:text = "loonbalxballpoon" 输出:2 输入:text = "leetcode" 输出:0 约束 : 1 <= text.length <= 10^4 文本字符串仅包含小写英文字母。 第一种解法 题目的意思是要在一个给定的字符串中,找到能够组成字符串 "balloon" 的最大字符对数,本质上和木桶装水的容量由短板决定类似。 直接遍历text字符串中的字符,对字母 a 、 b 、 l 、 n 、 o 的出现次数计数,因为l和o是需要两个,在计数完后,需要对 l 和 o 的次数除2,然后比较5个字母出现次数的最小值,因为只有出现次数最小的那个字母才能最终决定组成多少个 "balloon" 。 public int maxNumberOfBalloons(String text) { if (text == "" || text.length() < 7) { return 0; } int A

[LeetCode] 829. Consecutive Numbers Sum 连续数字之和

北城余情 提交于 2019-11-30 13:38:59
Given a positive integer N , how many ways can we write it as a sum of consecutive positive integers? Example 1: Input: 5 Output: 2 Explanation: 5 = 5 = 2 + 3 Example 2: Input: 9 Output: 3 Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4 Example 3: Input: 15 Output: 4 Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5 Note: 1 <= N <= 10 ^ 9 . Github 同步地址: https://github.com/grandyang/leetcode/issues/829 参考资料: https://leetcode.com/problems/consecutive-numbers-sum/ LeetCode All in One 题目讲解汇总(持续更新中...) 来源: https://www.cnblogs.com/grandyang/p/11595236.html

LeetCode第四题-寻找两个有序数组的中位数

馋奶兔 提交于 2019-11-30 12:22:36
题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 你可以假设 nums1 和 nums2 不会同时为空。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 示例 示例 1: nums1 = [1, 3] nums2 = [2] 则中位数是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 则中位数是 (2 + 3)/2 = 2.5 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路 本菜鸡拿到这道题第一想法仍然是暴力破解,没办法菜鸡就是菜鸡,但是忽然看到题目限定了时间复杂度 O(log(m + n)),然而暴力破解需要将两个数组进行排序,然后找到中位数,这样一来时间复杂度就成了O(m+n),这与要求是不符的,接下来看一下官方是在限定的时间复杂度内怎么解决这道题的方法的思路: 方法

[LeetCode] 811. Subdomain Visit Count

落花浮王杯 提交于 2019-11-30 10:30:35
Easy A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com", and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly. Now, call a "count-paired domain" to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com". We are given a list cpdomains of count

LeetCode-堆栈的使用

末鹿安然 提交于 2019-11-30 09:40:06
20. Valid Parentheses 有效的配对 https://leetcode.com/problems/valid-parentheses/ 题目:如果一个字符串只包含‘(’,‘)、’{‘、’}‘、’[‘和’]‘,则确定输入字符串是否有效。打开括号必须由相同类型的括号关闭,则输入字符串是有效的。开括号必须按照正确的顺序关闭。注意,空字符串也被认为是有效的。 思路:新建一个栈,遍历输入字符串,如果当前字符为左半边括号,则将其压入栈中;如果当前字符为右半边括号且栈为空,则直接返回false;如果当前字符为右半边括号且栈不为空,则取出栈顶元素,若为对应的左半边括号,则继续循环,反之返回false。 32. Longest Valid Parentheses 最长有效配对 https://leetcode.com/problems/longest-valid-parentheses/ 题目:给定一个只包含字符‘(’和‘)’的字符串,查找最长的有效(格式良好)括号子字符串的长度。 思路: ①使用Stack栈 ②DP动态规划 class Solution { public int longestValidParentheses(String s) { // using Stack Stack<Integer> st = new Stack<>(); int result = 0;

本周学习小结(23/09 - 29/09)

走远了吗. 提交于 2019-11-30 07:25:18
LeetCode 本周有进展。需要及时复习。 学习笔记之LeetCode - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/5528520.html Explore - LeetCode - Sorting and Searching (DONE) https://leetcode.com/explore/interview/card/top-interview-questions-medium/110/sorting-and-searching/ Explore - LeetCode - Dynamic Programming (DONE) https://leetcode.com/explore/interview/card/top-interview-questions-medium/111/dynamic-programming/ Explore - LeetCode - Design https://leetcode.com/explore/interview/card/top-interview-questions-medium/112/design/ Explore - LeetCode - Math https://leetcode.com/explore/interview/card/top-interview