作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: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:

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.

题目大意

把字符串中的每个单词进行翻转,翻转后仍然按照原来的单词顺序进行拼接。

解题方法

Java解法

很简单的题,直接分割开每个单词,然后把单词翻转再拼接就好了。我的第一种做法:

public class Solution {
public String reverseWords(String s) {
String[] words = s.split(" ");
StringBuilder ans = new StringBuilder();
boolean isFirst = true;
for(String word : words){
StringBuilder temp = new StringBuilder(word);
word = temp.reverse().toString();
if(isFirst){
ans.append(word);
isFirst = false;
}else{
ans.append(" " + word);
} }
return ans.toString();
}
}

看着时间有点长 14 ms,于是没用StringBuilder,方法如下

public class Solution {
public String reverseWords(String s) {
String[] words = s.split(" ");
StringBuilder ans = new StringBuilder();
boolean isFirst = true;
for(String word : words){
word= reverse(word);
if(isFirst){
ans.append(word);
isFirst = false;
}else{
ans.append(" " + word);
} }
return ans.toString();
}
public String reverse(String s){
char[] chars = s.toCharArray();
for(int i = 0; i < chars.length / 2; i++){
char temp = chars[i];
chars[i] = chars[chars.length - 1 - i];
chars[chars.length - 1 - i] = temp;
}
return new String(chars);
}
}

时间变为 13 ms,还想继续压缩时间。全部用数组实现:

public class Solution {
public String reverseWords(String s) {
String[] words = s.split(" ");
char[] ans = new char[s.length()];
boolean isFirst = true;
int i = 0;
for (String word : words) {
char[] chars = word.toCharArray();
for (int j = 0; j < chars.length / 2; j++) {
char temp = chars[j];
chars[j] = chars[chars.length - 1 - j];
chars[chars.length - 1 - j] = temp;
}
System.arraycopy(chars, 0, ans, i, chars.length);
i += chars.length;
if (i != ans.length) {
ans[i] = ' ';
}
i++;
}
return new String(ans);
}
}

这个时间却变成了16ms,已经无语。嗯。就这样吧。

Python解法

使用Python可以直接使用split函数之后,进行[::-1]即做了翻转操作,然后再用" ".join()拼接在一起就行了。

class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
return " ".join(map(lambda x : x[::-1], s.split(" ")))

日期

2017 年 4 月 12 日
2018 年 11 月 6 日 —— 腰酸背痛要废了

【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)的更多相关文章

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

  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 翻转字符串中的单词 III

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

  5. Leetcode - 557. Reverse Words in a String III (C++) stringstream

    1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是 ...

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

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

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

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

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

随机推荐

  1. UE4 C++工程以Game模式启动

    UE4版本:4.24.3源码编译版本 Windows10 + VS2019环境 UE4 C++工程,默认情况下VS中直接运行是启动Editor模式: 有时为了调试等目的需要以Game模式启动,可以避免 ...

  2. C#筛选项联动,联动筛选

    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { FillDep(); } // FillDG(); ...

  3. vivo 敏感词匹配系统的设计与实践

    一.前言 谛听系统是vivo的内容审核平台,保障了vivo各互联网产品持续健康的发展.谛听支持审核多种内容类型,但日常主要审核的内容是文本,下图是一个完整的文本审核流程,包括名单匹配.敏感词匹配.AI ...

  4. Vue2全家桶+Element搭建的PC端在线音乐网站

    目录 1,前言 2,已有功能 3,使用 4,目录结构 5,页面效果 登录页 首页 排行榜 歌单列表 歌单详情 歌手列表 歌手详情 MV列表 MV详情 搜索页 播放器 1,前言 项目基于Vue2全家桶及 ...

  5. Git提交规范

    Commit message 的格式 每次提交,Commit message 都包括三个部分:Header,Body 和 Footer. <type>(<scope>): &l ...

  6. 【Reverse】初遇花指令

    解密花指令 全文参考了一个大师傅的blog:https://blog.csdn.net/zhangmiaoping23/article/details/38400393 介绍 花指令是对抗反汇编的有效 ...

  7. C++中union相关

    前两天做阿里笔试遇到一个选择题题目大概是 #include <iostream> #include <stdlib.h> using namespace std; union ...

  8. Windows zip版本安装MySQL

    Windows --MySQL zip版本安装记录: step1. 官网download zip包:http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5. ...

  9. 【Linux】【Shell】【Basic】字符串操作

    1. 字符串切片:             ${var:offset:number}                 取字符串的子串:                 取字符趾的最右侧的几个字符:${ ...

  10. java通过JDBC访问数据库(最基本的查询)

    一.步骤介绍 1.通过Class.forName()加载驱动: 2.通过DriverManager.getConnection()获取Conncetion连接对象: 3.创建Statement对象传递 ...