博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Reverse Words in a String 翻转字符串
阅读量:4108 次
发布时间:2019-05-25

本文共 650 字,大约阅读时间需要 2 分钟。

题目:

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

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

解答:

在尾部添加一个空格 构成标准形式 避免复杂判断。

代码:

class Solution {public:	void reverseWords(string &s) {		int i = 0;		int start = -1;		stack
st; string temp = "", result = ""; s = s + ' '; while (i < s.length()) { if (s[i] != ' ') { if (start == -1) start = i; } else { if (start != -1) { temp = s.substr(start, i - start); st.push(temp); start = -1; } } i++; } while (!st.empty()) { result = result + st.top() + " "; st.pop(); } s = result.substr(0, result.length() - 1); }};

转载地址:http://futsi.baihongyu.com/

你可能感兴趣的文章
stm32 堆和栈(stm32 Heap & Stack)【worldsing笔记】
查看>>
STM32 keil mdk启动代码发分析 .
查看>>
解析 STM32 的启动过程(写的不错)
查看>>
应用层和传输层的关系
查看>>
802.11协议用到的简写
查看>>
802.11 学习笔记
查看>>
lwip--有趣的数组定义(预处理)
查看>>
lwIP配置文件opt.h和lwipopts.h初步分析
查看>>
lwIP配置文件opt.h和lwipopts.h初步分析
查看>>
lwIP ARP协议分析
查看>>
智能卡操作系统(COS),什么是智能卡操作系统(COS)
查看>>
基于linux-2.6.38.8内核的SDIO/wifi驱动分析
查看>>
天线 基本概念
查看>>
【经典讨论】STM8L和MSP430的低功耗对比(长期开放)
查看>>
S3C2440、S3C2450和S3C6410之间区别
查看>>
S3C2440和S3C6410性能比较
查看>>
HDMI接口与VGA接口有什么区别?
查看>>
protel99se 定位孔干什么用的,定位孔和安装孔有什么区别?
查看>>
RCC_APB2Periph_AFIO--复用IO时钟的使用
查看>>
C语言运算符优先级 详细列表
查看>>