【LeetCode】557. Reverse Words in a String III

问题描述

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

解决方法

//方法1
class Solution
{
    public static String reverseWords(String s)
    {
        int start = 0;
        int end;
        char[] c = s.toCharArray();
        String s1 = "";
        for(int i=0; i<s.length(); i++)
        {
            if(c[i] == ' ')
            {
                end = i-1;
                reverse(c, start, end);
                start = i+1;
            }
            if(i == c.length - 1)
            {
                end = i;
                reverse(c, start, end);
            }
        }
        return  new String(c);
    }
    private void reverse(char[] a, int start, int end)
    {
        for(; start<end; start++,end--)
        {
            char tmp = a[start];
            a[start] = a[end];
            a[end] = tmp;
        }
    }
}

/*方法2:先将字符串按照空格进行分割为字符串数组,然后将每个字符串作为参数传到新建的StringBuilder对象中,之后求其逆串,最后转为String。然后使用append加上空格,最后使用trim消除多余的空格。*/
class Solution
{
    public String reverseWords(String s) 
    {
            String[] str = s.split(" ");
            for (int i = 0; i < str.length; i++) str[i] = new StringBuilder(str[i]).reverse().toString();
            StringBuilder result = new StringBuilder();
            for (String st : str) result.append(st + " ");
            return result.toString().trim();
    } 
}

//方法3,和方法1类似
class Solution
{
    public String reverseWords(String s) 
    {
        char[] s1 = s.toCharArray();
        int i = 0;
        for(int j = 0; j < s1.length; j++)
        {
            if(s1[j] == ' ')
            {
                reverse(s1, i, j - 1);
                i = j + 1;
            }
        }
        reverse(s1, i, s1.length - 1);
        return new String(s1);
    }

    public void reverse(char[] s, int l, int r)
    {
        while(l < r)
        {
            char temp = s[l];
            s[l] = s[r];
            s[r] = temp;
            l++; r--;
        }
    }
}   

//方法4,和方法1类似
public class Solution 
{
    public String reverseWords(String s) {
        char[] ca = s.toCharArray();
        for (int i = 0; i < ca.length; i++) {
            if (ca[i] != ' ') {   // when i is a non-space
                int j = i;
                while (j + 1 < ca.length && ca[j + 1] != ' ') { j++; } // move j to the end of the word
                reverse(ca, i, j);
                i = j;
            }
        }
        return new String(ca);
    }

    private void reverse(char[] ca, int i, int j) {
        for (; i < j; i++, j--) {
            char tmp = ca[i];
            ca[i] = ca[j];
            ca[j] = tmp;
        }
    }
}

出现的问题

以下程序提交出现超时的错误:
Submission Result: Time Limit Exceeded

class Solution
{
    public static String reverseWords(String s)
    {
        int start = 0;
        int end;
        char[] c = s.toCharArray();
        String s1 = "";
        for(int i=0; i<s.length(); i++)
        {
            if(c[i] == ' ')
            {
                end = i-1;
                for(int j=end; j>=start; j--)
                    s1 += c[j];
                s1 += " ";
                start = i+1;
            }
            if(i == c.length - 1)
            {
                end = i;
                for(int j=end; j>=start; j--)
                    s1 += c[j];
            }
        }
        return  s1;
    }
}

原因:http://blog.csdn.net/GSH_Hello_World/article/details/78599162

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大师兄电子工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值