作者: 负雪明烛
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. Linux-普通用户和root用户任意切换

    普通用户切换为root: 1.[xnlay@bogon ~]$含义:xnlay代表当前用户,bogon指的是主机名,~表示当前用户,$表示普通用户:[root@bogon ~]#root代表是超级用户 ...

  2. kubernetes部署 flannel网络组件

    创建 flannel 证书和私钥flannel 从 etcd 集群存取网段分配信息,而 etcd 集群启用了双向 x509 证书认证,所以需要为 flanneld 生成证书和私钥. cat > ...

  3. Windows cmd 命令行基本操作

    Windows cmd 命令行基本操作 1. 进入到指定根目录 注意:不区分大小写 例如进入到 D 盘 2. 进入到指定的目录 例如 (如果目录文件名太长,可以使用 tab 键来自动补全.重复按可以进 ...

  4. java类加载、对象创建过程

    类加载过程: 1, JVM会先去方法区中找有没有相应类的.class存在.如果有,就直接使用:如果没有,则把相关类的.class加载到方法区 2, 在.class加载到方法区时,会分为两部分加载:先加 ...

  5. 浅谈MySQL数据库面试必要掌握知识点

    概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 MySQL官方地址 https://www.mysql.com/ MySQL 8系列最新版本为8.0.27,5系列的最 ...

  6. day10设置文件权限

    day10设置文件权限 yum复习 1.修改IP [root@localhost ~]# sed -i 's#.200#.50#g' /etc/sysconfig/network-scripts/if ...

  7. 商业爬虫学习笔记day2

    1. get传参 (1)url中包含中文报错解决方法 urllib.request.quote("包含中文的url", safe = "string.printtable ...

  8. java代码定时备份mysql数据库及注意事项——基于 springboot

    源码地址: https://gitee.com/kevin9401/BackUpDataBase git 拉取: https://gitee.com/kevin9401/BackUpDataBase. ...

  9. cordova配置与开发

    1.环境配置 1.1.安装ant 从 apache官网 下载ant,安装并配置,将ant.bat所在目录加到path环境变量,如c:\apache-ant\bin\.在cmd中运行以下语句如不报错即可 ...

  10. 【Java基础】Java中new对象的过程

    序言 联系我上次写的关于Java内存的文章,对象访问在 Java 语言中无处不在,是最普通的程序行为,但即使是最简单的访问,也会却涉及 Java 栈.Java 堆.方法区这三个最重要内存区域之间的关联 ...