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

分析:将一句英文中单词倒序后输出

思路:用空格符将单词分开,倒序后再重新组装。关于单词倒序,可以先转成charArray后逆序组装,但oj会报time limit exceeded Last executed input:

即算法复杂度过高。这里JAVA中使用StringBuffer的reverse方法即可

JAVA CODE

class Solution {
public String reverseWords(String s) {
String[] ss = s.split(" ");
s = "";
for (int i = 0; i < ss.length; i++) {
StringBuffer stringBuffer = new StringBuffer (ss[i]);
stringBuffer = stringBuffer.reverse();
s += stringBuffer;
if (i != ss.length - 1)
s += " ";
}
return s;
}
}

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

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

  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 、151. Reverse Words in a String

    557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...

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

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

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

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

  7. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

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

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

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

随机推荐

  1. mysql的索引问题分析

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "PingFang SC"; color: #454545 } span. ...

  2. github如何添加ssh

    1.运行git Bash 输入如下命令: $ cd ~/.ssh $ ls 输入这2个命令 ,我们可以看到 id_rsa.pub 或 id_dsa.pub 这2个文件已经存在了,id_rsa 是私钥, ...

  3. Centos 7服务启动文件

    在Centos 7中,如果要编辑一个脚本服务文件,并使用systemd进行管理,则必须将服务文件命名为/etc/systemd/system/*.service. service unit文件中的选项 ...

  4. Makefile中export分析

    在分析内核启动过程的./arch/arm/Makefile文件里碰到了这样字段 162 export TEXT_OFFSET GZFLAGS MMUEXT 然后在子目录arch/arm/kernel/ ...

  5. 汇编指令-MRS(读)和MSR(写)指令操作CPSR寄存器和SPSR寄存器使用(1)

    1.MSR和MRS指令介绍 MRS 指令:  对状态寄存器CPSR和SPSR进行读操作.通过读CPSR可以获得当前处理器的工作状态.读SPSR寄存器可以获得进入异常前的处理器状态(因为只有异常模式下有 ...

  6. UCOSii项目在NIOSii上的移植

    概览 本次使用Altera公司的NIOS II软核. 使用Quatus工具生成BSP并利用BSP打包工具生成UCOSII嵌入环境. 手动书写LCD驱动与显示函数,对UCOS II加入简单图像显示接口. ...

  7. Qt--自定义Model

    众所周知,Qt提供了一套Model/View框架供开发者使用,Model用来提供数据, View则用来提供视觉层的显示.实际上这是一套遵循MVC设计模式的GUI框架,因为Qt还提供了默认的Delega ...

  8. CentOS 6.5安装部署Zabbix监控系统

    CentOS 6.5安装部署Zabbix监控系统 先说一点废话,我没有用centos7做实验,讲真,centos 7我也不常用,喜欢新版本的同学其实可以尝试下,注意一点的就是centos 6.5只支持 ...

  9. 【Alpha阶段】第一次Scrum Meeting!

    每日任务 1.本次会议为第一次 Meeting会议: 2.本次会议在中午12:30,在第五社区5号楼楼下,召开本次会议为30分钟讨论接下来的任务: 一.今日站立式会议照片 二.每个人的工作 (有wor ...

  10. 第二次项目冲刺(Beta阶段)5.22

    1.提供当天站立式会议照片一张 会议内容: ①检查前一天的任务情况,将遇到的困难反馈.解决. ②制定新一轮的任务计划. 2.每个人的工作 (1)工作安排 队员 今日进展 明日安排 王婧 #53(完成) ...