【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)
作者: 负雪明烛
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)的更多相关文章
- 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 ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 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 ...
- [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 ...
- Leetcode - 557. Reverse Words in a String III (C++) stringstream
1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是 ...
- 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 ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 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 ...
- 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 ...
随机推荐
- 一个画组织解剖图R包
地址: https://github.com/jespermaag/gganatogram
- Oracle、MySQL关机操作步骤
一.Oracle数据库单机关机(eg:LEAP系统) 先关闭使用数据库的应用系统,再关闭数据库 关闭数据库执行以下命令 1.关闭Oracle数据库监听器:(使用操作系统下管理Oracle的账户,关闭监 ...
- day12 查找文件
day12 查找文件 find命令:查找文件 find命令:在linux系统中,按照我们的要求去查询文件. 格式: find [查询的路径] [匹配模式] [匹配规则] 匹配模式: -name : 按 ...
- express系列(1)概述
在 Node.js 出现之前,前后端的开发必须使用不同的语言进行.为此你需要学习多种的语言和框架.有了 Node.js 之后,你就可以使用一门语言在前后端开发中自由切换,这是最吸引人的地方. 什么是 ...
- 2021广东工业大学十月月赛 F-hnjhd爱序列
题目:GDUTOJ | hnjhd爱序列 (gdutcode.cn) 一开始是用双指针从尾至头遍历,但发现会tle!! 后来朋友@77给出了一种用桶的做法,相当于是用空间换时间了. 其中用到的一个原理 ...
- Spring Cloud中,如何解决Feign整合Hystrix第一次请求失败的问题
Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...
- angular过滤器在html和js中的使用
在HTML中使用格式为:{{数据 | 过滤器名称:条件一:条件二--}}:过滤条件间使用:隔开 例如: 在代码中一般格式为: 变量 = $filter("过滤器名称")(被过滤数 ...
- Mybatis-Plus默认主键策略导致自动生成19位长度主键id的坑
原创/朱季谦 某天检查一位离职同事写的代码,发现其对应表虽然设置了AUTO_INCREMENT自增,但页面新增功能生成的数据主键id很诡异,长度达到了19位,且不是从1开始递增的-- 我检查了一下,发 ...
- 快速上手ANTLR
回顾前文: ANTLR 简单介绍 ANTLR 相关术语 ANTLR 环境准备 下面通过两个实例来快速上手ANTLR. 使用Listener转换数组 完整源码见:https://github.com/b ...
- centos7部署mysql-5.7
目录 一.环境声明 二.程序部署 三.更改初始密码 一.环境声明 [mysql-Server] 主机名 = host-1 系统 = centos-7.3 地址 = 1.1.1.1 软件 = mysql ...