[抄题]:

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"

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

先分割再合并,各种调用api

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. stringbuilder新建字符串都要用,此题恰好符合
  2. 空格真的要敲一个空格才行
  3. for (String st : str) 简写似乎更优雅

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

stringbuilder新建字符串都要用

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

【string类】

split 方法 :分割

toString() 方法返回此对象本身(它已经是一个字符串)

trim() 方法用于删除字符串的头尾空白符

【stringbuilder类】

.reverse()方法:翻转
.append 添加字符串

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

151. Reverse Words in a String 用指针反而麻烦了

[代码风格] :

class Solution {
public String reverseWords(String s) {
//cc
if (s == null) {
return s;
}
//split
String[] str = s.split(" ");
//reverse
for (int i = 0; i < str.length; i++) {
str[i] = new StringBuilder(str[i]).reverse().toString();
}
//combine
StringBuilder result = new StringBuilder();
for (String st : str) {
result.append(st + " ");
}
//return
return result.toString().trim();
}
}

557. Reverse Words in a String III 翻转句子中的每一个单词的更多相关文章

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

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

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

  4. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  5. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

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

  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. 【leetcode_easy】557. Reverse Words in a String III

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

随机推荐

  1. Redis常见面试题总结

    Redis面试题总结(1) 2018年02月28日 17:42:21 LSX丨笔头先生 阅读数:3568更多 个人分类: 面试题总结   (1)什么是redis? Redis 是一个基于内存的高性能k ...

  2. [BZOJ5290][HNOI2018]道路

    bzoj luogu sol 考场上普及\(dp\)都做不来,果然是思想僵化了. 设\(f[u][i][j]\)表示在\(u\)点,上方有\(i\)条未修复的公路和\(j\)条未修复的铁路的最小花费. ...

  3. LeetCode Optimal Division

    原题链接在这里:https://leetcode.com/problems/optimal-division/description/ 题目: Given a list of positive int ...

  4. var ev = ev || event

    event是事件对象(也是window的属性),但不是标准的,只有IE支持. 在W3C标准支持的浏览器下事件对象是引发事件函数的第一个参数,参数名随意. 所以,我们一般使用事件对象: function ...

  5. ORACLE系统表大全

    下面全面是对Oracle系统表的一些介绍: 数据字典dict总是属于Oracle用户sys的. 1.用户: select username from dba_users; 改口令 alter user ...

  6. 使用 key 登录时分开记录操作历史记录

    线上服务器一般都是配置 key 登录,一个账号可以多个工作人员连接,操作命令历史却全部记录在一个文件中,当然后查看某条命令是谁执行的时候就不好查了.这时候我们就可以通过配置 histroy 相关环境变 ...

  7. php://input解决APP发送图片问题

    今天公司要求用APP发送一个图片到PHP程序接收并保存起来,而且中间还需要很多参数! 以前没有做过APP和PHP交互,这次算是一个挑战吧(对一个没有人指导实习生来说) 1.APP发1.jpg,而且带有 ...

  8. 洛谷 2831 (NOIp2016) 愤怒的小鸟——仅+1所以bfs优化

    题目:https://www.luogu.org/problemnew/show/P2831 状压dp.跑得很慢.(n^2*2^n) 注意只打一只猪的情况. #include<iostream& ...

  9. 归并排序的JavaScript实现

    思想 这是一种分治算法.将原始数组切分成较小的数组,直到每个小数组只有一项,然后在将小数组归并为排好序的较大数组,直到最后得到一个排好序的最大数组. 代码 function mergeSort(arr ...

  10. linux下安装ZipArchive扩展和libzip扩展

    在项目开发的时候,由于要下载多个录音文件,我就需要打包下载这个功能 学习源头: https://www.landui.com/help/show-8079 https://www.aliyun.com ...