题目:

给定一个字符串,逐个翻转字符串中的每个单词。

样例

给出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 翻转字符串的更多相关文章

  1. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  2. [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 ...

  3. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  4. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  5. [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& ...

  6. 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...

  7. Leetcode151. Reverse Words in a String翻转字符串里的单词

    给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...

  8. 345. Reverse Vowels of a String翻转字符串中的元音字母

    [抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  9. Reverse Word in a String(翻转字符串)&字符串最后一个单词的长度

    1.题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...

随机推荐

  1. EasyUI_Datagrid学习总结

    EasyUI_Datagrid学习总结 2016年7月25日星期一 一.简介 Easyui中的datagrid从总的作用上讲,就是在列表上显示数据,类似于table,但是在table的基础上,此控件更 ...

  2. uniform 中checkbox通过jquery 选中

    你是否曾经为不能修改多选框.单选框.文件选择框的样式而郁闷呢,是否想过控制它们的样式且兼容所有浏览器呢?我现在给你推荐的这个jQuery表单美化插件Uniform就可以解决这些问题. Uniform可 ...

  3. javascript中的光标

    最近项目中要做一个键盘操作,光标移动的功能:增强用户体验:问朋友查资料了解到这方面的知识:整理备忘: 1.IE使用textRange对象,其他使用selectionStart selectionEnd ...

  4. 【转】MyEclipse快捷键大全(绝对全)

    ref:http://www.douban.com/note/254195820/?type=like# ctrl+w 关闭单个窗口 F3 跳转到类.变量的声明 F11 运行上次程序 Ctrl + F ...

  5. 浅析Mysql 数据回滚错误的解决方法

    介绍一下关于Mysql数据回滚错误的解决方法.需要的朋友可以过来参考下 MYSQL的事务处理主要有两种方法.1.用begin,rollback,commit来实现begin 开始一个事务rollbac ...

  6. c# datatable list 相互转换

    /*Converts List To DataTable*/ public static DataTable ToDataTable<TSource>(IList<TSource&g ...

  7. 1101. Quick Sort (25)

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  8. Linux 挂载2T以上存储

    在生产环境中,我们会遇到分区大于2T的磁盘(比如:添加一个3TB的存储),由于MBR分区表只支持2T磁盘,所以大于2T的磁盘必须使用GPT分区表 而fdisk是不支持GPT分区的,我们可以使用part ...

  9. PHP中日期时间函数date()用法总结

    date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...

  10. js之正则表达式(上)

    1.正则表达式的创建方式 两种方式创建:通过new修饰符创建和字面量的方式创建 1>new修饰符方式创建 var b2=new RegExp('Box','ig'); //第二个参数是 模式字符 ...