557. Reverse Words in a String III【easy】
557. Reverse Words in a String III【easy】
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
解法一:
class Solution {
public:
void reverseSelf(string & s, int start, int end)
{
while (start <= end) {
char c = s[start];
s[start] = s[end];
s[end] = c;
++start;
--end;
}
}
string reverseWords(string s) {
int i = ;
while (i < s.length()) {
int j = i;
while (j < s.length() && s[j] != ' ') {
++j;
}
reverseSelf(s, i, j - );
i = j + ;
}
return s;
}
};
无他,注意下标边界尔。
解法二:
public String reverseWords(String s)
{
char[] s1 = s.toCharArray();
int i = ;
for(int j = ; j < s1.length; j++)
{
if(s1[j] == ' ')
{
reverse(s1, i, j - );
i = j + ;
}
}
reverse(s1, i, s1.length - );
return new String(s1);
} public void reverse(char[] s, int l, int r)
{
while(l < r)
{
char temp = s[l];
s[l] = s[r];
s[r] = temp;
l++; r--;
}
}
参考@sooryaprasanna 的代码
Step 1. Convert the string to char[] array
Step 2. Whenever I encounter a space ' ' , I call the reverse function ( just to keep the code clean )
Step 3. Repeat till the end!
解法三:
class Solution {
public:
string reverseWords(string s) {
for (int i = ; i < s.length(); i++) {
if (s[i] != ' ') { // when i is a non-space
int j = i;
for (; j < s.length() && s[j] != ' '; j++) { } // move j to the next space
reverse(s.begin() + i, s.begin() + j);
i = j - ;
}
}
return s;
}
};
参考@alexander 的代码。
补充一下reverse函数:
reverse函数可以反转一个容器中的内容,包含在<algorithm>库中。
1、函数原型
reverse函数等同于下面的代码:
template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last))
{
std::iter_swap (first,last);
++first;
}
}
reverse函数使用iter_swap来交换两个元素。
2、参数:first、last
first和last是双向迭代器类型,reverse函数反转的范围是[first,last),所以包括first指向的元素,不包括last指向的元素。
3、返回值
reverse函数没有返回值。
参考自:http://blog.csdn.net/u012877472/article/details/49557077
557. Reverse Words in a String III【easy】的更多相关文章
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【leetcode_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- Week4 - 500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...
- 557. Reverse Words in a String III 翻转句子中的每一个单词
[抄题]: Given a string, you need to reverse the order of characters in each word within a sentence whi ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- LeetCode 557 Reverse Words in a String III 解题报告
题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...
随机推荐
- 【树状数组】bzoj2789 [Poi2012]Letters
处理数组A,A[i]表示字符串a的第i个字符排序后应去的位置,队列可. 对于多次出现的字符,其在a中的顺序和在b中的顺序应该是一一对应的. #include<cstdio> #includ ...
- [OpenJudge8462][序列DP]大盗阿福
大盗阿福 总时间限制: 1000ms 内存限制: 65536kB [描述] 阿福是一名经验丰富的大盗.趁着月黑风高,阿福打算今晚洗劫一条街上的店铺. 这条街上一共有 N 家店铺,每家店中都有一些现金. ...
- MythXinWCF通用宿主绿色版V1.1
更新内容:宿主的唯一编号和名称可以输入符号"."日志文本框增加滚动条,并且总是显示文本末端增加启动方式选择:1.手动启动 2.跟随系统启动 最新下载地址: http://pan.b ...
- 位图(BitMap)索引
前段时间听同事分享,偶尔讲起Oracle数据库的位图索引,顿时大感兴趣.说来惭愧,在这之前对位图索引一无所知,因此趁此机会写篇博文介绍下位图索引. 1. 案例 有张表名为table的表,由三列组成,分 ...
- C#远程获取图片文件流的方法【很通用】
因为之前写的代码,也能获取到图片流信息,但是会是凌乱的线条,后百度得这个方法,必须记录一下 C# try { WebRequest myrequest = WebRequest.Create(Http ...
- 网络流量工具iftop,ifstat
此文非原创,转自 http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858923.html 介绍 ifstat工具是个网络接口监测工具,比较简单 ...
- MongoDB集群设置集合分片生效及查看集合分片情况
#连接mongos /opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:27017 #使用admin数据库 use admin ...
- JavaScript basics: 2 ways to get child elements with JavaScript
原文: https://blog.mrfrontend.org/2017/10/2-ways-get-child-elements-javascript/ Along the lines of oth ...
- HDU 2604 Queuing(矩阵高速幂)
题目地址:HDU 2604 这题仅仅要推出公式来,构造矩阵就非常easy了.问题是推不出公式来..TAT.. 从递推的思路考虑.用f(n)表示n个人满足条件的结果.假设最后一个是m则前n-1人能够随意 ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何配置虚拟轴 TC3
在Motion上添加一个NC Task 在Axis上右击添加一个轴,类型为Continuous Axis 在PLC上右击添加新项,然后添加一个PLC项目 在引用中添加TC2_MC2的库引用 ...