[抄题]:

Given an input string, reverse the string word by word.

Example:

Input: "the sky is blue",
Output: "blue is sky the".

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

打碎成数组后再添加,居然有看不懂的bug。

public class Solution {
public String reverseWords(String s) {
//ini
char[] str = s.toCharArray(); //cc
if (str == null || str.length == 0) return s; //3 step3: reverse the whole, word, last
reverse(str, 0, str.length - 1); int wordStart = 0;
for (int i = 0; i < str.length; i++) {
if (str[i] == ' ') {
reverse(str, wordStart, i - 1);
wordStart = i + 1;
}
} reverse(str, wordStart, str.length - 1); private void reverse(char[] str, int start, int end) {
//do in a while loop
while (start < end) {
char temp = str[start];
str[start] = str[end];
str[end] = temp; start++;
end--;
}
} //return
StringBuilder sb = new StringBuilder();
for (char ch : str) sb.append(ch); return sb.toString();
}
}

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

[一句话思路]:

.split("\\s+")把单词分开(特别注意是反斜杠),然后倒贴(单词+空格),最后一个单词不贴空格。

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

[画图]:

[一刷]:

反斜杠写成正斜杠就错了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

不管输入是字符串还是字符数组,最后一个单词都先不贴,因为后面没有空格

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

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

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

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

public class Solution {
public String reverseWords(String s) {
//ini
String result = new String();
s = s.trim(); //cc
if (s == null || s.length() == 0) return result; //split into words
String[] words = s.split("\\s+"); //append for n - 1 to 1
for (int i = words.length - 1; i > 0; i--)
result += words[i] + " "; //append 0
result += words[0]; //return
return result;
}
}

151. Reverse Words in a String翻转一句话中的单词的更多相关文章

  1. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

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

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  3. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  4. 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...

  5. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  6. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  7. Leetcode151. Reverse Words in a String翻转字符串里的单词

    给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...

  8. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  9. [leetcode]151. Reverse Words in a String翻转给定字符串中的单词

    Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...

随机推荐

  1. https://blog.csdn.net/doegoo/article/details/50749817

    因为使用DiscuzX3.2进行系统的整合后,因为只是想在原J2EE的系统上增加论坛功能,而且J2EE中已经有一套用户的注册认证的体系,所以不需要在Discuz的系统中去注册以及登录功能,而是通过在J ...

  2. string函数的一些实现

    /************************************************************************* > File Name: test.cpp ...

  3. ASP.NET资源大全-知识分享 【转载】

    API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台.官网 ASP.NET WebAPI:快捷创建 HTTP 服务 ...

  4. 18.3 #define DM9000_DBG(fmt,args...) printf(fmt, ##args)代表什么

    标准C支持可变参数的函数,意味着函数的参数是不固定的,例如printf()函数的原型为:int printf( const char *format [, argument]... ) 而在GNU C ...

  5. OpenStack上搭建Q版的公共环境准备(step1)

    vmware14 centos7.5minimal版 controller1节点虚拟硬件配置: CPU:1颗2核 Memory:2G 硬盘:20G 网卡: VMnet1(仅主机模式):关闭DHCP,手 ...

  6. 双心一键获取winsxs的写入权限,解决VC运行库安装error1935错误

    @Echo offtitle 双心一键获取winsxs的写入权限,解决VC运行库安装error1935等错误set path=%path%;%~dp0setlocal EnableDelayedExp ...

  7. SpringSecurity-RememberMeAuthenticationFilter的作用

    启用remember-me功能,在配置文件中的http节点下添加: <remember-me remember-me-parameter="remember-me" reme ...

  8. 涂抹mysql笔记-管理mysql服务

    -DSYSCONFDIR=/mysql/conf \ 所以在conf下建立my.cnf文件 vi my.cnf [client]port=3306socket=/mysql/conf/mysql.so ...

  9. (整理)MySQL_REHL6.5 MySQL5.5 中文支持问题

    1 查看字符集命令 SHOW VARIABLES LIKE ‘character%’; 2 修改my.cnf文件中来实现中文支持 2.1 复制my.cnf 安装结束后,etc目录下并没有my.cnf文 ...

  10. leetcode85

    class Solution { public int maximalRectangle(char[][] matrix) { if(matrix == null || matrix.length = ...