-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
396 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# [0804. 唯一摩尔斯密码词](https://leetcode.cn/problems/unique-morse-code-words/) | ||
|
||
- 标签:数组、哈希表、字符串 | ||
- 难度:简单 | ||
|
||
## 题目链接 | ||
|
||
- [0804. 唯一摩尔斯密码词 - 力扣](https://leetcode.cn/problems/unique-morse-code-words/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: | ||
|
||
- `'a'` 对应 `".-"`, | ||
- `'b'` 对应 `"-..."`, | ||
- `'c'` 对应 `"-.-."` ,以此类推。 | ||
|
||
为了方便,所有 $26$ 个英文字母的摩尔斯密码表如下: | ||
|
||
`[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]` | ||
|
||
给定一个字符串数组 $words$,每个单词可以写成每个字母对应摩尔斯密码的组合。 | ||
|
||
- 例如,`"cab"` 可以写成 `"-.-..--..."` ,(即 `"-.-."` + `".-"` + `"-..."` 字符串的结合)。我们将这样一个连接过程称作单词翻译。 | ||
|
||
**要求**:对 $words$ 中所有单词进行单词翻译,返回不同单词翻译的数量。 | ||
|
||
**说明**: | ||
|
||
- $1 \le words.length \le 100$。 | ||
- $1 \le words[i].length \le 12$。 | ||
- $words[i]$ 由小写英文字母组成。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```python | ||
输入: words = ["gin", "zen", "gig", "msg"] | ||
输出: 2 | ||
解释: | ||
各单词翻译如下: | ||
"gin" -> "--...-." | ||
"zen" -> "--...-." | ||
"gig" -> "--...--." | ||
"msg" -> "--...--." | ||
|
||
共有 2 种不同翻译, "--...-." 和 "--...--.". | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输入:words = ["a"] | ||
输出:1 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:模拟 + 哈希表 | ||
|
||
1. 根据题目要求,将所有单词都转换为对应摩斯密码。 | ||
2. 使用哈希表存储所有转换后的摩斯密码。 | ||
3. 返回哈希表中不同的摩斯密码个数(脊哈希表的长度)作为答案。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def uniqueMorseRepresentations(self, words: List[str]) -> int: | ||
table = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] | ||
word_set = set() | ||
|
||
for word in words: | ||
word_mose = "" | ||
for ch in word: | ||
word_mose += table[ord(ch) - ord('a')] | ||
word_set.add(word_mose) | ||
|
||
return len(word_set) | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(s)$,其中 $s$ 为数组 $words$ 中所有单词的长度之和。 | ||
- **空间复杂度**:$O(s)$。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# [0868. 二进制间距](https://leetcode.cn/problems/binary-gap/) | ||
|
||
- 标签:位运算 | ||
- 难度:简单 | ||
|
||
## 题目链接 | ||
|
||
- [0868. 二进制间距 - 力扣](https://leetcode.cn/problems/binary-gap/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定一个正整数 $n$。 | ||
|
||
**要求**:找到并返回 $n$ 的二进制表示中两个相邻 $1$ 之间的最长距离。如果不存在两个相邻的 $1$,返回 $0$。 | ||
|
||
**说明**: | ||
|
||
- $1 \le n \le 10^9$。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```python | ||
输入:n = 22 | ||
输出:2 | ||
解释:22 的二进制是 "10110"。 | ||
在 22 的二进制表示中,有三个 1,组成两对相邻的 1。 | ||
第一对相邻的 1 中,两个 1 之间的距离为 2。 | ||
第二对相邻的 1 中,两个 1 之间的距离为 1。 | ||
答案取两个距离之中最大的,也就是 2。 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输入:n = 8 | ||
输出:0 | ||
解释:8 的二进制是 "1000"。 | ||
在 8 的二进制表示中没有相邻的两个 1,所以返回 0。 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:遍历 | ||
|
||
1. 将正整数 $n$ 转为二进制字符串形式 $bin\underline{}n$。 | ||
2. 使用变量 $pre$ 记录二进制字符串中上一个 $1$ 的位置,使用变量 $ans$ 存储两个相邻 $1$ 之间的最长距离。 | ||
3. 遍历二进制字符串形式 $bin\underline{}n$ 的每一位,遇到 $1$ 时判断并更新两个相邻 $1$ 之间的最长距离。 | ||
4. 遍历完返回两个相邻 $1$ 之间的最长距离,即 $ans$。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def binaryGap(self, n: int) -> int: | ||
bin_n = bin(n) | ||
pre, ans = 2, 0 | ||
|
||
for i in range(2, len(bin_n)): | ||
if bin_n[i] == '1': | ||
ans = max(ans, i - pre) | ||
pre = i | ||
|
||
return ans | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(\log n)$。 | ||
- **空间复杂度**:$O(1)$。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# [1451. 重新排列句子中的单词](https://leetcode.cn/problems/rearrange-words-in-a-sentence/) | ||
|
||
- 标签:字符串、排序 | ||
- 难度:中等 | ||
|
||
## 题目链接 | ||
|
||
- [1451. 重新排列句子中的单词 - 力扣](https://leetcode.cn/problems/rearrange-words-in-a-sentence/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:「句子」是一个用空格分隔单词的字符串。给定一个满足下述格式的句子 $text$: | ||
|
||
- 句子的首字母大写。 | ||
- $text$ 中的每个单词都用单个空格分隔。 | ||
|
||
**要求**:重新排列 $text$ 中的单词,使所有单词按其长度的升序排列。如果两个单词的长度相同,则保留其在原句子中的相对顺序。 | ||
|
||
请同样按上述格式返回新的句子。 | ||
|
||
**说明**: | ||
|
||
- $text$ 以大写字母开头,然后包含若干小写字母以及单词间的单个空格。 | ||
- $1 \le text.length \le 10^5$。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```python | ||
输入:text = "Leetcode is cool" | ||
输出:"Is cool leetcode" | ||
解释:句子中共有 3 个单词,长度为 8 的 "Leetcode" ,长度为 2 的 "is" 以及长度为 4 的 "cool"。 | ||
输出需要按单词的长度升序排列,新句子中的第一个单词首字母需要大写。 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输入:text = "Keep calm and code on" | ||
输出:"On and keep calm code" | ||
解释:输出的排序情况如下: | ||
"On" 2 个字母。 | ||
"and" 3 个字母。 | ||
"keep" 4 个字母,因为存在长度相同的其他单词,所以它们之间需要保留在原句子中的相对顺序。 | ||
"calm" 4 个字母。 | ||
"code" 4 个字母。 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:模拟 | ||
|
||
1. 将 $text$ 按照 `" "` 进行分割为单词数组 $words$。 | ||
2. 将单词数组按照「单词长度」进行升序排序。 | ||
3. 将单词数组用 `" "` 连接起来,并将首字母转为大写字母,其他字母转为小写字母,将结果存入答案字符串 $ans$ 中。 | ||
4. 返回答案字符串 $ans$。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def arrangeWords(self, text: str) -> str: | ||
words = text.split(' ') | ||
words.sort(key=lambda word:len(word)) | ||
ans = " ".join(words).capitalize() | ||
|
||
return ans | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n \times \log n)$,其中 $n$ 为字符串 $text$ 的长度。 | ||
- **空间复杂度**:$O(n)$。 | ||
|
Oops, something went wrong.