Leetcode - Reverse Words in a String

Leetcode - Reverse Words in a String_第1张图片

My code:


public class Solution {
    public String reverseWords(String s) {
        if (s == null)
            return null;
        else if (s.length() == 0)
            return s;
        int i = s.length() - 1;
        int temp = i;
        String reverse = "";
        while (i >= 0) {
            if ((i + 1 < s.length()) && s.charAt(i) == ' ' && s.charAt(i + 1) != ' ')
                reverse += s.substring(i + 1, temp + 1) + " ";
            else if ((s.charAt(i) != ' ' && i + 1 < s.length()) && s.charAt(i + 1) == ' ')
                temp = i;
            i--;
        }
        if (s.charAt(0) != ' ')
            reverse += s.substring(0, temp + 1);
        
        if (s.charAt(temp) != ' ' && reverse.charAt(reverse.length() - 1) == ' ')
            return reverse.substring(0, reverse.length() - 1);
        return reverse;
    }
    
    public static void main(String[] args) {
        Solution test = new Solution();
        System.out.println(test.reverseWords(" "));
    }
}

My test result:

Leetcode - Reverse Words in a String_第2张图片
Paste_Image.png

这道题目好奇怪。我就遍历了一次数组。。。跑的时间这么慢。
写的我心情很糟糕了。题目本身没什么难度。

**
总结: String
**

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public String reverseWords(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        
        char[] c = s.toCharArray();
        reverse(c, 0, c.length - 1);
        
        int begin = 0;
        int end = 0;
        while (end < c.length) {
            if (c[end] == ' ') {
                if (begin == end) {
                    begin++;
                    end++;
                }
                else {
                    reverse(c, begin, end - 1);
                    begin = end + 1;
                    end = begin;
                }
            }
            else {
                end++;
            }
        }
        
        if (begin < c.length) {
            reverse(c, begin, c.length - 1);
        }
        
        StringBuilder ret = new StringBuilder();
        int i = 0;
        while (i < c.length) {
            if (c[i] != ' ') {
                ret.append(c[i]);
                i++;
            }
            else {
                if (ret.length() == 0) {
                    i++;
                    continue;
                }
                ret.append(c[i]);
                while (i < c.length && c[i] == ' ') {
                    i++;
                }
            }
        }
        
        if (ret.length() > 0 && ret.charAt(ret.length() - 1) == ' ')
            ret.deleteCharAt(ret.length() - 1);
        return ret.toString();
    }
    
    private void reverse(char[] c, int i, int j) {
        int begin = i;
        int end = j;
        while (begin < end) {
            char temp = c[begin];
            c[begin] = c[end];
            c[end] = temp;
            begin++;
            end--;
        }
    }
}

思路很明显,就是reverse
但是 corner case 很多。
我为了完全不用Java自带的高级方法,所有东西都是通过数组实现。
其实 leading and trailing spaces 可以用 string.trim() 消除。
但是又得新生成一个string,效率低。

所以,最后得判断下,末尾是否有 空格, 如果有,那么删除之。

Anyway, Good luck, Richardo! -- 09/12/2016

My code:

public class Solution {
    public String reverseWords(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        
        StringBuilder sb = new StringBuilder();
        for (int start = s.length() - 1; start >= 0; start--) {
            char curr = s.charAt(start);
            if (curr == ' ') {
                continue;
            }
            int end = start;
            while (start >= 0 && s.charAt(start) != ' ') {
                start--;
            }
            sb.append(s.substring(start + 1, end + 1) + " ");
        }
        return sb.toString().trim();
    }
}

reference:
https://discuss.leetcode.com/topic/30410/my-10-line-java-code-that-beats-73-of-the-submissions

还是选择用一些更加简单地方法来做,避免面试的时候写出bug

Anyway,Good luck, Richardo! -- 09/25/2016

你可能感兴趣的:(Leetcode - Reverse Words in a String)