【LeetCode】151. Reverse Words in a String
Difficulty: Medium
More:【目录】LeetCode Java实现
Description
Given an input string, reverse the string word by word.
Example:
Input: "the sky is blue
",
Output: "blue is sky the
".
Note:
- A word is defined as a sequence of non-space characters.
- Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
- You need to reduce multiple spaces between two words to a single space in the reversed string.
Follow up: For C programmers, try to solve it in-place in O(1) space.
Intuition
Use two pointers, startint from the end to the begining of the string to get each word.
Make use of StringBuilder to append each word.
Solution
public String reverseWords(String s) {
if(s==null || s.length()==0)
return s;
StringBuilder sb= new StringBuilder();
int j=s.length();
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)==' ')
j=i;
else if(i==0 || s.charAt(i-1)==' '){
if(sb.length()!=0)
sb.append(" ");
sb.append(s.substring(i,j));
}
}
return sb.toString();
}
Complexity
Time complexity : O(n).
Space complexity : O(n)
What I've learned
1. There is only one space between two words in the reversed string.
2. Learn how to use two pointers to get each word.
More:【目录】LeetCode Java实现
【LeetCode】151. Reverse Words in a String的更多相关文章
- 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【刷题-LeetCode】151 Reverse Words in a String
Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...
- 【leetcode】345. Reverse Vowels of a String
problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...
- 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...
- 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【leetcode】Find All Anagrams in a String
[leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...
随机推荐
- 【ARC076D/F】Exhausted?
Description 题目链接 Solution 场上尝试使用优化建图网络流实现,结果T到怀疑人生. 鉴于这是个匹配问题,考虑用贪心做一下. 先退一步,想一下如果每一个人只有\([1 ...
- aws上部署zabbix3.4
三台机器 10.0.0.149 AmazonLinux2.0 zabbix-server zabbix-agent 10.0.1.61 CentOS6.9 zabbix-agent 10.0.1.11 ...
- SpringBoot整合Swagger-ui
SpringBoot整合Swagger-ui 引入依赖 <dependency> <groupId>org.springframework.boot</groupId&g ...
- bzoj 2275: [Coci2010]HRPA
据说叫斐波那契博弈. 先手最少取的石子数是把n用斐波那契数列拆分后最小的数. 原题+证明: http://blog.csdn.net/acm_cxlove/article/details/783501 ...
- c++并发编程之线程的互斥与同步
什么是线程的同步与互斥? 互斥:指在某一时刻指允许一个进程运行其中的程序片,具有排他性和唯一性. 对于线程A和线程B来讲,在同一时刻,只允许一个线程对临界资源进行操作,即当A进入临界区对资源操作时,B ...
- dwr网上使用例子
转: dwr实现聊天室功能 2016年01月15日 10:22:43 我爱喝可乐 阅读数:564 用dwr的comet(推)来实现简单的无刷新多人聊天室,comet是长连接的一种.通常我们要实现无 ...
- python xml.etree.ElementTree模块
使用的XML文件如下:file.xml <?xml version="1.0"?> <data name="ming"> <cou ...
- unity常用小知识点
感觉自己抑郁变得更严重了,超级敏感,经常想崩溃大哭,睡眠超差,实在不想药物治疗,多看看书,多约约朋友,多出去走走. 来几句鸡汤吧,人一定要活得明白一点,任何关系都不要不清不楚,说不定最后受伤的就是自个 ...
- 20181105 Timer(慕课网)
定时任务调度 基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务 Java中的定时调度工具 Timer JDK提供,不许引入 功能简单,能用Timer尽量用 Quartz 需要引入 功能 ...
- Zephir入门教程一
一.如何安装 zephir-安装和初体验:http://blog.csdn.net/u011142688/article/details/51619811 二.如何使用 需要切到工作目录下,也就是co ...