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 ...
随机推荐
- 利用PowerDesigner绘制PDM生成SQL Server数据库
PowerDesigner是个很强大的建模工具,可以利用它绘制各种图形,本文利用该工具绘制PDM,进而生成SQL Server数据库. 比如绘制一个简单的学生选课.教师授课管理系统的PDM: pk表示 ...
- JQuery在iframe中实现 点击后选中当前栏目的样式
二级或者三级折叠菜单参考http://www.cnblogs.com/qigege/p/5178947.html <script type="text/javascript" ...
- MQTT开发小记(一)
最近在协助公司硬件组进行MQTT协议的嵌入式SDK包开发. 简述一下MQTT MQTT简单的来说是一种订阅/发布模式的通信形式,一般分为客户端和服务器端. MQTT服务器端可以简单理解为一个消息中转站 ...
- 创建SQL数据库指定文件路径
create database b2c on primary -- 默认就属于primary文件组,可省略(/*--数据文件的具体描述--*/ name='b2c', -- 主数据文件的逻 ...
- mapreduce 实现矩阵乘法
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...
- Mininet VM设置笔记
Mininet VM是为了加快Mininet安装,而且可以很容易在linux平台上运行. VM运行在Windows,Mac,Linux,通过VMware.VirtualBox,QEMU和KVM. 下载 ...
- 从零开始学ios开发(十九):Application Settings and User Defaults(上)
在iphone和ipad中,有一个东西大家一定很熟悉,那个东西就是Settings. 这次要学习的东西说白了很简单,就是学习如何在Settings中对一个app的某些属性进行设置,反过来,在app中更 ...
- java集合类(三)About Iterator & Vector(Stack)
接上篇:java集合类学习(二) Talk about “Iterator”: 任何容器类,在插入元素后,还需要取回元素,因为这是容器的最基本工作.对于一般的容器,插入有add()相关方法(List, ...
- bzoj 4010: [HNOI2015]菜肴制作 拓扑排序
题目链接: 题目 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory Limit: 512 MB 问题描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴 ...
- ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
引子: 本项目在老电脑上用的是oracle10g,换新电脑装的是oracle11g,但运行项目本没有什么关系,本来说创建个用户,用PLSQL手工导入数据,再改几下配置文件即可跑起来--但实际启动中遇到 ...