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 ...
随机推荐
- 知乎背景图 canvas 效果
思路分析: 1.创造一块画布, 2.在画布内随机产生一些小球,小球位置,半径,颜射,大小,速度等都可以随机产生, 3.定义画小球函数与小球移动函数, 4.将每一个小球圆心都与其它小球链接, 5判断每一 ...
- 强大的打印功能jatoolsPrinter使用总结
最近功能做项目,需要实现打印条码标签的功能,对于第一次接触打印机的小白来说简直是折磨死我拉,公司采购的打印机是斑马的GK888T,其实,如果单纯的想实现能打印出来标签的话,直接用window.prin ...
- 获取Graphics对象的方法
在做自定义控件时或者GDI+的时候经常会遇到获取Graphics实例的问题.一般有三种获取方式 1.从Paint事件的参数中获取.窗体和许多控件都有一个Paint事件,有一个PaintEventArg ...
- Centos启动Cassandra交互模式失败:No appropriate python interpreter found
在CentOS6.5安装好Cassandra后,启动交互模式: bin/sqlsh 192.168.10.154 时,报错 No appropriate python interpreter foun ...
- hashmap 的作用
就是一个键值对应的集合HashMap a = new HashMap(); a.put("name", "abcdef"); // key是name,value ...
- acm系统开发笔记
时间: 2016/2/29 遇到的困难: 数据库配置的mysql和java(Date)不一致,出现下面错误 Date date = new Date(); SimpleDateFormat ...
- C#精髓 第四讲 GridView 72般绝技
http://blog.csdn.net/21aspnet/article/details/1540301
- Ext4 ComboBox组件使用
先来看例子: Ext.define('schoolModel', { extend: 'Ext.data.Model', fields: [{ name: 'id', type: 'string' ...
- Java集合类学习笔记(各种Map实现类的性能分析)
HashMap和Hashtable的实现机制几乎一样,但由于Hashtable是一个古老的.线程安全的集合,因此HashMap通常比Hashtable要快. TreeMap比HashMap和Hasht ...
- 职工工资管理系统 --C语言
#include<stdio.h> #include<string.h> #include<stdlib.h> #define NUM 1000 void ente ...