当前位置:网站首页>Leetcode 151. Invert words in string
Leetcode 151. Invert words in string
2022-07-18 08:55:00 【LuZhouShiLi】
Leetcode 151. Invert words in string
subject
Give you a string s , Invert the string word The order of .
word Is a string of non whitespace characters .s Use at least one space in the string word Separate .
return word Reverse order and word The result string connected with a single space .
Be careful : Input string s There may be leading spaces in 、 Trailing spaces or multiple spaces between words . In the returned result string , Words should be separated by only a single space , And does not contain any additional spaces .
Ideas
- First, remove all redundant spaces in the word
- Then flip the entire string
- Then look for the beginning and end subscripts of each word from the beginning , Flip each word
Code
class Solution {
public:
string reverseWords(string s) {
removeExtraSpaces(s);// Remove redundant spaces
reverse(s,0,s.size() - 1);// Invert all strings
for(int i = 0; i < s.size(); i++)
{
// Assign the first subscript of a word to j
int j = i;
// Find spaces between words Flip Words
while(j < s.size() && s[j] != ' ') j++;
// j Until the space position stops
reverse(s,i,j - 1);
i = j;// At this time, the subscript at the space position is given i
// But the next step i++ i Point to the beginning and end of the next word
}
return s;
}
void reverse(string &s,int start,int end)
{
for(int i = start,j = end; i < j; i++,j--)
{
// Define the beginning and end pointers Direct exchange
swap(s[i],s[j]);
}
}
void removeExtraSpaces(string& s){
// Remove the space in the string
int slowIndex = 0,fastIndex = 0;// Define speed pointer
// Remove the space before the string
while(s.size() > 0 && fastIndex < s.size() && s[fastIndex] == ' ')
{
fastIndex++;
}
for(; fastIndex < s.size(); fastIndex++)
{
// Remove redundant spaces in the middle of the string
if(fastIndex - 1 > 0 && s[fastIndex - 1] == s[fastIndex] && s[fastIndex] == ' ')
{
continue;// If both the current character and the previous character are spaces So directly continue
}
else
{
s[slowIndex] = s[fastIndex];
slowIndex++;// Use non space characters to cover directly forward
}
}
if(slowIndex - 1 > 0 && s[slowIndex - 1] == ' ')
{
// Remove the space at the end of the string
s.resize(slowIndex - 1);
}
else
{
s.resize(slowIndex);// Reset string size
}
}
};
边栏推荐
- Grails 引发的中文乱码问题
- [2023 school recruitment questions] column planning (non final presentation status, for bloggers' personal reference)
- 【集训DAY2】Torch bearer【暴力】【DFS】
- Event 4624 is login successful!?! Is that true?
- 90% of people have never used Microsoft!
- Unity2D--相机跟随
- Selenium uses' chromedriver 'executable needs to be in path Error reporting information
- Huawei's general card identification function enables multiple card bindings with one key
- Lambda表达式二:方法引用
- 谱归一化(Spectral Normalization)在以LSTM为判别器的GAN中的运用(tensorflow2)
猜你喜欢

华为通用卡证识别功能,一键实现多种卡绑定

90% of people have never used Microsoft!
![[leetcode weekly -- string] 6114 Move the clip to get the string](/img/2f/1efd4d88d2ea3db5dfb77ff9887ba4.png)
[leetcode weekly -- string] 6114 Move the clip to get the string

使电脑拥有公网IP方法
![[leetcode weekly -- hash table simulation] 6113 The smallest number in an infinite set](/img/4f/817ca6ff4265ab2fafe71210e22a4e.png)
[leetcode weekly -- hash table simulation] 6113 The smallest number in an infinite set
![[untitled]](/img/1d/f0519a7cd66c1e5a4dd048c71707b9.jpg)
[untitled]

When the wechat applet activates the account, it will prompt "this account has been activated, please log in directly with the account password"
VBA drives SAP GUI to complete initialization of interface element value

黑龙江“智慧林草”顶层规划方案

(2021 Niuke multi school V) B-boxes (probability expectation)
随机推荐
(2021牛客多校五)D-Double Strings(乘法原理+动态规划)
Range of motion of leecode robot
One article understand go integrate CAPTCHA to realize the function of verification code
【Arduino与mpu6050握手】
About March 2022, apt-c-41 disguised as winrar Exe attack terminal side emergency response troubleshooting point
建立自己的网站(23)
PCIE知识点-011:PCIE 配置能力结构与协议版本的关系
PCIe knowledge point-011: relationship between PCIe configuration capability structure and protocol version
Online sql to yaml tool
【集训DAY4】Sequence transformation【思维】
base64和blob对图片的压缩
国产版Postman工具,好用!
iNFTnews | NFT门票将改变参与活动的方式
【Leetcode二叉树--最大路径和】124.二叉树中的最大路径和
Huawei's general card identification function enables multiple card bindings with one key
Lambda表达式二:方法引用
Google Earth Engine APP(GEE)—查看亚马逊平原的1984——至今的每一景影像
力扣解法汇总558- 四叉树交集
【LeetCode】10. Maximum Subarray·最大子数组和
Laravel reports an error: requires ext DOM * - > the requested PHP extension DOM is missing from your system