Leetcode - Reverse Words
比起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的更多相关文章
- 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 ...
- 2016.5.16——leetcode:Reverse Bits(超详细讲解)
leetcode:Reverse Bits 本题目收获 移位(<< >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- [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 ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- [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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [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-> ...
随机推荐
- c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...
- hibernate加载实体映射文件 及映射文件auto-import
第一种方法: 在hibernate.cfg.xml中<mapping resource="包名/Xxx.hbm.xml"/>包名为路径形式( x/x/x这种形式) 第二 ...
- JS获取IP
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js新浪多地域测试方法:http://int.dpool.s ...
- JSP执行过程
JSP执行流程: @1.客户端发出请求. @2.Web容器将JSP转译成Servlet源代码. @3.Web容器将产生的源代码进行编译. @4.Web容器加载编译后的代码并执行. @5.把执行结果响应 ...
- Bootstrap 和 LESS
Bootstrap 简介 什么是 Bootstrap? Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的 ...
- PHPCMS收集标签使用
调用子栏目(在栏目首页模板需要用到) {pc:content action="category" catid="$catid" num="25&quo ...
- Sass学习
1.1下载地址: http://rubyinstaller.org/downloads 2.1 安装 SASS是Ruby语言写的,但是两者的语法没有关系.不懂Ruby,照样使用.只是必须先安装Ruby ...
- To Build A Dev Env On Linux(Ubuntu)
Step1:System Installing 1)use iso image to Step2:Configuration Step3:Software Installing Step4:Other ...
- instancetype 与 id for Objective-C
instancetype.id.NSObject的区别 - simalone 1.instancetype只能用于方法的返回类型,而id用处和NSObject *类似. 2.instancetyp ...
- Standard Numeric Format Strings
The following table describes the standard numeric format specifiers and displays sample output prod ...