[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 still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
151. Reverse Words in a String ,186. Reverse Words in a String II 的类似题目,这题是让翻转字符串里的单词。
Python:One Place
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] s, i = list(s), 0
for j in xrange(len(s) + 1):
if j == len(s) or s[j] == ' ':
reverse(s, i, j)
i = j + 1
return "".join(s)
Python:New Array
class Solution2(object):
def reverseWords(self, s):
reversed_words = [word[::-1] for word in s.split(' ')]
return ' '.join(reversed_words)
C++:
class Solution {
public:
string reverseWords(string s) {
for (int i = 0, j = 0; j <= s.length(); ++j) {
if (j == s.length() || s[j] == ' ') {
reverse(s.begin() + i, s.begin() + j);
i = j + 1;
}
}
return s;
}
};
类似题目:
[LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
[LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II
All LeetCode Questions List 题目汇总
[LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III的更多相关文章
- [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 ...
- [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(翻转字符串中的单词)
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- [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 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] 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#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- C#版(击败97.76%的提交) - Leetcode 557. 反转字符串中的单词 III - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- Java实现 LeetCode 557 反转字符串中的单词 III(StringBuilder的翻转和分割)
557. 反转字符串中的单词 III 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode c ...
随机推荐
- luoguP1120小木棍(POJ - 1011 )
题意: 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50,个数不超过65. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段 ...
- 使用Apache commons-maths3-3.6.1.jar包进行简单的数据统计分析(java)
使用maths3函数进行简单的数据统计性描述: 使用场景:本地,直接运行就可以: 具体后面有个性化的需求,可以再修改~ package com; import org.apache.commons.l ...
- django-带参数路由
路由urls.py from django.conf.urls import url from goods.views import IndexView, DetailView, ListView u ...
- 浏览器报400-Bad Request异常
今天在使用ie浏览器在测试程序的时候,报这个错误,后台日志打印出来显示的是:连接一个远程主机失败 解决Invalid character found in the request target. Th ...
- 宽带DOA估计方法
Wideband DOA Estimation. 语音信号以及野外的车辆信号的声音都是宽带信号,所以传统的窄带DOA算法(MUSIC,ESPRIT等)都不适用.需要采用宽带DOA算法来计算目标信号的波 ...
- LeetCode 838. Push Dominoes
原题链接在这里:https://leetcode.com/problems/push-dominoes/ 题目: There are N dominoes in a line, and we plac ...
- LeetCode 958. Check Completeness of a Binary Tree
原题链接在这里:https://leetcode.com/problems/check-completeness-of-a-binary-tree/ 题目: Given a binary tree, ...
- 关于new FormData() 对象的用法
formData.append() 理论上本身若键值已经存在,那么我们append的数据是进行类似push的操作,为了匹配php,我们进行加了[] ,这个操作.!
- bg/fg
将一个在后台暂停的命令,变成继续执行 (在后台执行). 一般ctrl+z就把前台命令调到了后台 将后台中的命令调至前台继续运行
- CSS文本元素
一.属性 font-size:16px; 文字大小 Font-weight: 700 ; 值从100-900,文字粗细,不推荐使用font-weight:bold; Font-family:微软 ...