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

这个程序有点问题,没有解决的。。。

时间: 2024-09-29 09:15:27

lintcode 容易题:Reverse Words in a String 翻转字符串的相关文章

[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:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o

[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",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a

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

给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用O(1) 时间复杂度的原地解法.说明:    什么构成一个词?    一系列非空格字符组成一个词.    输入字符串是否可以包含前导或尾随空格?    是.但是,您的反转字符串不应包含前导或尾随空格.    两个单词之间多空格怎么样?    将它们缩小到反转字符串中的单个空格.详见:https://leetc

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

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. click to show clarification. Cl

Reverse Words in a String(翻转字符串)

给定一个字符串,逐个翻转字符串中的每个单词. Given s = "the sky is blue",return "blue is sky the". 单词的构成:无空格字母构成一个单词 输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括 如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个 思路: 1.定义一个新的字符串str 接收原字符串删除首尾空字符后的字符串 trim(): 2.定义字符串tmp 存储遍历字符串时的每个单词:

[CareerCup] 1.2 Reverse String 翻转字符串

1.2 Implement a function void reverse(char *str) in C or C++ which reverses a null-terminated string. 这道题让我们用C++或C语言来翻转一个字符串,不算一道难题,在之前那道Reverse Words in a String 翻转字符串中的单词中用到了这个函数,跟那道题比起来,这题算简单的了.C语言的版本要比C++的稍微复杂一些,应为string类集成了很多有用的功能,比如得到字符串的长度,用下标

Leetcode 151题 Reverse Words in a String

时间:2014.05.10 地点:基地 心情:准备刷下Leetcode题了,就从第151题开始吧 ------------------------------------------------------------------------------------------ 一.题目 Reverse Words in a String Given an input string, reverse the string word by word. For example, Given s =

Reverse Words in a String 翻转一个字符串里的单词顺序 @LeetCode

LeetCode新题,但是比较简单,直接用栈即可 Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". click to show clarification. Clarification: What constitutes a word?A sequence of non-sp

leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)

题目如下: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters c