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 ...
随机推荐
- 7 -- Spring的基本用法 -- 6...
7.6 Spring 3.0 提供的Java配置管理 Spring 允许使用Java类进行配置管理,可以不使用XML来管理Bean,以及Bean之间的依赖关系. Interface :Person p ...
- H5 学习笔记(一、关于position定位)
主要是relative与absolute的用法: 1.relative 依据left right top bottom 等属性在正常文档流中脱离位置,即相对于他的正常文档流位置进行移动.两个都为rel ...
- 区分IE版本的三个方法
我们通常使用IE条件判断语言来处理IE的CSS问题,但其实还是有其他的一些方法来处理IE的CSS bug的. 一.IE条件判断语句 IE条件判断语句也许是用的最多的区分IE版本(IE6, IE7, I ...
- c#数据绑定(3)——数据转化为信息
文/嶽永鹏 本文主要在数据绑定1和2中新增了DataSet对象,练习了如何在DataSet中添加表.关系和约束,同时本文也简要的介绍了如何将数据转化为信息. 目标界面: XAML代码: <Gri ...
- rpm 软件安装
安装 vi 软件 rpm q查询 a全部软件 e 卸载 i 安装 v 显示进度 h 以#显示 首先确定软件时候安装 rpm 实现软件的安装 卸载 升级 查询等 1)查询是否已经安装过 rpm -qa ...
- git代码冲突解决
1.git fetch 跟git pull差别是前者不会和本地直接merge code,而后者会,所以git fetch更安全 git fetch origin master:tmpgit dif ...
- MySQL之存储引擎MyISAM/InnoDB高并发优化经验
https://www.centos.bz/2011/09/mysql-myisam-innodb-optimization-experience/
- web sql database数据存储位置
Q1: 数据存储在哪儿? Web Storage / Web SQL Database / Indexed Database 的数据都存储在浏览器对应的用户配置文件目录(user profile di ...
- java 中Session 持久化问题
首先: 今天发现了个session 持久化的问题 在Tomcat 停止运行后再启动 session 中保存的东西还会存在 ,百度了一下 原理 1.Session Create 时 2.Sessio ...
- 关于如何在cenos7.0上实现mysql数据库远程连接
设置mysql允许别的客户机控制的权限 mysql -uroot -p #此处为本地linux帐号密码 select user,host from mysql.user; #查看mysql表对应use ...