leetcode Online Judge 150题 解答分析之一 Reverse Words in a String
问题
Given an input string, reverse the string word by word.
For example, Given s = "the sky is blue", return "blue is sky the".
面试时我应该至少问的问题
- 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.
我大概想了下,但是没有问出来,需要改进!
代码分析
class Solution {
public:
static void reverseWords(string &s)
{
int len = s.length();
string result;
for(int i= len - 1; i >= 0; )
{
int lastend = 0;
while(i >= 0 && s[i] == ' ') //需要先判断i >=0,否则s[i]会出错
{
i--;
}
if(i < 0) //这里需要break,因为可能一连串的空格,没有任何字母,这时就应该及时退出
break;
lastend = i;
while(i >= 0 && s[i] != ' ') //这里也一样,需要先保证i在合法范围,再访问s[i]
{
i--;
}
if(i <= lastend) // i may be -1
{
if(!result.empty())
result.append(" "); // 至少有一个字符串时,需要添加空格
result.append(s.substr(i+1, lastend - i));
}
}
s.assign(result);
}
};
leetcode Online Judge 150题 解答分析之一 Reverse Words in a String的更多相关文章
- 【LeetCode刷题Java版】Reverse Words in a String
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- 决战Leetcode: easy part(1-50)
本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...
- AI面试必备/深度学习100问1-50题答案解析
AI面试必备/深度学习100问1-50题答案解析 2018年09月04日 15:42:07 刀客123 阅读数 2020更多 分类专栏: 机器学习 转载:https://blog.csdn.net ...
- (转载)Autodesk面试技术题解答
Autodesk面试技术题解答 By SmartPtr(http://www.cppblog.com/SmartPtr/) 近一年以来,AUTODESK的面试题在网上是闹的沸沸扬扬, ...
- [LeetCode] 接雨水,题 Trapping Rain Water
这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...
- LeetCode面试常见100题( TOP 100 Liked Questions)
LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...
- AliCrackme_2题的分析
作者:Fly2015 AliCrackme_2.apk运行起来的注册界面,如图. 首先使用Android反编译利器Jeb对AliCrackme_2.apk的Java层代码进行分析. 很幸运,就找到了该 ...
- Python小白的数学建模课-A1.国赛赛题类型分析
分析赛题类型,才能有的放矢. 评论区留下邮箱地址,送你国奖论文分析 『Python小白的数学建模课 @ Youcans』 带你从数模小白成为国赛达人. 1. 数模竞赛国赛 A题类型分析 年份 题目 要 ...
- LeetCode之字符串处理题java
344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...
随机推荐
- Michael Schatz - 序列比对课程
Michael Schatz - Cold Spring Harbor Laboratory 最近在研究 BWA mem 序列比对算法,直接去看论文,看不懂,论文就3页,太精简了,好多背景知识都不了解 ...
- 【转】PCI学习笔记
1.PCI设备编号 每一个PCI device都有其unique PFA(PCI Fcntion Address) PFA由 bus number.device number.functi ...
- 【转】 linux内存管理
一 为什么需要使用虚拟内存 大家都知道,进程需要使用的代码和数据都放在内存中,比放在外存中要快很多.问题是内存空间太小了,不能满足进程的需求,而且现在都是多进程,情况更加糟糕.所以提出了虚拟内存,使得 ...
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
摘要: JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外,还有jps.jstack.jmap.jhat.jstat.hprof等小巧的工具,本博客希望 ...
- 关于firstChild,firstElementChild和children
<div> <p>123</p> </div> 在上面这段代码中,如果使用以下js代码 var oDiv=document.getElementByTa ...
- Linux 系统把英文修改成中文界面
1.一般安装后的linux系统都是英文的界面,网上查了一下各种说法都有,我只做了如下的配置就好了,下载个中文包,改一下i18n就完事了,并没有那么复杂 下面上图文: 目前是英文的界面 2.下载个中文包 ...
- VC比例放大缩小
CRect rect; ::GetWindowRect(m_hWnd, rect); ScreenToClient(rect); m_nDlgWidth = rect.right - rect.lef ...
- DataFormatString 转
数据绑定之DataFormatString 设定BoundField的DataFormatString,通常有以下几种 DataFormatString= "{0:C}" 货币,货 ...
- 转网页WB.ExecWB控件打印方法
网页WB.ExecWB控件打印方法 2010-02-01 12:48 代码: <table width="100%" cellpadding="1" on ...
- git代码冲突解决
1.git fetch 跟git pull差别是前者不会和本地直接merge code,而后者会,所以git fetch更安全 git fetch origin master:tmpgit dif ...