[Leetcode]151.ReverseWordsInaString

61 阅读1分钟

Description: Given an input string s, reverse the order of the words.

word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.

Input: s = "  hello world  "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.

Solution: Best explanation with photos(without extra space)

The explanation of the above link is very clear. First of all, reverse the entire string; Then find individual words and reverse each word. Though, I think the code in the explanation may cause OutOfBoundException.

fun reverseWords(s: String): String {
    val words = s.toCharArray()
    // reverse the entire array
    reverseSingleWord(words, 0, words.size - 1)

    var current = 0
    var left = 0
    var right: Int

    while (left < words.size) {
        // find the start point of individual word
        while (left < words.size && words[left] == ' ') {
            left++
        }
        right = left
        // find the end point of individual word
        while (right < words.size && words[right] != ' ') {
            right++
        }
        var singleWordLength = right - left
        // no more word, return
        if (singleWordLength == 0) {
            break
        }
        // if current is 0, it is first word. or it need to move to next point.
        // Beacuse, one blank should be add into each word.
        if (current != 0) {
            current++
        }
        // the start point of current word
        val temp = current
        
        // copy the word to front of array
        while (singleWordLength > 0) {
            words[current++] = words[left++]
            singleWordLength--
        }
        // reverse individual word
        reverseSingleWord(words, temp, current - 1)
        
        // Because we just copy behind to front, 
        // so the current may not be blank. we need set blank between each words.
        if (current != words.size) {
            words[current] = ' '
        }
    }
    // words.toString() will drop blank(' '), so use String(words) 
    return String(words).substring(0, current)
}

// reverse the part between start and end.
fun reverseSingleWord(word: CharArray, start: Int, end: Int) {
    var startIndex = start
    var endIndex = end
    while (startIndex < endIndex) {
        val temp = word[startIndex]
        word[startIndex] = word[endIndex]
        word[endIndex] = temp
        startIndex++
        endIndex--
    }
}