557. Reverse Words in a String III
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
题目:
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take contest" Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
思路:本题思路比较简单,遍历数组,遇到空格,便将此次空格与上次的空格间的字符串反转,本题要注意的是起始单词与末尾单词的处理
代码:
class Solution {
public:
string reverseWords(string s) {
int z = -1;
for(int i=0;i<s.size();i++)
{
if(s[i]==' ')
{
reverse(s.begin()+z+1,s.begin()+i);
z=i;
}
}
reverse(s.begin()+z+1,s.end());
return s;
}
};
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |