题目例如以下:

Given an input string, reverse the string word by word.

For example,

Given s = "the sky is blue",

return "blue is sky the".

click to show clarification.

Clarification:

  • What constitutes a word?

    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?

    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?

    Reduce them to a single space in the reversed string.
题目要求不仅须要把字符串中的单词进行旋转,并且測试用例中还包括头尾含有空格、单词间空格数大于1个之类的字符串。所以要处理好空格,处理完空格的字符串肯定是小于或等于原字符串长度的,考虑到通过移动字符的方式来减小空格耗时长,我的思路是首先计算出去多余的空格后字符串应有的长度,然后又一次定义一个暂时字符串,开辟终于应有长度的空间。然后将原字符串逆序copy到新定义的字符串变量中,保证头尾多余空格已去除,保证单词间仅保留一个空格。然后将每一个单词再进行一次反转,便实现了题目所要求的功能。AC代码例如以下:
class Solution {
public:
void reverseWords(string &s)
{
if(s.empty())
return;
int count = 0;
int index = 0, indexTemp = 0, begin = 0, end = s.length()-1;
while(s[begin] == ' ' && begin<s.length()-1)
begin++;
while(s[end] == ' ' && end>0)
end--;
if(end == 0 && s[end] == ' ')
{
s = "";
return;
}
else if(end == 0)
{
s = s.substr(0,1);
return;
}
index = begin;
while(index <= end)
{
if(s[index] == ' ')
{
count++;
while(s[index] == ' ')
index++;
}
count++;
index++;
}
string temp(count,'\0');
index = end;
indexTemp = 0;
while(index >= begin)
{
if(s[index] == ' ')
{
temp[indexTemp] = s[index];
while(s[index] == ' ')
index--;
indexTemp++;
}
temp[indexTemp] = s[index];
indexTemp++;
index--;
}
indexTemp = 0;
begin = -1;
end = -1;
while(indexTemp < count)
{
if(temp[indexTemp] != ' ' && begin == -1)
{
begin = 0;
}
else if(indexTemp-1 >= 0 && temp[indexTemp-1] == ' '&&temp[indexTemp] != ' ')
{
begin = indexTemp;
}
else if((indexTemp+1 < count && temp[indexTemp+1] == ' ' && temp[indexTemp] != ' ') || ((indexTemp == count-1)&&temp[indexTemp] != ' '))
{
end = indexTemp;
reverse(temp, begin, end); }
indexTemp++;
}
s = temp;
} void reverse(string &s, int begin, int end)
{
while(begin < end)
{
char temp = s[begin];
s[begin] = s[end];
s[end] = temp;
begin++;
end--;
}
}
};


leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)的更多相关文章

  1. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  2. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  3. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  4. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  5. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  6. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  7. 345 Reverse Vowels of a String 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...

  8. LeetCode Reverse Words in a String 将串中的字翻转

    class Solution { public: void reverseWords(string &s) { string end="",tem="" ...

  9. 345. Reverse Vowels of a String翻转字符串中的元音字母

    [抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

随机推荐

  1. vb.net 调用api

    Public Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) ...

  2. 使用javascript实现图片上下切换效果并且实现顺序循环播放

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 毕业两年做到测试经理的经历总结- 各个端的自动化,性能测试结合项目具体场景实战,分析客户反馈的Bug

    前言 最近看到行业的前辈都分享一些过往的经历来指导我们这些测试人员,我很尊敬我们的行业前辈,没有他们在前面铺路,如今我们这帮年轻的测试人估计还在碰壁或摸着石头过河,结合前辈们的经验,作为年轻的测试人也 ...

  4. 【转】排列组合 "n个球放入m个盒子m"问题 总结

    出处:https://blog.csdn.net/qwb492859377/article/details/50654627 球,盒子都可以分成是否不能区分,和能区分,还能分成是否能有空箱子,所以一共 ...

  5. 【hdu 2328】Corporate Identity

    [链接]h在这里写链接 [题意] 找一个字典序最小的公共最长子串; [题解] 后缀数组. 把所有的串用不同的分隔符分开.(大于'z'的分隔符); 然后求出那几个固定的数组. 二分一下那个子串的长度. ...

  6. [D3] Start Visualizing Data Driven Documents with D3 v4

    It’s time to live up to D3’s true name and potential by integrating some real data into your visuali ...

  7. 可直接复制粘贴的boostrap图标库网址

    1:http://fontawesome.dashgame.com/ 2:http://www.kuiyu.net/art-34.html 3:http://www.bootcss.com/p/fon ...

  8. [慕课笔记]mongodb入门篇

    一:对mongodb有一个系统的完备的了解,只有概念清楚了,才能更好的使用 二:学会mongodb数据库的搭建 首先:了解如何部署数据库的服务 搭建简单的单机服务到搭建具有冗余容错功能的复制集再到搭建 ...

  9. Eclipse查看某个方法被哪些类调用

    方法一:打开该类,在类的定义上即类名上,右键-->References--->Project ,就可以查看该类是否被工程中的其他Java文件引用过:但是如果在JSP页面,这个方法查不出来 ...

  10. JavaScript的Math对象

    原文 简书原文:https://www.jianshu.com/p/8776ec9cfb58 大纲 前言 1.Math对象的值属性 2.Math对象的函数属性 3.Math对象的函数的使用 前言 Ma ...