[抄题]:

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. Django初体验——搭建简易blog

    前几天在网上看到了篇采用Django搭建简易博客的视频,好奇心驱使也就点进去学了下,毕竟自己对于Django是无比敬畏的,并不是很了解,来次初体验. 本文的操作环境:ubuntu.python2.7. ...

  2. 在spring配置文件中的 <context:property-placeholder/>用途

    location属性为 具体配置文件的classpath:地址 (可以取配置文件中的值利用${key}的形式,而不用多次写值) 1.这样一来就可以为spring配置的bean的属性设置值了,比如spr ...

  3. CodeForces - 622F:The Sum of the k-th Powers (拉格朗日插值法求自然数幂和)

    There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees. ...

  4. button和input type=button的区别及注意事项

    <button>标签 定义和用法 <button>标签定义一个按钮. 在button元素内部,您可以放置内容,比如文本或图像.这是该元素与使用input元素创建的按钮之间的不同 ...

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

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

  6. C语言中的printf函数的输入输出问题

    这个问题是个很基础,但是我一直不知道的问题,是同学问了之后才知道的,长知识了. 这里要注意的是,printf函数是从右向左计算,从左向右输出. 距离如下: #include <stdio.h&g ...

  7. log4net生成多个日志文件

    使用Log4Net日志组件时,经常会碰到这样一种场景,我想把错误的日志记录在Error.log文件中,而把操作的日志放在Operation.log文件中 经过几番尝试,终于实现了,在此把Log4Net ...

  8. 解决时间控件input不能选择的问题

    方法一: 方法二: 方法二参考: https://blog.csdn.net/huilan_same/article/details/52385401

  9. java代码实现点击鼠标从控制台输出信息

    总结:最难的就是当我们需要点击按钮时去实现某个功能-----------因为那个我没有理解透,是涉及整个程序的 package com.a.b; import javax.swing.*; impor ...

  10. Error EBUSY: osd.0 is still up; must be down before removal的解决办法

    标签(空格分隔):ceph,ceph运维,osd故障 集群环境: [root@node3 ~]# cat /etc/redhat-release CentOS Linux release 7.3.16 ...