原题链接在这里: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. 前端 css续

    CSS选择器 1.标签选择器 为类型标签设置样式例如:<div>.<a>.等标签设置一个样式,代码如下: <style> /*标签选择器,找到所有的标签应用以下样式 ...

  2. Linux文件系统管理 开机自动挂载及fstab文件修复

    概述 开机自动挂载及fstab文件修复 开机自动挂载 实现开机后自动挂载,就需要修改系统的自动挂载文件 /etc/fstab.因为系统就是依赖这个文件决定启动时加载的文件系统的.通过vi 打开/etc ...

  3. 使用JDK将tomcat变成https访问

    1,今日JDK目录,执行命令 keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore " ...

  4. poj 3617输出格式问题

    注意是说的80个字母一行....

  5. maven项目在打war包时出现非法字符: '\ufeff' 解决方案

    问题描述: 开发工具MyEclipse 的总体开发环境,编码格式总体设置为UTF-8,在将web项目打包的时候出现:非法字符:'\ufeff" 错误. 解决方案: 利用notePad++打开 ...

  6. Openfire部署和配置说明

    一.程序部署 1.1 程序和脚本 将文件拷贝到对应目录下,文件包括:Openfire.tar和setup.sh脚本.Openfire.tar为可执行文件库.配置等的压缩包,setup.sh为解压和部署 ...

  7. centos7 下安装eclipse

    1 在下面路径下载 eclipse-jee-neon-2-linux-gtk-x86_64.tar.gzhttp://eclipse.stu.edu.tw/technology/epp/downloa ...

  8. 【codevs2011】最小距离之和 [LNOI2013](Floyd)

    题目网址:http://codevs.cn/problem/2011/ 题目大意:有一个图,每次删一条边(可以重复删),求每次删边之后所有点对的最短距离之和. 看了一眼题目,顿时发现了O(n^4)的暴 ...

  9. HBase-建表(普通建表及预分区建表)

    package com.hbase.HBaseAdmin; import java.io.IOException; import org.apache.hadoop.conf.Configuratio ...

  10. hadoop hdfs java api操作

    package com.duking.util; import java.io.IOException; import java.util.Date; import org.apache.hadoop ...