151. Reverse Words in a String翻转一句话中的单词
[抄题]:
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翻转一句话中的单词的更多相关文章
- [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& ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- 151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...
- 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [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 ...
- Leetcode151. Reverse Words in a String翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
随机推荐
- LeetCode - Fruit Into Baskets
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- Nginx源码结构及如何处理请求
一.源码结构 1:下载安装包后,解压,可以看到目录结构,其中src目录下放的是源码 2:src源码目录下,可以看到这几个目录 mail:mail目录中存放了实现Nginx服务器 ...
- nginx里proxy_pass有无/的区别
nginx在反向代理的时候,proxy_pass需要指定路径,有无"/"的区别,如下: location /lile { 配置一: proxy_pass http://192. ...
- laravel 路由分組
laravel 路由分組 Route::group(['prefix' => 'admin'], function () { $namespacePrefix="\\App\\Http ...
- docker安装solr集群5.3.1
docker-compose.yml: version: '3' services: zookeeper-A: image: zookeeper:3.4.11 ports: - "12181 ...
- 【WebLogic使用】1.WebLogic的下载与安装
一.WebLogic的介绍 WebLogic是美国bea公司出品的一个application server,确切的说是一个基于Javaee架构的中间件,纯java开发的,最新版本WebLogic ...
- 强制找回gitlab管理员密码
强制找回gitlab管理员密码 最近使用gitlab的时候发现管理员密码忘记,现将找回密码的操作过程记录下来. 1.在gitlab登录窗口 如果密码忘记了登录不进入,可以先尝试点击登录框下方的Forg ...
- MySQL创建数据库与用户
root远程访问授权 mysql> SHOW DATABASES; +--------------------+ | Database | +---------------- ...
- dll文件32位64位检测工具以及Windows文件夹SysWow64的坑【转发】
原文地址:http://www.cnblogs.com/hbccdf/archive/2014/03/09/3590916.html 自从操作系统升级到64位以后,就要不断的需要面对32位.64位的问 ...
- PAT 乙级 1065 单身狗 (25 分)
1065 单身狗 (25 分) “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000),是 ...