题目:

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

样例

给出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. HTML表单样式

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  2. js根据IP取得天气

    <span id="weather"></span> <script> function weather(cityName) { var cha ...

  3. SOAP 介绍

    简介 SOAP(Simple Object Access Protoco)简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于 XML 的协议.此协议规范由 IBM.Microso ...

  4. Oracle 表的访问方式(1) ---全表扫描、通过ROWID访问表

    1.Oracle访问表的方式 全表扫描.通过ROWID访问表.索引扫描 2.全表扫描(Full Table Scans, FTS) 为实现全表扫描,Oracle顺序地访问表中每条记录,并检查每一条记录 ...

  5. 1049. Counting Ones/整数中1出现的次数(从1到n整数中1出现的次数)

    The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...

  6. [原创]PostgreSQL Plus Advince Server在 HA环境中一对多的Stream Replication配置(二)

    三.配置主机与备机的ssh无密码登录1.主机s1到备机s3的无密码登录a.创建ssh目录[root@s1 ~]# mkdir /opt/PostgresPlus/9.2AS/.sshb.修改ssh目录 ...

  7. Java 图形编程 二:布局管理器之顺序布局

    package second; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.Window ...

  8. 从零开始学ios开发(十一):Tab Bars和Pickers

    不好意思各位,本人休息了一个礼拜,所以这次的进度延后了,而且这次的学习的内容比较多,时间用的也比较长,文章发布的时间间隔有些长了,望各位谅解,下面继续我们的ios之旅. 这次我们主要学习的内容有2个, ...

  9. ASP.NET 运行机制续(完结)

    上一篇说到applicationInstance会执行一些列的事件.下面是我在msdn上找到有关asp.net程序生命周期相关的描述及图片 声明周期的起始 ASP.NET 应用程序的生命周期以浏览器向 ...

  10. Animations--动画基础

    基础动画 //1.在0.5s内,将_field.alpha 的数值变为1.0 [UIView animateWithDuration:0.5 animations:^{ _field.alpha = ...