[抄题]:

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. 使用wlan接收器经常重新登录怎么办

    wlan接收器是一个大功率的网卡,能够接受耿远距离的无线网络,在农村和乡镇很普及,很多家庭里都是用这个装置来接受远距离的CMCC信号.但是在使用的时候会经常出现一些问题,例如我们登陆以后,还没等上网就 ...

  2. VS 生成 dll、exe 版本号与SVN版本号一致

    1.VS 可自动生成版本号 注释掉以下两行代码 [assembly: AssemblyVersion("1.0.0.0")][assembly: AssemblyFileVersi ...

  3. Spring IOC容器的初始化-(二)BeanDefinition的载入和解析

    前言 1.在讲BeanDefinition的载入和解析之前,我们先来看看什么是BeanDefinition. Bean对象在Spring中是以BeanDefinition来描述的,也就是说在Sprin ...

  4. 列表推导式,两个for循环的例子

    [a for a in alist for b in blist if a>b] for i in alist,blist: print(i) >>  alist[] >> ...

  5. 真正明白c语言二级指针

    指针是C语言的灵魂,我想对于一级指针大家应该都很熟悉,也经常用到:比如说对于字符串的处理,函数参数的“值,结果传递”等,对于二级指针或者多级指针,我想理解起来也是比较容易的,比如二级指针就是指向指针的 ...

  6. vuecli3 运行报错

    Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Users\Administrator\De ...

  7. Ajax验证用户名

    用Ajax验证用户名: 接口: get guestbook/index.php m : index a : verifyUserName username : 要验证的用户名 返回 { code : ...

  8. SpringMVC上传文件大小的设置

    在spring-mvc.xml(springmvc的配置文件)里: <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> <bean id ...

  9. 源码安装ceph后使用测试集群的方法

    标签(空格分隔): ceph,ceph实验,ceph源码 通过博客 源码编译安装ceph(aarch64架构) 成功安装ceph之后,之后可以运行一个测试集群进行实验 1,进入安装构建目录: [roo ...

  10. 爬取github上流行的python项目

    # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import requests from pyquery import PyQ ...