Skip to content

Commit 104855c

Browse files
committed
添加了问题“2788.按分隔符拆分字符串”的代码和题解
1 parent 18786fc commit 104855c

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-01-20 23:18:28
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-01-20 23:22:03
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<string> splitWordsBySeparator(vector<string>& words, char separator) {
14+
vector<string> ans;
15+
for (string& word : words) {
16+
int last = -1;
17+
for (int i = 0; i <= word.size(); i++) {
18+
if (i == word.size() || word[i] == separator) {
19+
if (i - last > 1) {
20+
ans.push_back(word.substr(last + 1, i - last - 1));
21+
}
22+
last = i;
23+
}
24+
}
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-01-20 23:24:06
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-01-20 23:25:26
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
11+
ans = []
12+
for word in words:
13+
splited = word.split(separator)
14+
for this in splited:
15+
if this:
16+
ans.append(this)
17+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@
521521
|2731.移动机器人|中等|<a href="https://leetcode.cn/problems/movement-of-robots/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/10/LeetCode%202731.%E7%A7%BB%E5%8A%A8%E6%9C%BA%E5%99%A8%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133758255" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/movement-of-robots/solutions/2476843/letmefly-2731yi-dong-ji-qi-ren-nao-jin-j-vwf2/" target="_blank">LeetCode题解</a>|
522522
|2744.最大字符串配对数目|简单|<a href="https://leetcode.cn/problems/find-maximum-number-of-string-pairs/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/17/LeetCode%202744.%E6%9C%80%E5%A4%A7%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%85%8D%E5%AF%B9%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135662583" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-maximum-number-of-string-pairs/solutions/2608691/letmefly-2744zui-da-zi-fu-chuan-pei-dui-lnd3n/" target="_blank">LeetCode题解</a>|
523523
|2760.最长奇偶子数组|简单|<a href="https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/11/16/LeetCode%202760.%E6%9C%80%E9%95%BF%E5%A5%87%E5%81%B6%E5%AD%90%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134449952" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-even-odd-subarray-with-threshold/solutions/2529749/letmefly-2760zui-chang-qi-ou-zi-shu-zu-m-f5ob/" target="_blank">LeetCode题解</a>|
524+
|2788.按分隔符拆分字符串|简单|<a href="https://leetcode.cn/problems/split-strings-by-separator/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/20/LeetCode%202788.%E6%8C%89%E5%88%86%E9%9A%94%E7%AC%A6%E6%8B%86%E5%88%86%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135724019" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/split-strings-by-separator/solutions/2612832/letmefly-2788an-fen-ge-fu-chai-fen-zi-fu-s1dn/" target="_blank">LeetCode题解</a>|
524525
|2807.在链表中插入最大公约数|中等|<a href="https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/06/LeetCode%202807.%E5%9C%A8%E9%93%BE%E8%A1%A8%E4%B8%AD%E6%8F%92%E5%85%A5%E6%9C%80%E5%A4%A7%E5%85%AC%E7%BA%A6%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135424056" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/solutions/2593002/letmefly-2807zai-lian-biao-zhong-cha-ru-idjs4/" target="_blank">LeetCode题解</a>|
525526
|2824.统计和小于目标的下标对数目|简单|<a href="https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/11/24/LeetCode%202824.%E7%BB%9F%E8%AE%A1%E5%92%8C%E5%B0%8F%E4%BA%8E%E7%9B%AE%E6%A0%87%E7%9A%84%E4%B8%8B%E6%A0%87%E5%AF%B9%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134594377" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-pairs-whose-sum-is-less-than-target/solutions/2539873/letmefly-2824tong-ji-he-xiao-yu-mu-biao-7ulk5/" target="_blank">LeetCode题解</a>|
526527
|2828.判别首字母缩略词|简单|<a href="https://leetcode.cn/problems/check-if-a-string-is-an-acronym-of-words/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/12/20/LeetCode%202828.%E5%88%A4%E5%88%AB%E9%A6%96%E5%AD%97%E6%AF%8D%E7%BC%A9%E7%95%A5%E8%AF%8D/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135106811" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-a-string-is-an-acronym-of-words/solutions/2573316/letmefly-2828pan-bie-shou-zi-mu-suo-lue-kiuxi/" target="_blank">LeetCode题解</a>|
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: 2788.按分隔符拆分字符串
3+
date: 2024-01-20 23:18:08
4+
tags: [题解, LeetCode, 简单, 数组, 字符串, 字符串解析, 模拟, 遍历]
5+
---
6+
7+
# 【LetMeFly】2788.按分隔符拆分字符串:模拟(字符串处理)
8+
9+
力扣题目链接:[https://leetcode.cn/problems/split-strings-by-separator/](https://leetcode.cn/problems/split-strings-by-separator/)
10+
11+
<p>给你一个字符串数组 <code>words</code> 和一个字符 <code>separator</code> ,请你按 <code>separator</code> 拆分 <code>words</code> 中的每个字符串。</p>
12+
13+
<p>返回一个由拆分后的新字符串组成的字符串数组,<strong>不包括空字符串</strong> 。</p>
14+
15+
<p><strong>注意</strong></p>
16+
17+
<ul>
18+
<li><code>separator</code> 用于决定拆分发生的位置,但它不包含在结果字符串中。</li>
19+
<li>拆分可能形成两个以上的字符串。</li>
20+
<li>结果字符串必须保持初始相同的先后顺序。</li>
21+
</ul>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong>示例 1:</strong></p>
26+
27+
<pre>
28+
<strong>输入:</strong>words = ["one.two.three","four.five","six"], separator = "."
29+
<strong>输出:</strong>["one","two","three","four","five","six"]
30+
<strong>解释:</strong>在本示例中,我们进行下述拆分:
31+
32+
"one.two.three" 拆分为 "one", "two", "three"
33+
"four.five" 拆分为 "four", "five"
34+
"six" 拆分为 "six"
35+
36+
因此,结果数组为 ["one","two","three","four","five","six"] 。</pre>
37+
38+
<p><strong>示例 2:</strong></p>
39+
40+
<pre>
41+
<strong>输入:</strong>words = ["$easy$","$problem$"], separator = "$"
42+
<strong>输出:</strong>["easy","problem"]
43+
<strong>解释:</strong>在本示例中,我们进行下述拆分:
44+
45+
"$easy$" 拆分为 "easy"(不包括空字符串)
46+
"$problem$" 拆分为 "problem"(不包括空字符串)
47+
48+
因此,结果数组为 ["easy","problem"] 。
49+
</pre>
50+
51+
<p><strong>示例 3:</strong></p>
52+
53+
<pre>
54+
<strong>输入:</strong>words = ["|||"], separator = "|"
55+
<strong>输出:</strong>[]
56+
<strong>解释:</strong>在本示例中,"|||" 的拆分结果将只包含一些空字符串,所以我们返回一个空数组 [] 。 </pre>
57+
58+
<p>&nbsp;</p>
59+
60+
<p><strong>提示:</strong></p>
61+
62+
<ul>
63+
<li><code>1 &lt;= words.length &lt;= 100</code></li>
64+
<li><code>1 &lt;= words[i].length &lt;= 20</code></li>
65+
<li><code>words[i]</code> 中的字符要么是小写英文字母,要么就是字符串 <code>".,|$#@"</code> 中的字符(不包括引号)</li>
66+
<li><code>separator</code> 是字符串 <code>".,|$#@"</code> 中的某个字符(不包括引号)</li>
67+
</ul>
68+
69+
70+
71+
## 方法一:模拟
72+
73+
新建一个空的字符串数组作为答案,遍历字符串数组的所有字符串,使用一个变量```last```记录上一个```separator```的位置(初始值为```-1```)。
74+
75+
接着遍历这个字符串的每一个字符,如果遍历到了字符串尾或当前字符为```separator```,就看当前下标和```last```之间是否有字符存在。若有,则添加到答案数组中。
76+
77+
最终返回答案数组即为答案。
78+
79+
+ 时间复杂度$O(sum_character(words))$,时间复杂度为字符串数组中字符串的个数
80+
+ 空间复杂度$O(1)$,力扣的算法返回值不计入算法的空间复杂的
81+
82+
### AC代码
83+
84+
#### C++
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
vector<string> splitWordsBySeparator(vector<string>& words, char separator) {
90+
vector<string> ans;
91+
for (string& word : words) {
92+
int last = -1;
93+
for (int i = 0; i <= word.size(); i++) {
94+
if (i == word.size() || word[i] == separator) {
95+
if (i - last > 1) {
96+
ans.push_back(word.substr(last + 1, i - last - 1));
97+
}
98+
last = i;
99+
}
100+
}
101+
}
102+
return ans;
103+
}
104+
};
105+
```
106+
107+
#### Python
108+
109+
```python
110+
# from typing import List
111+
112+
class Solution:
113+
def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
114+
ans = []
115+
for word in words:
116+
splited = word.split(separator)
117+
for this in splited: # 过滤空串
118+
if this:
119+
ans.append(this)
120+
return ans
121+
```
122+
123+
> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2024/01/20/LeetCode%202788.%E6%8C%89%E5%88%86%E9%9A%94%E7%AC%A6%E6%8B%86%E5%88%86%E5%AD%97%E7%AC%A6%E4%B8%B2/)哦~
124+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/135724019](https://letmefly.blog.csdn.net/article/details/135724019)

0 commit comments

Comments
 (0)