面試題,兄弟我栽在上面了,沒按人家的思路寫,回來想了想原來也不難。 java
解法一: 面試
總體先倒轉,而後開始掃描,碰到空格,則證實單詞結束,將其倒轉 算法
/** * * @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(); } }
解法三: app
我面試時寫的算法, code
/** * * @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; } }被X了。