[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 characters.
The input string does not contain leading or trailing spaces and the words are always separated by a single space.
For example,
Given s = "the sky is blue",
return "blue is sky the".
Could you do it in-place without allocating extra space?
151. Reverse Words in a String 一样,这里要求不能用额外空间,要用in-place完成。
该题假设开头和结尾没有空格,而且单词之间只有一个空格。不需要这些假设也是可以的,就是代码会比较复杂。
思路就是两步走,第一步就是将整个字符串翻转。然后从头逐步扫描,将每个遇到单词再翻转过来。
[注意事项]
1)如果是Java,应该跟面试官指出String是immutable,所以需要用char array来做。
2)follow-up问题:k-step reverse。也就是在第二部翻转的时候,把k个单词看作一个长单词,进行翻转。
Java:
public void reverseWords(char[] s) {
reverse(s, 0, s.length);
for (int i=0, j=0; j<=s.length; j++) {
if (j==s.length || s[j]==' ') {
reverse(s, i, j);
i = j + 1;
}
}
} private void reverse(char [] s, int begin, int end) {
for (int i=0; i<(end-begin)/2; i++) {
char temp = s[begin+i];
s[begin+i] = s[end-i-1];
s[end-i-1] = temp;
}
}
Python:
class Solution(object):
def reverseWords(self, s): def reverse(s, begin, end):
for i in xrange((end - begin) / 2):
s[begin + i], s[end - 1 - i] = s[end - 1 - i], s[begin + i] reverse(s, 0, len(s))
i = 0
for j in xrange(len(s) + 1):
if j == len(s) or s[j] == ' ':
reverse(s, i, j)
i = j + 1
C++:
class Solution {
public:
void reverseWords(string &s) {
int left = 0;
for (int i = 0; i <= s.size(); ++i) {
if (i == s.size() || s[i] == ' ') {
reverse(s, left, i - 1);
left = i + 1;
}
}
reverse(s, 0, s.size() - 1);
}
void reverse(string &s, int left, int right) {
while (left < right) {
char t = s[left];
s[left] = s[right];
s[right] = t;
++left; --right;
}
}
};
类似题目:
[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
[LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
All LeetCode Questions List 题目汇总
[LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II的更多相关文章
- [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 ...
- [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(翻转字符串中的单词)
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- 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 ...
- [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 ...
- [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] 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 ...
- Leetcode - 186 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-s ...
随机推荐
- django-带参数路由
路由urls.py from django.conf.urls import url from goods.views import IndexView, DetailView, ListView u ...
- C#笔记2 —常量
基本上和c语言中的常量类似,但有区别 在const关键字的基础上,添加了readonly,readonly关键字在笔记中说明. 常量是固定值,程序执行期间不会改变.常量可以是任何基本数据类型,比如整数 ...
- SUID提权
查看tmp目录权限 ll -d /tmp 切换到tmp目录 cd /tmp 创建一个exploit目录 mkdir exploit 查看ping命令带suid权限 ll /bin/ping 创建tar ...
- 关于我&声明
声明 本站内容仅作记录,严禁私人用于参考用药或诊断!请遵循医嘱. 访问本站请确保您有一定的医学知识,本人不对任何个人或团体因参考本站文章负法律责任! 关于 医学生,资深玩家. Logo [ Logo ...
- 14-Flutter移动电商实战-ADBanner组件的编写
拨打电话的功能在app里也很常见,比如一般的外卖app都会有这个才做.其实Flutter本身是没给我们提供拨打电话的能力的,那我们如何来拨打电话那? 1.编写店长电话模块 这个小伙伴们一定轻车熟路了, ...
- ASP.net MVC C# 当前上下文中不存在名称"viewbag"
出现的错误如下: 错误 2 当前上下文中不存在名称“model” e:\Stuff\projects\蓝狐软件工作室\src\Lanhu.Admin\Views\Student\Index.cshtm ...
- S1_搭建分布式OpenStack集群_03 Mysql、MQ、Memcached、ETCD安装配置
一.安装mysql(contorller)controller ~]# yum -y install mariadb mariadb-server python2-PyMySQL 配置my.cnf文件 ...
- 7kyu kata
https://www.codewars.com/kata/isograms/train/java CW 大神 solution: public class isogram { public stat ...
- PowerDesigner 画流程图
原因: 以前赶时间写了n长一个类,现在又增加新需求了,but以前怎么写的忘了,虽然注释都有,一个一个方法的看很累,准备把它用面向对象改造一下,不知道时间够不,先试一试在说.之前设计数据库用的Power ...
- [RN] React Native 使用 react-native-vector-icons 图标显示问号
我在第一次使用 react-native-vector-icons 时图标显示问号 后来在网上查了很多文章,发现原因有两个 1)安装完 react-native-vector-icons 后,没有li ...