[LeetCode]题解(python):151-Reverse Words in a String
题目来源:
https://leetcode.com/problems/reverse-words-in-a-string/
题意分析:
给定一个字符串,里面包括多个单词,将这个字符串的单词翻转,例如"the sky is blue",得到"blue is sky the".
题目思路:
首先获取每个单词,将单词记录到一个数组里面,然后翻转过来就行了。要处理的是前面和后面有空格的情况。
代码(python):
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
if len(s) == 0:
return ""
ans = []
begin = i = 0
mark = True
while i < len(s):
if s[i] != ' 'and mark:
begin = i
mark = False
if s[i] == ' ' and i > 0 and s[i - 1] != ' ':
ans.append(s[begin:i])
while i < len(s) and s[i] == ' ':
i += 1
begin = i
i += 1
if s[-1] != ' ':
ans.append(s[begin:len(s)])
#print(ans)
j = len(ans)
if j == 0:
return ""
res = ans[j - 1]
j -= 1
while j > 0:
res += ' ' + ans[j - 1]
j -= 1
return res
[LeetCode]题解(python):151-Reverse Words in a String的更多相关文章
- 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] 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】151 Reverse Words in a String
Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- Java for 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 151. Reverse Words in a String --------- java
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [leetcode]151. Reverse Words in a String翻转给定字符串中的单词
Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...
- LeetCode 151 reverse word in a string
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
随机推荐
- JS判断表单内容是否更改过
1,根据具体标签判断 function JudgesubmitForm() { var judjeWs = false; var judjeAt = false; var judjeWd = fals ...
- HDU 1328 IBM Minus One
IBM Minus One Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- jquery精简选项卡
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8&qu ...
- Linux PCI网卡驱动的详细分析
学习应该是一个先把问题简单化,在把问题复杂化的过程.一开始就着手处理复杂的问题,难免让人有心惊胆颤,捉襟见肘的感觉.读Linux网卡驱动也是一 样.那长长的源码夹杂着那些我们陌生的变量和符号,望而生畏 ...
- jQuery随记
每次申明一个jQuery对象的时候,返回的是jQuery.prototype.init对象,很多人就会不明白,init明明是jQuery.fn的方法啊,实际上这里不是方法,而是init的构造函数,因为 ...
- PBOC/EMV 中SDA和DDA简介
PBOC/EMV里有两个非常重要的概念,SDA(static data authentication)和DDA(dynamic data authentication),分别叫做静态数据认证和动态数据 ...
- 银行卡检测中心BCTC
BCTC是Banking Card Test Center的缩写. 银行卡检测中心(下称中心)经中国人民银行总行批准成立于1998年4月,作为一个独立的第三方专业技术检测机构,其主要职责是按照国际.国 ...
- MATLAB介绍
MATLAB MATLAB[1] 是美国MathWorks公司出品的商业数学软件,用于算法开发.数据可视化.数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink ...
- C++ 文件的复制、删除、重命名
一.文件的复制 #include <iostream>#include <fstream>using namespace std; int CopyFile(char *Sou ...
- Unity2D屏幕适配方案
看了cnblogs里的一篇文章,终于理解了Unity2D的摄像机系统:http://www.cnblogs.com/flyFreeZn/p/4073655.html 我根据他的方案,改写了两种适配方案 ...