当前位置:网站首页>Leetcode daily exercise: sort sentences
Leetcode daily exercise: sort sentences
2022-07-27 04:08:00 【Sharp blade CC】
1859. Sort the sentences
The difficulty is simple 15
One The sentence It refers to a sequence of words connected by a single space , And there are no spaces at the beginning and end . Every word contains only lowercase or uppercase letters .
We can add... To a sentence from 1 Start word position index , And put all the words in the sentence Out of order .
- For example , The sentence
"This is a sentence"Can be scrambled to get"sentence4 a3 is2 This1"perhaps"is2 sentence4 This1 a3".
To give you one Out of order Sentences s , It contains no more than 9 individual , Please reconstruct and get the sentences in the original order .
Example 1:
Input :s = "is2 sentence4 This1 a3"
Output :"This is a sentence"
explain : take s The words in are sorted according to their initial positions , obtain "This1 is2 a3 sentence4" , Then delete the number .
Example 2:
Input :s = "Myself2 Me1 I4 and3"
Output :"Me Myself and I"
explain : take s The words in are sorted according to their initial positions , obtain "Me1 Myself2 and3 I4" , Then delete the number .
Tips :
2 <= s.length <= 200sOnly lowercase and uppercase letters 、 Spaces and from1To9The number of .sThe number of words in is1To9individual .sWords in are separated by a single space .sDoes not contain any leading or suffix spaces .
Ideas :
Thinking is more violent haha , Is to define a variable first cur Find the position of the number , And then use begin Variable from cur From back to front until you find Space , Or if it's the first word , Will be out of bounds , So you have to judge . And then begin To cur - 1 At the end of the letter insert the new string Variable tmp in , then Add a space at the end .
Then cycle through it , Add one to the number you want to find each time , Until the number cannot be found ,cur Out of bounds until . Finally, delete the last space at the end .
class Solution {
public:
string sortSentence(string s) {
string tmp;
char i = '1'; // For viewing s Numeric characters in
size_t cur = s.find(i); // use cur Look for numbers
while(cur != string::npos)
{
size_t begin = s.rfind(' ', cur); // use begin Find the beginning of the word
// if begin Come to the beginning , Then directly start with cur Part of append that will do
if(begin == string::npos)
{
tmp.append(s, 0, cur);
}
else
{
tmp.append(s, begin + 1, cur - begin - 1);
}
i++;
cur = s.find(i);
tmp.push_back(' '); // Remember to give each time tmp Insert a space at the end
}
tmp.pop_back(); // Remove the last space
return tmp;
}
};
边栏推荐
- Summer meal | rich people are different from what you think (day 5) + power system power flow simulation (documents and matlab code)
- js实现页面跳转与参数获取加载
- C language introduction practice (12): find the value of natural constant e
- 「Gonna Be Alright 会好的」数藏现已开售!感受艺术家的心灵共鸣
- Restful fast request 2022.2.2 release, supporting batch export of documents
- 线上一个隐匿 Bug 的复盘
- C# 使用SqlSugar Updateable系统报错无效数字,如何解决?求指导!
- Will this flinkcdc monitor all tables in the database? Or the designated table? I look at the background log. It monitors all tables. If it monitors
- Interview question: the difference between three instantiated objects in string class
- A. YES or YES?
猜你喜欢

Plato farm is expected to further expand its ecosystem through elephant swap

jmeter接口测试(登录、注册)
![Article main content extraction software [based on NLP technology]](/img/1c/7c1b0e9bc9af62308f4124104f6110.png)
Article main content extraction software [based on NLP technology]

Detailed analysis of trajectory generation tool in psins toolbox

NFT数字藏品系统开发:小蚁数智帮助品牌一键上链发行NFT

暑假加餐|有钱人和你想的不一样(第5天)+电力系统潮流仿真(文档和Matlab代码)

The fifth strong network cup national network security challenge Title reappearance (with title attachment, detailed explanation)

Application of one-dimensional array

VR全景现在是不是刚需?看完你就明白了

Redis(九) - Redis之分布式锁
随机推荐
Framework学习之旅:init 进程启动过程
222. Number of nodes of complete binary tree
Chapter 4 decision tree and random forest
安装umi4阻碍一天的问题解决了
【SemiDrive源码分析】【驱动BringUp】41 - LCM 驱动 backlight 背光控制原理分析
物联网智能家居项目---智能卧室
C语言学习笔记 —— 内存管理
356页14万字高端商业办公综合楼弱电智能化系统2022版
Parallel desktop startup virtual machine "operation failed" problem solution
leetcode:433. 最小基因变化
STM32CubeMX学习笔记(41)——ETH接口+LwIP协议栈使用(DHCP)
真正意义上的数字零售应当具有更加丰富的内涵和意义
JS array de duplication (including simple array de duplication and object array de duplication)
Threads and processes
手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
Golang发送邮件库email
Redis (IX) - redis distributed lock
Case when in MySQL returns multiple field processing schemes
Article main content extraction software [based on NLP technology]
Parallels Desktop启动虚拟机“操作失败”问题解决