[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 ...
随机推荐
- JavaScript经典代码总结
1. oncontextmenu="window.event.returnvalue=false" 将彻底屏蔽鼠标右键<table border oncontextmenu= ...
- iOS切换window根控制器 (转)
转自linfengwenyou 在运行过程中更改根控制器的方法:(假设:A为当前根控制器,B为要设的根控制器) 方法一: 1. appdelegate.m中 self.window = [[UIWin ...
- 全站 HTTPS
1.HTTPS 基础 HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议 它是一个安全通信通道,它基于HTTP开发,用于在客户计算机和服务器之间交换信 ...
- Android 修改toast的默认位置和获取当前屏幕的高度和宽度
Toast toast; toast=Toast.makeText(this, "toast", Toast.LENGTH_LONG); toast.setGravity(grav ...
- highcharts:根据Y的数值范围,动态改变图形的填充颜色
图形实例: 源代码如下: <!DOCTYPE html><html><head><meta charset="utf-8">&l ...
- distance.c
#include "stdio.h" #include "string.h" #include "math.h" #include &quo ...
- JQuery DataTables Editor---页面内容修改&&数据库信息修改 (2)
接上篇博文,详细说一下js代码以及JQuery DataTables Editor---页面内容修改&&数据库信息修改遇到的问题和解决办法. 1.关于dialog 初始化: $(&qu ...
- MYSQ 查看 2 进制日志
方法 1: myqlbinlog filename; ------------------------------------------------------------------------- ...
- ubuntu下的notepad++
安装方法: 终端输入命令:sudo apt-get install scite 安装完成后dash中输入scite查找已经安装的scite,拖动到桌面快捷方式.
- jenkins中Deploy to container Plugin插件发布配置
参数详解: 第一项(WAR/EAR files):是war包的相对路径(相对于工作区路径,即在工作区中war包的相对路径.)如我的maven执行完成之后会在工作区的target目录下生成项目.war, ...