LeetCode 151. Reverse Words in a String(java)

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".
思路:从后向前走,然后遍历到第i个的时候,判断第i个和第i-1个的关系,从而进行不同的操作,最后一个case 4的代码是LeetCode可以跑过的代码。
case 1: no leading space, no tailing space, no continuous inner space

public String reverseWords(String s) {
        //special case
        if (s.length() == 1) return "";
        //general case
        StringBuilder sb = new StringBuilder();
        int end = s.length();
        for (int i = s.length() - 1; i >= 0; i--) {
            if (s.charAt(i) == ' ') {
                sb.append(s.substring(i + 1, end));
                sb.append(" ");
                end = i;
            }
        }
        sb.append(s.substring(0, end));
        return sb.toString();
}
case2: no leading space, no tailing space, has continuous inner space

public String reverseWords(String s) {
        //special case
        if (s.length() == 0) return "";
        if (s.length() == 1) {
            if (s.equals(" ")) return "";
            else return s;
        }
        //general case
        StringBuilder sb = new StringBuilder();
        int end = s.length();
        for (int i = s.length() - 2; i >= 0; i--) {
            if (s.charAt(i) == ' ' && s.charAt(i + 1) != ' ') {
                sb.append(s.substring(i + 1, end));
                if (i != 0) sb.append(" ");
                end = i;
            }
            if (s.charAt(i) == ' ' && s.charAt(i + 1) == ' ') {
                end = i;
            }
        }
        if (end != s.length()) sb.append(s.substring(0, end));
        return sb.toString();
}
case3: has leading space, no tailing space, has continuous inner space
        //special case
        if (s.length() == 0) return "";
        if (s.length() == 1) {
            if (s.equals(" ")) return "";
            else return s;
        }
        //general case
        StringBuilder sb = new StringBuilder();
        int end = s.length();
        for (int i = s.length() - 2; i >= 0; i--) {
            if (s.charAt(i) == ' ' && s.charAt(i + 1) != ' ') {
                sb.append(s.substring(i + 1, end));
                if (i != 0) sb.append(" ");
                end = i;
            }
            if (s.charAt(i) == ' ' && s.charAt(i + 1) == ' ') {
                end = i;
            }
        }
        if (end != s.length()) sb.append(s.substring(0, end));
        String res = sb.toString();
        if (res.length() != 0 && res.charAt(res.length() - 1) == ' ') res = res.substring(0, res.length() - 1);
        return sb.toString();
case4: has leading space, has tailing space, has continuous inner space 
        //special case
        if (s.length() == 0) return "";
        if (s.length() == 1) {
            if (s.equals(" ")) return "";
            else return s;
        }
        //general case
        StringBuilder sb = new StringBuilder();
        int end = s.length();
        for (int i = s.length() - 2; i >= 0; i--) {
            if (s.charAt(i) == ' ' && s.charAt(i + 1) != ' ') {
                sb.append(s.substring(i + 1, end));
                if (i != 0) sb.append(" ");
                end = i;
            }
            if (s.charAt(i) == ' ' && s.charAt(i + 1) == ' ') {
                end = i;
            }
            if (s.charAt(i) != ' ' && s.charAt(i + 1) == ' ') {
                end = i + 1;
            }
        }
        sb.append(s.substring(0, end));
        String res = sb.toString();
        if (res.length() != 0 && res.charAt(res.length() - 1) == ' ') res = res.substring(0, res.length() - 1);
        return res;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值