Leetcode-cn.com 2021-09-27
🔴 639.decode-ways-ii
🏷️ Tags
#string #dynamic_programming
Description
一条包含字母
要 解码 一条已编码的消息,所有的数字都必须分组,然后按原来的编码方案反向映射回字母(可能存在多种方式)。例如,
注意,像
除了 上面描述的数字字母映射方案,编码消息中可能包含
给你一个字符串
由于答案数目可能非常大,返回对
Example
🔴 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
Return the minimum number of moves to transform the board into a chessboard board. If the task is impossible, return
A chessboard board is a board where no
Example
🔴 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
For example, in
If you add periods
For example,
If you add a plus
For example,
It is possible to use both of these rules at the same time.
Given an array of strings
Example
🟢 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
假设有
在每一步操作中,你可以选择任意
给定一个整数数组
Example
🔴 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
Sort the array so that whenever
Return any answer array that satisfies this condition.
Example
🟢 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
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
Example
🟡 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
给你一份旅游线路图,该线路图中的旅行线路用数组
题目数据保证线路图会形成一条不存在循环的线路,因此恰有一个旅行终点站。
Example
🟢 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
Example
🟡 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
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
注意:
十六进制中所有字母(
十六进制字符串中不能包含多余的前导零。如果要转化的数为0,那么以单个字符
给定的数确保在32位有符号整数范围内。
不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。
undefined
🟢 405.convert-a-number-to-hexadecimal
🏷️ Tags
#bit_manipulation #math
Description
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
注意:
十六进制中所有字母(
a-f)都必须是小写。十六进制字符串中不能包含多余的前导零。如果要转化的数为0,那么以单个字符
'0'来表示;对于其他情况,十六进制字符串中的第一个字符将不会是0字符。 给定的数确保在32位有符号整数范围内。
不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。
undefined
百度百科
补码_百度百科
计算机中的有符号数有三种表示方法,即原码、反码和补码。三种表示方法均有符号位和数值位两部分,符号位都是用0表示“正”,用1表示“负”,而数值位,三种表示方法各不相同。在计算机系统中,数值一律用补码来表示和存储。原因在于,使用补码,可以将符号位和数值域统一处理;同时,加法和减法也可以统一处理。
Leetcode-cn.com 2021-10-04
🟢 482.license-key-formatting
🏷️ Tags
#string
Description
有一个密钥字符串 S ,只包含字母,数字以及 '-'(破折号)。其中, N 个 '-' 将字符串分成了 N+1 组。
给你一个数字 K,请你重新格式化字符串,使每个分组恰好包含 K 个字符。特别地,第一个分组包含的字符个数必须小于等于 K,但至少要包含 1 个字符。两个分组之间需要用 '-'(破折号)隔开,并且将所有的小写字母转换为大写字母。
给定非空字符串 S 和数字 K,按照上面描述的规则进行格式化。
Example
🟢 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
🟢 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
🟢 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 都由一系列缩写为
编写一个函数来找出所有目标子串,目标子串的长度为 10,且在 DNA 字符串
Example
🟡 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
你总共有
给你一个数字
Example
🟢 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
将非负整数
Example
🔴 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
给定两个整数,被除数
返回被除数
整数除法的结果应当截去(
Example
🟡 29.divide-two-integers
🏷️ Tags
#bit_manipulation #math
Description
给定两个整数,被除数
dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。返回被除数
dividend 除以除数 divisor 得到的商。整数除法的结果应当截去(
truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2Example
输入: 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的倍数,输出“Fizz”;
2. 如果 n 是5的倍数,输出“Buzz”;
3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。
undefined
🟢 412.fizz-buzz
🏷️ Tags
#math #string #simulation
Description
写一个程序,输出从 1 到 n 数字的字符串表示。
1. 如果 n 是3的倍数,输出“Fizz”;
2. 如果 n 是5的倍数,输出“Buzz”;
3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。
undefined
Leetcode-cn.com 2021-10-14
🟢 剑指 Offer II 069.B1IidL
🏷️ Tags
#array #binary_search
Description
符合下列属性的数组
存在
给定由整数组成的山峰数组
Example
🟢 剑指 Offer II 069.B1IidL
🏷️ Tags
#array #binary_search
Description
符合下列属性的数组
arr 称为 山峰数组(山脉数组) :arr.length >= 3存在
i(0 < i < arr.length - 1)使得:arr[0] < arr[1] < ... arr[i-1] < arr[i] arr[i] > arr[i+1] > ... > arr[arr.length - 1]给定由整数组成的山峰数组
arr ,返回任何满足 arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1] 的下标 i ,即山峰顶部。Example
输入:arr = [0,1,0]
输出:1
Leetcode-cn.com 2021-10-15
🟡 38.count-and-say
🏷️ Tags
#string
Description
给定一个正整数
「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。
你可以将其视作是由递归公式定义的数字字符串序列:
前五项如下:
要 描述 一个数字字符串,首先要将字符串分割为 最小 数量的组,每个组都由连续的最多 相同字符 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。
例如,数字字符串
Example
🟡 38.count-and-say
🏷️ Tags
#string
Description
给定一个正整数
n ,输出外观数列的第 n 项。「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。
你可以将其视作是由递归公式定义的数字字符串序列:
countAndSay(1) = "1"countAndSay(n) 是对 countAndSay(n-1) 的描述,然后转换成另一个数字字符串。前五项如下:
1. 1
2. 11
3. 21
4. 1211
5. 111221
第一项是数字 1
描述前一项,这个数是 1 即 “ 一 个 1 ”,记作 "11"
描述前一项,这个数是 11 即 “ 二 个 1 ” ,记作 "21"
描述前一项,这个数是 21 即 “ 一 个 2 + 一 个 1 ” ,记作 "1211"
描述前一项,这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 "111221"
要 描述 一个数字字符串,首先要将字符串分割为 最小 数量的组,每个组都由连续的最多 相同字符 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。
例如,数字字符串
"3322251" 的描述如下图:Example
输入:n = 1
输出:"1"
解释:这是一个基本样例。
Leetcode-cn.com 2021-10-16
🔴 282.expression-add-operators
🏷️ Tags
#math #string #backtracking
Description
给定一个仅包含数字
Example
🔴 282.expression-add-operators
🏷️ Tags
#math #string #backtracking
Description
给定一个仅包含数字
0-9 的字符串 num 和一个目标值整数 target ,在 num 的数字之间添加 二元 运算符(不是一元)+、- 或 * ,返回所有能够得到目标值的表达式。Example
输入: num = "123", target = 6
输出: ["1+2+3", "1*2*3"]