【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 ...
随机推荐
- 端口TCP——简介
cmd命令:telnet 如果需要搭建外网可访问的网站,可以顺便勾选HTTP,HTTPS端口:
- nginx二级域名指向不同文件项目配置
需要使用泛域名解析, 并且加上空的判断,以保证没有二级域名的也可以访问 核心配置 server_name ~^(?<subdomain>.+)\.caipudq\.cn$;if ( $su ...
- C++面试基础篇(二)
1.数组与指针的区别 数组下标运算实际上都是通过指针进行的. 数组名代表着指向该数组中下标为0的元素的指针,但有例外:sizeof(数组名)返回整个数组的大小,而非指针大小:&数组名返回一个指 ...
- 生产调优3 HDFS-多目录配置
目录 HDFS-多目录配置 NameNode多目录配置 1.修改hdfs-site.xml 2.格式化NameNode DataNode多目录配置(重要) 1.修改hdfs-site.xml 2.测试 ...
- acquire, acre, across
acquire An acquired taste is an appreciation [鉴赏] for something unlikely to be enjoyed by a person w ...
- Spark 广播变量和累加器
Spark 的一个核心功能是创建两种特殊类型的变量:广播变量和累加器 广播变量(groadcast varible)为只读变量,它有运行SparkContext的驱动程序创建后发送给参与计算的节点.对 ...
- 重磅丨腾讯云开源业界首个 etcd 一站式治理平台 Kstone
Kstone 开源 在 CNCF 云原生基金会举办的2021年12月9日 KubeCon China大会上,腾讯云容器 TKE 团队发布了 Kstone etcd 治理平台开源项目. Kstone ...
- 转 android design library提供的TabLayout的用法
原文出处:http://chenfuduo.me/2015/07/30/TabLayout-of-design-support-library/ 在开发中,我们常常需要ViewPager结合Fragm ...
- maven的lifecycle
1.maven clean. 清理项目的target目录 2.maven compile 编译项目 3.maven test 编译项目后,再执行Junit测试方法 4.maven package 编译 ...
- 【Java 8】Optional 使用
一.前言 如果要给 Java 所有异常弄个榜单,我会选择将 NullPointerException 放在榜首.这个异常潜伏在代码中,就像个遥控炸弹,不知道什么时候这个按钮会被突然按下(传入 null ...