问题

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

For example, Given s = "the sky is blue", return "blue is sky the".

面试时我应该至少问的问题

  1. What constitutes a word? A sequence of non-space characters constitutes a word.
  2. Could the input string contain leading or trailing spaces? Yes. However, your reversed string should not contain leading or trailing spaces.
  3. 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的更多相关文章

  1. 【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 ...

  2. 决战Leetcode: easy part(1-50)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

  3. AI面试必备/深度学习100问1-50题答案解析

    AI面试必备/深度学习100问1-50题答案解析 2018年09月04日 15:42:07 刀客123 阅读数 2020更多 分类专栏: 机器学习   转载:https://blog.csdn.net ...

  4. (转载)Autodesk面试技术题解答

    Autodesk面试技术题解答 By SmartPtr(http://www.cppblog.com/SmartPtr/)          近一年以来,AUTODESK的面试题在网上是闹的沸沸扬扬, ...

  5. [LeetCode] 接雨水,题 Trapping Rain Water

    这题放上来是因为自己第一回见到这种题,觉得它好玩儿 =) Trapping Rain Water Given n non-negative integers representing an eleva ...

  6. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  7. AliCrackme_2题的分析

    作者:Fly2015 AliCrackme_2.apk运行起来的注册界面,如图. 首先使用Android反编译利器Jeb对AliCrackme_2.apk的Java层代码进行分析. 很幸运,就找到了该 ...

  8. Python小白的数学建模课-A1.国赛赛题类型分析

    分析赛题类型,才能有的放矢. 评论区留下邮箱地址,送你国奖论文分析 『Python小白的数学建模课 @ Youcans』 带你从数模小白成为国赛达人. 1. 数模竞赛国赛 A题类型分析 年份 题目 要 ...

  9. LeetCode之字符串处理题java

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

随机推荐

  1. MySQL FUNCTION 整理

    -- 返回最后一个INSERT查询中, AUTO_INCREMENT列设置的第一个表的值. SELECT LAST_INSERT_ID();

  2. SSH框架总结

    首先,SSH是由多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框架的系统从职责 ...

  3. Centos启动Cassandra交互模式失败:No appropriate python interpreter found

    在CentOS6.5安装好Cassandra后,启动交互模式: bin/sqlsh 192.168.10.154 时,报错 No appropriate python interpreter foun ...

  4. WPF进度条系列①滑动小圆点

     写在之前: 关于WPF的样式,我也是学习了很多朋友的文章才有了下面的东西,因为时间有些久远 & 备份的链接也都不在了. 所以,究竟是看过哪些文章,也是记不清楚了…… 请见谅. ------- ...

  5. (C# & Unity) 脚本语言 ES

    C# 编写,解释执行,语法类似 JS,动态类型,支持闭包,支持热更新,效率比较低,目前暂时没有发现 BUG,实际游戏运行稳定,没有发现内存泄漏 Github:https://github.com/ea ...

  6. perl基础:传递hash类型参数

    1 如果是只有一个参数要传,且是hash,最直接想到的办法就是像传其他类型参数一样直接传, 如:   subFuntion(%hash1); 2 如果有多于一个参数要传,这里假设只有一个参数的类型是h ...

  7. Microsoft Office Project 相关教程 收集

    Project教程 如何建立任务间链接 Project教程:[10]如何将项目插入主项目 如何有效使用Project(1)——编制进度计划.保存基准 如何有效使用Project(2)——进度计划的执行 ...

  8. c# 打乱数组

    有时候得到了一个List,我想把它随机排列一下顺序.而且如果针对不同类型的List都能用,就要用到泛型. 其实思想很简单,就是从原List中每次随机取一项,添加到新的List中,并在原List中删除. ...

  9. [IOS 开发] 自定义(重写) UITableViewCell的高亮背景色

    IOS的sdk中,对UITableViewCell的高亮背景色只支持两种颜色,分别为UITableViewCellSelectionStyleBlue和UITableViewCellSelection ...

  10. Apache、NGINX支持中文URL

    Apache(32位):安装环境:CentOS 5.6 + Apache 2.2.15安装结果:安装后支持“中文图片.文件名”链接直接打开以下为安装过程:1.下载安装包 wget ftp://ftp. ...