Are we allowed to use Java build-in functions?


  • 8
    A

    Hi all,

    I solved this problem by using java build-in functions: split and trim. I'm wondering if we are expected to use them or not, because this problem may become too easy by using these functions.

    public String reverseWords(String s) {
        if (s == null || s.length() <= 0) return s;
        String[] words = s.split(" +");
        StringBuilder ret = new StringBuilder();
        for (int i = words.length - 1; i >= 0; i--)
            if (words[i].length() > 0) ret.append(words[i]).append(" ");
        return ret.toString().trim();
    }

  • 7
    S

    It's easy to get an answer, just put yourself in interviewer's shoes. As an interviewer, what do your think about this solution?

    From my perspective, I consider this interviewee is familiar with string manipulation in Java, but I am going to ask for another implementation only with basic string operation.


  • 0
    S

    I wrote a same solution as yours with split and trim.


Log in to reply
 

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