Pregunta de entrevista de Amazon

Write a program that reverses the words in a sentence.

Respuestas de entrevistas

Anónimo

30 ene 2012

I used a stack, pushing words onto it as I reached spaces.

Anónimo

6 feb 2012

String[] temp; temp = input.split(" "); System.out.println("Array length : " + temp.length); for (int i = temp.length; i > 0; i--) System.out.print(temp[i-1] + " " ); System.out.println(""); }

Anónimo

15 mar 2012

public static String reverseSentence(String a){ StringBuilder result = new StringBuilder(); Stack stack = new Stack(); String[] temp; temp = a.split(" "); for (String s : temp){ stack.push(s); } while(!stack.empty()){ result.append(stack.pop() + " "); } return result.toString(); }

Anónimo

1 feb 2012

=0; $index--) { $result[strlen($word)-1 - $index] = $word[$index]; } return implode("", $result); } // O(n/2) times = O(n) times function reverse2($word) { $temp = ""; if(strlen($word)%2 == 1) $stop = strlen($word)/2; else $stop = strlen($word)/2-1; for ($index = 0; $index