面试题,兄弟我栽在上面了,没按人家的思路写,回来想了想原来也不难。
解法一:
整体先倒转,然后开始扫描,碰到空格,则证明单词结束,将其倒转
/**
*
* @author Jason Li 2014-5
* 一个char数组中存储着无标点的英文,将其按单词倒置
* 要求:只使用常数辅助空间
* 例如:"what are you doing" 要变成"doing you are what"
*
*/
public class CharArrayReverseTest {
public static void main(String[] args) {
char[] input = "what are you doing".toCharArray();
ReverseSentence(input);
printArray(input);
}
private static void ReverseSentence(char[] input) {
int word_start_index = 0;
int word_end_index = 0;
reverseArray(input, 0, input.length - 1); // 整体翻转
while (word_end_index < input.length) { // 按空格区分单词翻转
if (input[word_end_index] == ' ') {
reverseArray(input, word_start_index, word_end_index - 1);
word_end_index++;
word_start_index = word_end_index;
}
word_end_index++;
}
reverseArray(input, word_start_index, word_end_index - 1);// 最后一个单词翻转
}
private static void reverseArray(char[] input, int i, int j) {
while (i < j) {
char c = input[i];
input[i] = input[j];
input[j] = c;
i++;
j--;
}
}
private static void printArray(char[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i]);
}
System.out.println();
}
}
左右同时向中心扫描并做交换,哪边先碰到空格,则证明已转到另一方的是完整单词了,则将这个单词倒转。
/**
*
* @author Jason Li 2014-5
* 一个char数组中存储着无标点的英文,将其按单词倒置
* 要求:只使用常数辅助空间
* 例如:"what are you doing" 要变成"doing you are what"
*
*/
public class CharArrayReverseTest2 {
public static void main(String[] args) throws Exception {
char[] input = "what are you doing".toCharArray();
reverseSentence(input);
printArray(input);
}
private static void reverseSentence(char[] input) {
int left_word_start_index = 0;
int right_word_start_index = input.length - 1;
int left_index = 0;
int right_index = input.length - 1;
while (left_index < right_index) {
if (input[left_index] == ' ') {
// 如果左方扫到空格,则已转到右方的是一个完整单词了,倒转该单词
reverseWord(input, right_index + 1, right_word_start_index);
right_word_start_index = right_index - 1;
}
if (input[right_index] == ' ') {
// 如果右方扫到空格,则已转到左方的是一个完整单词了,倒转该单词
reverseWord(input, left_word_start_index, left_index - 1);
left_word_start_index = left_index + 1;
}
swap(input, left_index, right_index); // 左右交换,
left_index++;
right_index--;
}
reverseWord(input, left_word_start_index, right_word_start_index);// 最后一个单词倒转
}
//交换i,j位置的char
private static void swap(char[] input, int i, int j) {
char c = input[i];
input[i] = input[j];
input[j] = c;
}
//i,j之间的内容交换
private static void reverseWord(char[] input, int i, int j) {
while (i < j) {
char c = input[i];
input[i] = input[j];
input[j] = c;
i++;
j--;
}
}
private static void printArray(char[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i]);
}
System.out.println();
}
}
解法三:
我面试时写的算法,
/**
*
* @author Jason Li 2014-5
* 一个char数组中存储着无标点的英文,将其按单词倒置
* 要求:只使用常数辅助空间
* 例如:"what are you doing" 要变成"doing you are what"
*
*/
public class WordReverse {
public static void main(String[] args) {
char [] input = "what are you doing".toCharArray();
String str = reverseSentence(input);
System.out.println(str);
}
private static String reverseSentence(char[] input) {
String str = String.valueOf(input); //input.toString()不行
String [] strArray = str.split(" ");
StringBuffer sb = new StringBuffer();
for (int i = strArray.length-1; i>=0; i--){//按空格切分成数组后,从后往前重新组串
sb.append(strArray[i] + " ");
}
str = sb.toString().substring(0,sb.toString().lastIndexOf(" "));
return str;
}
}