My accepted version with Java but need refine


  • 0
    J

    I'm new to java -- a C programmer trying to transform:) I'm also new to leetcode, not sure if I could change the definition of the function. For instance, I wanted to do it in place but String is immutable objects, so there is no way to do so if I didn't change the parameters to char[].

    This piece of program is probably not elegant enough. If I do not want to use java string functions or other data structures like stack, how can I refine my code in Java style?

    Lastly, I am curious how does leetCode decide to "accept" a solution? As far as I can tell, my code has to take 2 entire string copies, plus the construct/destruct costs. It is actually very inefficient especially for small inputs.

    public class Solution {
        static char delim = ' ';
        public String reverseWords(String s) {
                if (s == null) return "";
                
                int i;
                for (i = 0; i < s.length(); i++) {
                    if (s.charAt(i) != delim)
                        break;
                }
                int head = i;
                for (i = s.length() - 1; i >=0; i--) {
                    if (s.charAt(i) != delim)
                        break;
                }
                int tail = i;
                if (head > tail) return "";
                if (head == tail) {
                    return s.substring(head, (head + 1));
                }
                int j, k, h, t, index = 0;
                char[] arr = new char[s.length()];
                h = tail  - 1;
                t = tail;
                while(h >= head) {
                    if (h == head) {
                        for (i = h; i <= t; i++) {
                            arr[index] = s.charAt(i);
                            index++;
                        }         
                        break;
                    }
                    if ((s.charAt(h) == delim) && (s.charAt(h - 1) != delim)) {
                        j = h + 1;
                        k = t;
                        for (i = j; i <= k; i++) {
                            if (s.charAt(i) != delim) {
                                arr[index] = s.charAt(i);
                                index++;
                            }
                        }
                        arr[index] = delim;
                        index++;
                        
                        t = h - 1;
                        if (t == head) {
                            arr[index] = s.charAt(t);
                            break;
                        }
                        h = t - 1;
                    } else {
                        h--;
                    }
                }
                for (i = s.length() - 1; i >= 0; i--) {
                    if ((arr[i] != 0) && (arr[i] != delim)) 
                        break;
                }
                
                String ret = new String(arr);
                return ret.substring(0, i + 1);
    
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.