[抄题]:

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. python web开发配置

    安装python(最好是2.*版本) 安装easyinstall 参考Windows 下 Python easy_install 的安装 完成之后注意环境变量的配置 在系统环境变量的PATH中添加ea ...

  2. (十一)java循环结构

    while(循环的条件) {循环的语句} int a = 1; while(a < 5) { System.out.println(a);//1,2,3,4 a++; } System.out. ...

  3. 解决跨域POST登录中IE不能正常工作的bug

    结合我的这篇blog <简单实用的跨域表单POST提交> 文章,这篇blog中的思路是解决在www.a.com站中登录 同时要把关联站www.b.com登录状态也设置成登录状态,在a中获取 ...

  4. Linux 下安装composer

    1.下载composer.phar文件. 2.将composer.phar文件上传linux. 3.执行 php composer.phar 4.全局安装:mv composer.phar /usr/ ...

  5. Java实现7种常见的排序算法

    数据结构中的内部排序:不需要访问外存便能完成,是一个逐步扩大记录的有序序列长度的过程. 可以分为5类: 1.插入排序:直接插入排序,稳定排序,时间复杂度为O(n^2)非递减有序,设置r[0]为哨兵进行 ...

  6. remoting与socket、web service的比较及实例

    remoting基础 一种分布式处理方式,可以说是DCOM的一种升级 跨过应用程序域,与另外的应用程序域进行通信,即穿越边界 在remoting中是通过通道(channel)来实现两个应用程序域之间对 ...

  7. BZOJ3507 [Cqoi2014]通配符匹配

    题意 几乎所有操作系统的命令行界面(CLI)中都支持文件名的通配符匹配以方便用户.最常见的通配符有两个,一个是星号("*"),可以匹配0个及以上的任意字符:另一个是问号(" ...

  8. curl -I 说明

    curl -I 查看header头信息

  9. nginx学习资源

    在了解nginx的时候 看到的一些资源: https://www.cnblogs.com/EdwinChan/p/8350984.html http://tengine.taobao.org/book ...

  10. Php header()函数及其常见使用

    语法: Void header(string $string[,bool $replace=true [, int $http_response_code) 向客户端发送原始的HTTP报头 需注意: ...