原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description

题目:

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.

题解:

遇到空格就把之前标记的部分reverse.

Note: 最后一次reverse别忘了.

Time Complexity: O(n), n = s.length().

Space: O(n), char array.

AC Java:

 class Solution {
public String reverseWords(String s) {
if(s == null || s.length() == 0){
return s;
} int len = s.length();
char [] charArr = s.toCharArray(); int lo = 0;
for(int i = 0; i<=len; i++){
if(i==len || charArr[i]==' '){
reverse(charArr, lo, i-1);
lo = i+1;
}
} return new String(charArr);
} private void reverse(char [] s, int i, int j){
while(i < j){
swap(s, i++, j--);
}
} private void swap(char [] s, int i, int j){
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}

类似Reverse String II.

LeetCode Reverse Words in a String III的更多相关文章

  1. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

  2. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  3. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  4. 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 ...

  5. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  6. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  7. 53. leetcode557. Reverse Words in a String III

    557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...

  8. ledecode Reverse Words in a String III

    557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...

  9. 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 ...

随机推荐

  1. position:absolute width

    position:absolute; left:0px; right:0px; top:0px; bottom:0px; 设置布满整个父范围,设置了absolute的元素可以通过以上4个属性来展开面, ...

  2. OC导入框架方式#import、@import的区别

    #import负责导入程序所需的文件的信息导入到程序中,随着程序所需的文件越来越多,程序就要导入更多的文件,这就带来了越来越长的编译时间,而且有大量重复的.为了解决这个问题可以采用以下办法解决,创建. ...

  3. OpenGL学习进程(8)第六课:点、边和图形(三)绘制图形

    本节是OpenGL学习的第六个课时,下面介绍OpenGL图形的相关知识:     (1)多边形的概念: 多边形是由多条线段首尾相连而形成的闭合区域.OpenGL规定,一个多边形必须是一个“凸多边形”. ...

  4. MongoDB 使用Limit和Skip完成分页 和游标(二)

    //$slice操作符返回文档中指定数组的内部值 //查询出Jim书架中第2~4本书 db.persons.find({name:"jim"},{books:{"$sli ...

  5. 20145210姚思羽《网络对抗》Web基础

    20145210姚思羽<网络对抗>Web基础 实验后回答问题 (1)什么是表单 表但是与用户交互的窗口,负责采集网页中的数据,允许用户在表单中输入信息. (2)浏览器可以解析运行什么语言. ...

  6. Android开发 -- Bootloader

    本文转载自:http://blog.csdn.net/jmq_0000/article/details/7378348 LK是什么 LK 是 Little Kernel 它是 appsbl (Appl ...

  7. 一言(ヒトコト)Hitokoto API

    『想要成为无论多么悲伤的时候,也能够漂亮微笑的人吧.』 Hitokoto API 更新:2014.02.22 问题/反馈:api # hitokoto.us 数据获取:[ 数据获取 ] 调用举例:[  ...

  8. Luogu-1527 [国家集训队]矩阵乘法

    Luogu-1527 [国家集训队]矩阵乘法 题面 Luogu-1527 题解 昨天学CDQ分治时做了一些题,但是因为题(wo)太(tai)水(lan)了(le)并没有整理 学了一晚上的整体二分,拿这 ...

  9. MapReduce-文本输入

    1.TextInputFormat TextInputFormat是默认的InputFormat.每条记录是一行输入.键是LongWritable类型,存储该行在整个文件中的字节偏移量.值是这行的内容 ...

  10. matplotlib画子图时设置总标题

    matplotlib subplots绘图时 设置总标题 :fig.suptitle(name)