当前位置:网站首页>Invert words in a string (split, double ended queue)

Invert words in a string (split, double ended queue)

2022-06-12 01:43:00 There is no end to learning

Topic link :https://leetcode.cn/problems/reverse-words-in-a-string/

Give you a string s , Invert the string word The order of .

word Is a string of non whitespace characters .s Use at least one space in the string word Separate .

return word Reverse order and word The result string connected with a single space .

Be careful : Input string s There may be leading spaces in 、 Trailing spaces or multiple spaces between words . In the returned result string , Words should be separated by only a single space , And does not contain any additional spaces .

Their thinking :
1. use split Division ,reverse reverse ,join Splice three Java Built in functions solve

shortcoming : It was written in the interview , The interviewer may say : Let's stop here for the interview ( dog's head ).

Calling a function is not the original intention of this topic .

The code is as follows :

import java.util.Arrays;
import java.util.Collections;

public class Main {
    
    public static void main(String[] args) {
    
        String s = "excample a gg";
        System.out.println(reverseWords(s));
    }
    public static String reverseWords(String s) {
    
        // Remove the leading and trailing white space characters 
        s = s.trim();

        String[] words = s.split(" +");

        Collections.reverse(Arrays.asList(words));

        return String.join(" ", words);
    }
}

Double ended queue , Because the double ended queue supports the method of inserting from the queue head , So we can process word by word along the string , Then press the word into the head of the queue , Then turn the queue into a string .

The code is as follows :

import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
    
    public static void main(String[] args) {
    
        String s = " example a gg ";
        System.out.println(reverseWords(s));
    }
    public static String reverseWords(String s) {
    
        int left = 0;
        int right = s.length()-1;

        // Remove the white space at the beginning of the string 
        while (left <= right && s.charAt(left) == ' '){
    
            left++;
        }
        // Remove the white space characters at the end of the string 
        while (left <= right && s.charAt(right) == ' '){
    
            right--;
        }

        Deque<String> d = new ArrayDeque<String>();// deque 

        StringBuilder word = new StringBuilder();

        while (left <= right) {
    
            char c = s.charAt(left);
            if ((word.length() != 0) && (c == ' ')) {
    
                //  Press word to the head of the queue 
                d.offerFirst(word.toString());
                word.setLength(0);//word empty 
            } else if (c != ' ') {
    // Consider the case where there are spaces inside the string 
                word.append(c);
            }
            ++left;
        }
        d.offerFirst(word.toString());

        return String.join(" ", d);

    }
}

原网站

版权声明
本文为[There is no end to learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120135203321.html