java Realizes Flipping Word Order Columns

  • 2021-07-01 07:25:09
  • OfStack

In this paper, we share the specific code of java to flip the word sequence column for your reference, the specific contents are as follows

Recently, a new employee, Fish, always takes an English magazine and writes some sentences in his notebook every morning. Colleague Cat was interested in what Fish wrote. One day he borrowed it from Fish to read it, but he couldn't understand its meaning. For example, "student. a am I". Later, I realized that this guy had reversed the order of the words in the sentence, and the correct sentence should be "I am a student.". Cat is not good at flipping the order of 11. Can you help him?

Code

Use the way in the last article to left rotate the string

Solution 1

Define two pointers and exchange the corresponding strings in turn

For example, str = "I am a student.", array = {"I", "am", "a", "student."}

array [0] and array [3] exchange, {"student.", "am", "a", "I"} array [1] and array [2] exchange, {"student.", "a", "am", "I"}

public static String reverseStringSequence(String str) {
    if (Strings.isNullOrEmpty(str)) {
      return str;
    }
    String[] seq = str.split(" ");
    //  Define two pointers, 1 I started from scratch, 1 Swap in pairs, starting from the tail, and stop when two pointers meet 
    int start = 0;
    int end = seq.length - 1;
    while (start < end) {
      String temp = seq[start];
      seq[start] = seq[end];
      seq[end] = temp;
      start++;
      end--;
    }
    return StringUtils.join(seq, " ");
  }
 
  public static void main(String[] args) {;
    String result = reverseStringSequence("I am a student.");
    System.out.println(result);
  }

Solution 2

Thoughts and the above 1, are string inversion, here is not in the unit of words, but in the unit of characters, so we need to carry out two-step inversion

Invert each word to get "I ma a. tneduts" Invert the whole string to get "student. a am I"

public static String reverseStringSequence(String str) {
    if (Strings.isNullOrEmpty(str)) {
      return str;
    }
    char[] seq = str.toCharArray();
    int length = seq.length;
    //  Define two pointers to record the starting position of the word to be reversed 
    int start = 0;
    int end = 0;
    //  Here 1 It must contain equal to, because it is necessary to judge whether it is the last 1 Words, so that you can handle the last 1 Word 
    while (end <= length) {
      //  When you have traversed to the end of the string 1 Characters, or when the current character is a space 
      //  The word before the space is reversed, that is "am" Reverse to "ma"
      // 1 Be sure to put the judgment before whether it is the end, otherwise seq[end] An error is reported because the valid index of the array is derived from the 0 Beginning 
      //  After reversing, modify the starting pointer of the word to be below the space 1 Characters 
      //  If it does not meet the conditions, move the pointer and continue to judge 1 Characters 
      if (end == length || seq[end] == ' ') {
        reverse(seq, start, end - 1);
        start = end + 1;
      }
      end++;
    }
    //  Invert this array 
    reverse(seq, 0, length - 1);
    return new String(seq);
  }
 
  private static void reverse(char[] seq, int start, int end) {
    while (start < end) {
      char temp = seq[start];
      seq[start] = seq[end];
      seq[end] = temp;
      start++;
      end--;
    }
  }

Related articles: