lintcode :Reverse Words in a String 翻转字符串
题目:
给定一个字符串,逐个翻转字符串中的每个单词。
给出s = "the sky is blue",返回"blue is sky the"
- 单词的构成:无空格字母构成一个单词
- 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括
- 如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个
解题:
这个题目方法很多的
1.整体反转,对每个单词再反转,但是中间的多个空格还有单独处理
2.把后面的单词,拿到前面去。参考程序
Java程序:
public class Solution {
/**
* @param s : A string
* @return : A string
*/
public String reverseWords(String s) {
// write your code
if(s==null || s.length() == 0)
return "";
String[] array = s.split(" ");
StringBuilder sb = new StringBuilder();
for(int i = array.length - 1;i>=0 ;--i){
if(!array[i].equals("")){
sb.append(array[i]).append(" ");
}
}
return sb.length()==0?"":sb.substring(0,sb.length() - 1);
}
}
总耗时: 1929 ms
网站今天增加好几道新题,然后我提交一次Pending。。。
Python程序:
class Solution:
# @param s : A string
# @return : A string
def reverseWords(self, s):
# write your code here
begin = 0
end = 0
while end< len(s):
if s[end] == ' ':
self.swap(s,begin,end - 1)
begin = end + 1
end = begin
else:
end += 1
# print s
self.swap(s,begin,end - 1)
# print s
self.swap(s,0,len(s) - 1)
# print s
return s def swap(self,s,begin,end):
while begin< end:
tmp = s[begin]
s[begin] = s[end]
s[end] = tmp
begin +=1
end -=1
这个程序有点问题,没有解决的。。。
lintcode :Reverse Words in a String 翻转字符串的更多相关文章
- [LintCode] 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 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 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- 151 Reverse Words in a String 翻转字符串里的单词
给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...
- [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 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- Leetcode151. Reverse Words in a String翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...
- 345. Reverse Vowels of a String翻转字符串中的元音字母
[抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- Reverse Word in a String(翻转字符串)&字符串最后一个单词的长度
1.题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...
随机推荐
- MAC上 nodejs express 安装
最近在MAC上搭建 nodejs环境以及安装 express 框架,遇到了一些问题,不过最后总算还是安装成功了,下面是操作步骤 1.node js 安装 访问nodejs官网进入下载mac上的安装包 ...
- json string 与object 之间的转化
1.将json string转化成object 1: public static T GetObjectFromJson<T>(string jsonString) 2: { 3: Dat ...
- Wpf从资源中重用UI元素
在我的界面上有几个选项卡,每个选项卡中都有下面的元素: <StackPanel Orientation="Horizontal"> <Button Content ...
- django 更新model
修改models.py 中对应的class 在admin.py 中 增加 admin.site.register(WafDevice) 进入dbshell python manage.py dbshe ...
- UML 小结(1)- 整体阐述
前言: UML( Unified Modeling Language) 又称统一建模语言或标准建模语言,是始于1997年一个OMG标准,它是一个支持模型化和软件系统开发的图形 ...
- SlimDx绘制点图元的问题
问题:点图元在自己创建的三维环境里渲染不出来,代码如下: GMCustomVertex.PositionNormalColored wellPart = new GMCustomVertex.Posi ...
- 复习linq
复习linq linq的英文是language integrated query.其中query的意思就是疑问或者计算机用语就是从资料库中提取信息的要求,可以理解为查询的意思.那么它翻译过来的话就是集 ...
- java url中文参数乱码问题
http://www.blogjava.net/jerry-zhaoj/archive/2009/07/16/286993.html 转 JAVA 中URL链接中文参数乱码的处理方法JAVA 中URL ...
- 3223: Tyvj 1729 文艺平衡树 - BZOJ
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- python 安装 easy_intall 和 pip python无root权限安装
http://www.cnblogs.com/haython/p/3970426.html easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安装e ...