Leetcode 2021-04-09
154.find-minimum-in-rotated-sorted-array-ii
154.find-minimum-in-rotated-sorted-array-ii
力扣 LeetCode
154. 寻找旋转排序数组中的最小值 II - 力扣(LeetCode)
已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组。例如,原数组 nums = [0,1,4,4,5,6,7] 在变化后可能得到: 若旋转 4 次,则可以得到 [4,5,6,7,0,1,4] 若旋转 7 次,则可以得到 [0,1,4,4,5,6,7] 注意,数组 [a[0], a[1], a[2], ..., a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], ..., a[n-2]] 。 给你一个可能存在 重复 元素值的数组…
Leetcode 2021-04-10
263.ugly-number
263.ugly-number
力扣 LeetCode
263. 丑数 - 力扣(LeetCode)
编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 输出: true 解释: 8 = 2 × 2 × 2 示例 3: 输入: 14 输出: false 解释: 14 不是丑数,因为它包含了另外一个质因数 7。 说明: 1 是丑数。 输入不会超过 32 位有符号整数的范围: [−231, 231 − 1]。。263. Ugly Number: Given an integer n…
Leetcode 2021-04-11
264.ugly-number-ii
264.ugly-number-ii
力扣 LeetCode
264. 丑数 II - 力扣(LeetCode)
编写一个程序,找出第 n 个丑数。 丑数就是质因数只包含 2, 3, 5 的正整数。 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。 说明: 1 是丑数。 n 不超过1690。。264. Ugly Number II: Given an integer n, return the nth ugly number. Ugly number is a positive number whose prime factors…
Leetcode 2021-04-12
179.largest-number
179.largest-number
力扣 LeetCode
179. 最大数 - 力扣(LeetCode)
给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。 注意:输出结果可能非常大,所以你需要返回一个字符串而不是整数。 示例 1: 输入:nums = [10,2] 输出:"210" 示例 2: 输入:nums = [3,30,34,5,9] 输出:"9534330" 示例 3: 输入:nums = [1] 输出:"1" 示例 4: 输入:nums = [10] 输出:"10" 提示: 1 <= nums.length <= 100 0 <= nums[i] <= 109。179.…
Leetcode 2021-04-13
🟢 783.minimum-distance-between-bst-nodes
#tree #depth-first-search #recursion
Given the
🟢 783.minimum-distance-between-bst-nodes
#tree #depth-first-search #recursion
Given the
root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree.
Input: root = [4,2,6,1,3]
Output: 1
Leetcode 2021-04-14
🟡 208.implement-trie-prefix-tree
#design #trie
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
🟡 208.implement-trie-prefix-tree
#design #trie
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.
Input
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
Output
[null, null, true, false, true, null, true]
Explanation
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // return True
trie.search("app"); // return False
trie.startsWith("app"); // return True
trie.insert("app");
trie.search("app"); // return True
Leetcode 2021-04-15
🟡 213.house-robber-ii
#dynamic-programming
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
🟡 213.house-robber-ii
#dynamic-programming
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
Input: nums = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
Leetcode 2021-04-16
🔴 87.scramble-string
#string #dynamic-programming
We can scramble a string s to get a string t using the following algorithm:
🔴 87.scramble-string
#string #dynamic-programming
We can scramble a string s to get a string t using the following algorithm:
Input: s1 = "great", s2 = "rgeat"
Output: true
Explanation: One possible scenario applied on s1 is:
"great" --> "gr/eat" // divide at random index.
"gr/eat" --> "gr/eat" // random decision is not to swap the two substrings and keep them in order.
"gr/eat" --> "g/r / e/at" // apply the same algorithm recursively on both substrings. divide at ranom index each of them.
"g/r / e/at" --> "r/g / e/at" // random decision was to swap the first substring and to keep the second substring in the same order.
"r/g / e/at" --> "r/g / e/ a/t" // again apply the algorithm recursively, divide "at" to "a/t".
"r/g / e/ a/t" --> "r/g / e/ a/t" // random decision is to keep both substrings in the same order.
The algorithm stops now and the result string is "rgeat" which is s2.
As there is one possible scenario that led s1 to be scrambled to s2, we return true.
Leetcode 2021-04-18
🟢 26.remove-duplicates-from-sorted-array
#array #two-pointers
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.
🟢 26.remove-duplicates-from-sorted-array
#array #two-pointers
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
Leetcode 2021-04-19
🟢 27.remove-element
#array #two-pointers
Given an array nums and a value
🟢 27.remove-element
#array #two-pointers
Given an array nums and a value
val, remove all instances of that value in-place and return the new length.
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
Leetcode 2021-04-19
🟢 27.remove-element
1️⃣ Tags
#array #two-pointers
2️⃣ Description
Given an array nums and a value
Do not allocate extra space for another array, you must do this by modifying the input array in-place with
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller as well.
Internally you can think of this:
3️⃣ Example
🟢 27.remove-element
1️⃣ Tags
#array #two-pointers
2️⃣ Description
Given an array nums and a value
val, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place with
O(1) extra memory.The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
3️⃣ Example
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
Leetcode 2021-04-20
🟢 28.implement-strstr
1️⃣ Tags
#two-pointers #string
2️⃣ Description
Implement strStr().
Return the index of the first occurrence of needle in haystack, or
Clarification:
What should we return when
For the purpose of this problem, we will return 0 when
3️⃣ Example
🟢 28.implement-strstr
1️⃣ Tags
#two-pointers #string
2️⃣ Description
Implement strStr().
Return the index of the first occurrence of needle in haystack, or
-1 if needle is not part of haystack.Clarification:
What should we return when
needle is an empty string? This is a great question to ask during an interview.For the purpose of this problem, we will return 0 when
needle is an empty string. This is consistent to C's strstr() and Java's indexOf().3️⃣ Example
Input: haystack = "hello", needle = "ll"
Output: 2
Leetcode Daily Question
Leetcode 2021-04-20 🟢 28.implement-strstr 1️⃣ Tags #two-pointers #string 2️⃣ Description Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Clarification: What should we return…
YouTube
Knuth–Morris–Pratt (KMP) Pattern Matching Substring Search - First Occurrence Of Substring
Free 5-Day Mini-Course: https://backtobackswe.com
Try Our Full Platform: https://backtobackswe.com/pricing
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: Given a string…
Try Our Full Platform: https://backtobackswe.com/pricing
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions
Question: Given a string…
Leetcode 2021-04-21
🟡 91.decode-ways
1️⃣ Tags
#string #dynamic-programming
2️⃣ Description
A message containing letters from
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example,
Note that the grouping
Given a string
The answer is guaranteed to fit in a 32-bit integer.
3️⃣ Example
🟡 91.decode-ways
1️⃣ Tags
#string #dynamic-programming
2️⃣ Description
A message containing letters from
A-Z can be encoded into numbers using the following mapping:
'A' -> "1"
'B' -> "2"
...
'Z' -> "26"
To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example,
"11106" can be mapped into:"AAJF" with the grouping (1 1 10 6)"KJF" with the grouping (11 10 6)Note that the grouping
(1 11 06) is invalid because "06" cannot be mapped into 'F' since "6" is different from "06".Given a string
s containing only digits, return the number of ways to decode it.The answer is guaranteed to fit in a 32-bit integer.
3️⃣ Example
Input: s = "12"
Output: 2
Explanation: "12" could be decoded as "AB" (1 2) or "L" (12).