比起POJ弱爆了一题,从后往前扫描一遍,O(n)时间,仅仅要注意各种极端情况就可以,不明确通过率为什么仅仅有13%。

#include<iostream>
#include<string>
using namespace std; class Solution {
public:
void reverseWords(string &s) { char* cstr = new char[s.size()+1]; int cc = 0;
int revstrC = s.size() - 1; while (revstrC >= 0)
{
while (revstrC>=0 && s.at(revstrC) == ' ')
{
revstrC--;
} if (revstrC >= 0)//find the end of a word
{
int end = revstrC; while (revstrC >= 0 && s.at(revstrC) != ' ')
revstrC--; s.copy(cstr+cc, end-revstrC, revstrC+1); cc = cc + end - revstrC; cstr[cc] = ' '; cc++;
}
}
cstr[cc-1] = '\0'; s.assign(cstr);
}
}; int main()
{
Solution sol;
string str = " Hello fwe asg wf vergv ";
sol.reverseWords(str); cout << str << endl;
}

Leetcode - Reverse Words的更多相关文章

  1. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  2. 2016.5.16——leetcode:Reverse Bits(超详细讲解)

    leetcode:Reverse Bits 本题目收获 移位(<<  >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...

  3. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

  4. [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 ...

  5. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  8. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

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

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

  10. [LeetCode] Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

随机推荐

  1. opencv有关错误及解决办法

    1.载入图片时内存溢出情况,如图: 分析及解决办法:因为载入的图片太大,导致内存溢出.所以更换小一点的图片就行了. 2.

  2. HDU5311 Hidden String

    Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a str ...

  3. php之分页类代码

    /* 思路 1.把地址栏的URL获取 2.分析URL中的query部分--就是?后面传参数的部分 3.query部分分析成数组 4.把数组中的page单元,+1,-1,形成2个新的数组 5.再把新数组 ...

  4. linux 定时执行shell

    第一步:安装 crontab ,命令 yum -y install vixie-cron                扩展:service crond start //启动服务            ...

  5. 除法(Division ,UVA 725)-ACM集训

    参考:http://www.cnblogs.com/xiaobaibuhei/p/3301110.html 算法学到很弱,连这么简单个问题都难到我了.但我偏不信这个邪,终于做出来了.不过,是参照别人的 ...

  6. js touch触屏原理分析

    之前我们做过许多触屏的特效,那么,今天,我们来分析下js的触屏原理.事实上,大家百度一下js touch基本上可以找到这文章“指尖下的js ——多触式web前端开发之一:对于Touch的处理”,我想这 ...

  7. jquery 实现的一款超简单的图片切换功能

    <html><head> <meta http-equiv="Content-Type" content="text/html; chars ...

  8. Android Context作用

    Context 用于访问全局信息的接口 App的资源: strings, drawable资源等等 工程代码:LearnContext.zip ---------------------------- ...

  9. QLineEdit

    The QLineEdit widget is a one-line text editor. Header: #include <QLineEdit> qmake: QT += widg ...

  10. "sessionFactory " or "hibernateTemplate " is required异常

    <beans    xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http:/ ...