Leetcode Daily Question
2.45K subscribers
517 files
2.16K links
Why are you asking me to do Leetcode for this CSS job?
Download Telegram
Leetcode-cn.com 2021-09-25
🟡 583.delete-operation-for-two-strings

🏷️ Tags
#string #dynamic_programming

Description
给定两个单词 word1 word2,找到使得 word1 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。

Example
输入: "sea", "eat"
输出: 2
解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"
Leetcode.com 2021-09-24
🟢 1137.n-th-tribonacci-number

🏷️ Tags
#memoization #math #dynamic_programming

Description
The Tribonacci sequence Tn is defined as follows:

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given n, return the value of Tn.

Example
Input: n = 4
Output: 4
Explanation:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
Leetcode-cn.com 2021-09-26
🟡 371.sum-of-two-integers

🏷️ Tags
#bit_manipulation #math

Description
给你两个整数 ab ,不使用 运算符 +- ​​​​​​​,计算并返回两整数之和。

Example
输入:a = 1, b = 2
输出:3
Leetcode.com 2021-09-25
🔴 1293.shortest-path-in-a-grid-with-obstacles-elimination

🏷️ Tags
#breadth_first_search #array #matrix

Description
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.

Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.

Example
Input: 
grid =
[[0,0,0],
[1,1,0],
[0,0,0],
[0,1,1],
[0,0,0]],
k = 1
Output: 6
Explanation:
The shortest path without eliminating any obstacle is 10.
The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
Leetcode-cn.com 2021-09-27
🔴 639.decode-ways-ii

🏷️ Tags
#string #dynamic_programming

Description
一条包含字母 A-Z 的消息通过以下的方式进行了编码:


'A' -> 1
'B' -> 2
...
'Z' -> 26


要 解码 一条已编码的消息,所有的数字都必须分组,然后按原来的编码方案反向映射回字母(可能存在多种方式)。例如,"11106" 可以映射为:


"AAJF" 对应分组 (1 1 10 6)
"KJF" 对应分组 (11 10 6)


注意,像 (1 11 06) 这样的分组是无效的,因为 "06" 不可以映射为 'F' ,因为 "6""06" 不同。

除了 上面描述的数字字母映射方案,编码消息中可能包含 '*' 字符,可以表示从 '1''9' 的任一数字(不包括 '0')。例如,编码字符串 "1*" 可以表示 "11""12""13""14""15""16""17""18""19" 中的任意一条消息。对 "1*" 进行解码,相当于解码该字符串可以表示的任何编码消息。

给你一个字符串 s ,由数字和 '*' 字符组成,返回 解码 该字符串的方法 数目 。

由于答案数目可能非常大,返回对 109 + 7 取余 的结果。

Example
输入:s = "*"
输出:9
解释:这一条编码消息可以表示 "1"、"2"、"3"、"4"、"5"、"6"、"7"、"8" 或 "9" 中的任意一条。
可以分别解码成字符串 "A"、"B"、"C"、"D"、"E"、"F"、"G"、"H" 和 "I" 。
因此,"*" 总共有 9 种解码方法。
Leetcode.com 2021-09-26
🔴 782.transform-to-chessboard

🏷️ Tags
#bit_manipulation #array #math #matrix

Description
You are given an n x n binary grid board. In each move, you can swap any two rows with each other, or any two columns with each other.

Return the minimum number of moves to transform the board into a chessboard board. If the task is impossible, return -1.

A chessboard board is a board where no 0's and no 1's are 4-directionally adjacent.

Example
Input: board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
Output: 2
Explanation: One potential sequence of moves is shown.
The first move swaps the first and second column.
The second move swaps the second and third row.
Leetcode.com 2021-09-27
🟢 929.unique-email-addresses

🏷️ Tags
#array #hash_table #string

Description
Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.


For example, in "alice@leetcode.com", "alice" is the local name, and "leetcode.com" is the domain name.


If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.


For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.


If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.


For example, "m.y+name@email.com" will be forwarded to "my@email.com".


It is possible to use both of these rules at the same time.

Given an array of strings emails where we send one email to each email[i], return the number of different addresses that actually receive mails.

Example
Input: emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails.
Leetcode-cn.com 2021-09-29
🔴 517.super-washing-machines

🏷️ Tags
#greedy #array

Description
假设有 n 台超级洗衣机放在同一排上。开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的。

在每一步操作中,你可以选择任意 m (1 <= m <= n) 台洗衣机,与此同时将每台洗衣机的一件衣服送到相邻的一台洗衣机。

给定一个整数数组 machines 代表从左至右每台洗衣机中的衣物数量,请给出能让所有洗衣机中剩下的衣物的数量相等的 最少的操作步数 。如果不能使每台洗衣机中衣物的数量相等,则返回 -1

Example
输入:machines = [1,0,5]
输出:3
解释:
第一步: 1 0 <-- 5 => 1 1 4
第二步: 1 <-- 1 <-- 4 => 2 1 3
第三步: 2 1 <-- 3 => 2 2 2
Leetcode.com 2021-09-28
🟢 922.sort-array-by-parity-ii

🏷️ Tags
#array #two_pointers #sorting

Description
Given an array of integers nums, half of the integers in nums are odd, and the other half are even.

Sort the array so that whenever nums[i] is odd, i is odd, and whenever nums[i] is even, i is even.

Return any answer array that satisfies this condition.

Example
Input: nums = [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.
Leetcode.com 2021-09-29
🟡 725.split-linked-list-in-parts

🏷️ Tags
#linked_list

Description
Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts.

The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.

The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.

Return an array of the k parts.

Example
Input: head = [1,2,3], k = 5
Output: [[1],[2],[3],[],[]]
Explanation:
The first element output[0] has output[0].val = 1, output[0].next = null.
The last element output[4] is null, but its string representation as a ListNode is [].
Leetcode-cn.com 2021-10-01
🟢 1436.destination-city

🏷️ Tags
#hash_table #string

Description
给你一份旅游线路图,该线路图中的旅行线路用数组 paths 表示,其中 paths[i] = [cityAi, cityBi] 表示该线路将会从 cityAi 直接前往 cityBi 。请你找出这次旅行的终点站,即没有任何可以通往其他城市的线路的城市

题目数据保证线路图会形成一条不存在循环的线路,因此恰有一个旅行终点站。

Example
输入:paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
输出:"Sao Paulo"
解释:从 "London" 出发,最后抵达终点站 "Sao Paulo" 。本次旅行的路线是 "London" -> "New York" -> "Lima" -> "Sao Paulo" 。
Leetcode.com 2021-09-30
🟡 698.partition-to-k-equal-sum-subsets

🏷️ Tags
#bit_manipulation #memoization #array #dynamic_programming #backtracking #bitmask

Description
Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.

Example
Input: nums = [4,3,2,3,5,2,1], k = 4
Output: true
Explanation: It's possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
Leetcode-cn.com 2021-10-02
🟢 405.convert-a-number-to-hexadecimal

🏷️ Tags
#bit_manipulation #math

Description
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。

注意:


十六进制中所有字母(a-f)都必须是小写。
十六进制字符串中不能包含多余的前导零。如果要转化的数为0,那么以单个字符'0'来表示;对于其他情况,十六进制字符串中的第一个字符将不会是0字符。
给定的数确保在32位有符号整数范围内。
不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。


undefined
Leetcode-cn.com 2021-10-04
🟢 482.license-key-formatting

🏷️ Tags
#string

Description
有一个密钥字符串 S ,只包含字母,数字以及 '-'(破折号)。其中, N 个 '-' 将字符串分成了 N+1 组。

给你一个数字 K,请你重新格式化字符串,使每个分组恰好包含 K 个字符。特别地,第一个分组包含的字符个数必须小于等于 K,但至少要包含 1 个字符。两个分组之间需要用 '-'(破折号)隔开,并且将所有的小写字母转换为大写字母。

给定非空字符串 S 和数字 K,按照上面描述的规则进行格式化。

Example
输入:S = "5F3Z-2e-9-w", K = 4
输出:"5F3Z-2E9W"
解释:字符串 S 被分成了两个部分,每部分 4 个字符;
注意,两个额外的破折号需要删掉。
Leetcode-cn.com 2021-10-06
🟢 414.third-maximum-number

🏷️ Tags
#array #sorting

Description
给你一个非空数组,返回此数组中 第三大的数 。如果不存在,则返回数组中最大的数。

 

Example
输入:[3, 2, 1]
输出:1
解释:第三大的数是 1 。
Leetcode-cn.com 2021-10-07
🟢 434.number-of-segments-in-a-string

🏷️ Tags
#string

Description
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。

请注意,你可以假定字符串里不包括任何不可打印的字符。

Example
输入: "Hello, my name is John"
输出: 5
解释: 这里的单词是指连续的不是空格的字符,所以 "Hello," 算作 1 个单词。
Leetcode-cn.com 2021-10-08
🟡 187.repeated-dna-sequences

🏷️ Tags
#bit_manipulation #hash_table #string #sliding_window #hash_function #rolling_hash

Description
所有 DNA 都由一系列缩写为 'A''C''G''T' 的核苷酸组成,例如:"ACGAATTCCG"。在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助。

编写一个函数来找出所有目标子串,目标子串的长度为 10,且在 DNA 字符串 s 中出现次数超过一次。

 

Example
输入:s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
输出:["AAAAACCCCC","CCCCCAAAAA"]
Leetcode-cn.com 2021-10-10
🟢 441.arranging-coins

🏷️ Tags
#math #binary_search

Description
你总共有 n 枚硬币,并计划将它们按阶梯状排列。对于一个由 k 行组成的阶梯,其第 i 行必须正好有 i 枚硬币。阶梯的最后一行 可能 是不完整的。

给你一个数字 n ,计算并返回可形成 完整阶梯行 的总行数。

Example
输入:n = 5
输出:2
解释:因为第三行不完整,所以返回 2 。
Leetcode-cn.com 2021-10-11
🔴 273.integer-to-english-words

🏷️ Tags
#recursion #math #string

Description
将非负整数 num 转换为其对应的英文表示。

 

Example
输入:num = 123
输出:"One Hundred Twenty Three"
Leetcode-cn.com 2021-10-12
🟡 29.divide-two-integers

🏷️ Tags
#bit_manipulation #math

Description
给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。

返回被除数 dividend 除以除数 divisor 得到的商。

整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2

Example
输入: dividend = 10, divisor = 3
输出: 3
解释: 10/3 = truncate(3.33333..) = truncate(3) = 3
Leetcode-cn.com 2021-10-13
🟢 412.fizz-buzz

🏷️ Tags
#math #string #simulation

Description
写一个程序,输出从 1 到 n 数字的字符串表示。

1. 如果 n 是3的倍数,输出&ldquo;Fizz&rdquo;;

2. 如果 n 是5的倍数,输出&ldquo;Buzz&rdquo;;

3.如果 n 同时是3和5的倍数,输出 &ldquo;FizzBuzz&rdquo;。

undefined