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 ...
随机推荐
- 如何在java类中读取Properties配置文件
在com.example包下有一个test.properties文件和测试类PropertyReadTest.java. test.properties 文件内容: author=zeige tea ...
- mvc涉及到input设置了disabled
在做网站管理后台的用户修改功能时,由于当前用户修改个人信息时规定用户名不能修改,故使用了input标签的disabled属性,但是在提交数据后却发现用户名显示为空了.后来一查才知道input设置为di ...
- sublime text3的配置(整理)
一.代码片段 开发人员很多时候是在做一些重复的工作. 针对不同数据表的增删改查都差不多,重复来重去的.很久不写程序了,利用十一假期在家看看书,写写程序. 最近一直很喜欢使用Sublime Text,发 ...
- easyui获取一行数据和修改data-options的值
<table id="tab" class="easyui-datagrid" style="width: 100%; height: 500p ...
- hadoop自动安装的脚本与步骤
最近要在10几台机器上安装hadoop.对于这种繁复而重复的工作,一步步的打命令行,对于程序员来说是一件不能忍的事情.所以我就琢磨着怎么写一个脚本来自动安装hadoop. 任务: 在10几台机器上中的 ...
- 原创的基于HTML/CSS/JavaScript的层级目录树
之前参加过一些基于HTML/CSS/JavaScript的项目,当在页面中需要生成一颗目录树时,总是首先想着网上有没有现成的生成树的源代码,比如dtree.zthee,或者使用一些javascript ...
- Hadoop命令摘录
一:文件操作 1.建立目录 [hadoop@hadoop1:hadoop]$bin/hadoop dfs -mkdir testdir 在HDFS中建立一个名为testdir的目录 2.上传文件到HD ...
- winrar 压缩文件方法
问题描述: 我要一些大的文件进行压缩,看了网上有很多类拟的写法.但在我这都存在两个问题. 1.就是他们都是通过注册表找到rar的exe目录.我安装好winrar后,虽然注册表有,但那个目录一直报一个错 ...
- C#中DataTable与实体集合通用转换(使用扩展方法)
本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态 ...
- ThinkPHP学习笔记 实例化模型的四种方法
创建Action类 [php] <?php class NewObjectAction extends Action{ public function index(){ ...