leetcode 【 Reverse Words in a String 】python 实现
题目:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
代码:oj在线测试通过 Runtime: 172 ms
class Solution:
# @param s, a string
# @return a string
def reverseWords(self, s):
words = s.split(' ') if len(words) < 2 :
return s tmp = ""
for word in words:
word = word.replace(' ','')
if word != "" :
tmp = word + " " + tmp tmp = tmp.strip() return tmp
思路:
把中间多个空格考虑去除了就OK了
最后用strip()函数把首位的空白去了
leetcode 【 Reverse Words in a String 】python 实现的更多相关文章
- [leetcode]Reverse Words in a String @ Python
原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/ 题意: Given an input string, reverse ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- [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 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 blue ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- [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 解题报告
Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...
- leetcode:Reverse Words in a String【Python版】
class Solution: # @param s, a string # @return a string def reverseWords(self, s): ss = s.split(&quo ...
- LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
随机推荐
- Selenium常见元素定位方法和操作的学习介绍
参考地址: https://www.cnblogs.com/eastmount/p/4810690.html 这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操 ...
- wechat开发笔记之1.接口示例代码
修改后的php示例代码! <?php /** * wechat php test */ //define your token define("TOKEN", "w ...
- JS案例练习-手机微信聊天对话框
先附图 CSS部分: <style> body{} *{;} li{list-style: none;} .container{ width:310px; height:600px; ma ...
- Centos install ICU, INTL for php
1. Install ICU from source wget http://download.icu-project.org/files/icu4c/56.1/icu4c-56_1-src.tgz ...
- PHP代码规范的一些总结
世界第一语言在手,辅以前人的最佳实践,天下又算什么. 1.代码是写给小白用的 注释,注释,注释,重要的事情说三遍.我们做的虽然不是拿去卖源码的商业产品,不需要把注释写的多么优美.但也不要太过吝啬,到头 ...
- HTML和CSS中一些有趣的
CSS Rese http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css <link rel="styleshe ...
- jsop解析获得htmldome
package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...
- LeetCode Merge Two Sorted Lists 归并排序
题意: 将两个有序的链表归并为一个有序的链表. 思路: 设合并后的链表为head,现每次要往head中加入一个元素,该元素要么属于L1,要么属于L2,可想而知,此元素只能是L1或者L2的首个元素, ...
- HDU3973 线段树 + 字符哈希
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3973 , 线段树 + 字符哈希,好题. 又学了一种新的哈希方法,hhhh~ 解法: 想法是用P进制的数 ...
- web跨域及cookie相关知识总结
原文:web跨域及cookie相关知识总结 之前对于跨域相关的知识一致都很零碎,正好现在的代码中用到了跨域相关的,现在来对这些知识做一个汇总整理,方便自己查看,说不定也可能对你有所帮助. 本篇主要 ...