【字符串】Reverse Words in a String(两个栈)
题目:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
思路:
利用两个stack,一个表示单词,一个表示句子。当遇到非空格字符时放入单词stack;当遇到空格时将单词stack中的字符压入句子stack中(注意:单词此时已经逆序一次),然后仅添加一个空格。最后将句子stack依次输出,此时句子逆序。
/**
* @param {string} str
* @returns {string}
*/
var reverseWords = function(str) {
var word=[],res=[]; for(var len=str.length,i=len-1;i>=0;){
while(i>=0&&str[i]==" "){
i--;
}
if(i<0){
break;
}
if(res.length!=0){
res.push(" ");
}
word.length=0;
while(i>=0&&str[i]!=" "){
word.push(str[i--]);
}
for(var jlen=word.length,j=jlen-1;j>=0;j--){
res.push(word[j]);
} }
return res.join("")
};
【字符串】Reverse Words in a String(两个栈)的更多相关文章
- [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [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 ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words in a String
公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 1: 输入: ...
- [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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编
在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编 ...
- [Swift]LeetCode186. 翻转字符串中的单词 II $ Reverse Words in a String II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- [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& ...
随机推荐
- UVa 11384 Help is needed for Dexter (递归)
题意:给定一个n表示1到n的序列,让你用最小的步数把这个序列都变为0,每个操作可以从序列中选择一个或多个个,同时减掉一个正整数,求最少的步数. 析:一看这个题,感觉挺高深的,但是静下心来想想,其实挺简 ...
- vivado用法
声明为”DEBUG”,即使没有连接到其他模块,也不会被优化掉.但并不是所有的信号都是在声明为“debug”属性之后就不会优化掉. (2)同一个bank中能设置一个电平. (3)
- new Date()之参数传递
JavaScript下,new Date([params]),参数传递有以下五种方式: 1.new Date("month dd,yyyy hh:mm:ss"); 2.new Da ...
- application cache 应用缓存
这些应用还是要自己实现一遍,否则真不知道哪里会出问题. 客户端: <!DOCTYPE html> <html manifest = 'demo.appcache'> <h ...
- 磁盘配额(Quota)的应用与实践
1>什么是Quota 在Linux中,由于是多用户,多任务的环境,所以会有多用户共同使用一个硬盘空间的情况发生,如果其中有少数几个用户大量占用掉了硬盘空间的话,那肯定影响其他用户的使 ...
- Oracle SQL Trace 和 10046 事件
http://blog.csdn.net/tianlesoftware/article/details/5857023 一. SQL_TRACE 当SQL语句出现性能问题时,我们可以用SQL_TRAC ...
- Android 一个应用多个桌面图标
理解android.intent.action.MAIN 与 android.intent.category.LAUNCHER: 在Android 应用程序开发过程中,Activity入口会增加: a ...
- [ACM_动态规划] UVA 12511 Virus [最长公共递增子序列 LCIS 动态规划]
Virus We have a log file, which is a sequence of recorded events. Naturally, the timestamps are s ...
- JS里关于特殊字符的转义
重定向的url里含有百分号“%”,遇到了apache 找不到该文件的报错.通过查询相关文档,知道了原来是url里含有特殊字符要转码才能定位到正确的地址.比如"%"要转码为" ...
- Office - Outlook
将邮件存到本地 服务器容量有限,避免丢失和经常提示容量不足 步骤 在File->Account Settings->Account Settings下面 在Data Files标签页新建一 ...