LeetCode OJ1: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
".
解题思路:
先利用split()方法将句子按空格分为几个单词的列表,
然后reverse()函数使列表逆序,
最后join函数以空格为间隔拼成字符串返回结果。
Python代码:
class Solution:
# @param s, a string
# @return a string
def reverseWords(self, s):
a = s.split()
a.reverse()
return ' '.join(a)
LeetCode OJ1: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] 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] 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#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- leetcode - [1]Reverse Words in a String
Question: Reverse Words in a String Given an input string, reverse the string word by word. For exam ...
- LeetCode 345. Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels(元音字母) of a string. Example ...
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
随机推荐
- Node.js之NPM工具使用
1.NPM介绍:包管理工具 (1)允许用户从NPM服务器下载别人编写的第三方包到本地石使用 (2)允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用 (3)允许用户将自己编写的包或命令行程 ...
- 1. Activiti 运行时表信息总结
Activiti的后台是有数据库的支持,所有的表都以ACT_开头. 第二部分是表示表的用途的两个字母标识. 用途也和服务的API对应. ACT_RE_*: 'RE'表示repository. 这个前缀 ...
- 使用VisualVM分析性能
性能分析神器VisualVM VisualVM 是一款免费的,集成了多个 JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成和分析海量 ...
- Ajax基础(小猫)
Ajax 1.什么是Ajax: 不用刷新整个页面便可与服务器通讯的办法 2.Ajax的基本使用 2.1XMLHttpRequest l XMLHttpRequest对象 XMLHttpRequest是 ...
- sort命令
1.默认情况下,sort命令,以字母序进行文本排序sort word.txt2.如果想对数字进行排序,可以使用-n参数sort num.txt -n3指定列排序下面是对passwd文件,以冒号(:)进 ...
- 两种让tableview返回顶部的方法
1. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_currentRow inSection:0] animat ...
- jquery基本
对于jquery属性的访问: //对于bool值的属性,元素标签中如果写了这个属性,attr能够获取到,如果没有写,就获取不到. 如:<input type="checkbox&quo ...
- es6转es5
一.在没有IDE情况下的转换 在"我的电脑->D盘”新建个文件夹es6,文件夹里新建一个文件es6.js. 打开命令行窗口 1.先全局安装babel-cli,输入命令 npm inst ...
- 正则表达式在python中的应用
一.常用符号 . :匹配任意字符,换行符\n除外 * :匹配前一个字符0次或无限次 ? :匹配前一个字符0次货1次 .* :贪心算法 .*? :非贪心算法 () :括号内的数据作为结果返回 ...
- 一次sql排序的问题。
select date, count(fail) as fail,count(win) as win from (select date,(case (result) when 'fail' then ...