码迷,mamicode.com
首页 > 其他好文 > 详细

151. Reverse Words in a String

时间:2017-07-31 17:15:04      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:turn   between   tps   ogr   stringbu   tar   tween   空格   public   

Clean Java two-pointers solution (no trim( ), no split( ), no StringBuilder)

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.

click to show clarification.

Clarification:
What constitutes a word?
A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.
How about multiple spaces between two words?
Reduce them to a single space in the reversed string.

窗口指针找单词, in-place删除空格类似于数组中删除重复的元素, 所以先把其转化为数组

trim的实现用双指针

while(r < n) {

  while (r < n && charAt(j) == " ") {  } // 删除前驱空格

  while (r < n && charAt(j) != " ") { a[i++] = a[j++]} // 数组中删除重复的元素

while (j < n && a[j] == ‘ ‘) j++; // 删除后驱的空格

if (j < n) a[i++] = ‘ ‘; //加空格


reversewords 窗口指针先左指针, 再右指针, 直到满足窗口表示的意义 , while () 中的|| 使得指针从上一个单词处移动.不然造成死循环
 while (i < n) {
      while (i < j || i < n && a[i] == ‘ ‘) i++; // skip spaces
      while (j < i || j < n && a[j] != ‘ ‘) j++; // skip non spaces
      reverse(a, i, j - 1);                      // reverse the word
    }
public class Solution {
  
  public String reverseWords(String s) {
    if (s == null) return null;
    
    char[] a = s.toCharArray();
    int n = a.length;
    
    // step 1. reverse the whole string
    reverse(a, 0, n - 1);
    // step 2. reverse each word
    reverseWords(a, n);
    // step 3. clean up spaces
    return cleanSpaces(a, n);
  }
  
  void reverseWords(char[] a, int n) {
    int i = 0, j = 0;
      
    while (i < n) {
      while (i < j || i < n && a[i] == ‘ ‘) i++; // skip spaces
      while (j < i || j < n && a[j] != ‘ ‘) j++; // skip non spaces
      reverse(a, i, j - 1);                      // reverse the word
    }
  }
  
  // trim leading, trailing and multiple spaces
  String cleanSpaces(char[] a, int n) {
    int i = 0, j = 0;
      
    while (j < n) {
      while (j < n && a[j] == ‘ ‘) j++;             // skip spaces
      while (j < n && a[j] != ‘ ‘) a[i++] = a[j++]; // keep non spaces
      while (j < n && a[j] == ‘ ‘) j++;             // skip spaces
      if (j < n) a[i++] = ‘ ‘;                      // keep only one space
    }
  
    return new String(a).substring(0, i);
  }
  
  // reverse a[] from a[i] to a[j]
  private void reverse(char[] a, int i, int j) {
    while (i < j) {
      char t = a[i];
      a[i++] = a[j];
      a[j--] = t;
    }
  }
  
}

  

151. Reverse Words in a String

标签:turn   between   tps   ogr   stringbu   tar   tween   空格   public   

原文地址:http://www.cnblogs.com/apanda009/p/7263938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!