Given an array of characters which form a sentence of words, give an efficient algorithm to reverse
2012-08-23 11:12
288 查看
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.
import java.util.ArrayList;
import java.util.List;
public class Reverse {
public static void main(String args[]) {
String sentence = "one world one dream";
List<String> words = new ArrayList<String>();
int j = 0;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ' ') {
words.add(sentence.substring(j,i ));
j=i+1;
}
}
words.add(sentence.substring(j));
for(int i=0;i<words.size()/2;i++)
{
String temp=words.get(i);
words.set(i,words.get(words.size()-i-1));
words.set(words.size()-i-1,temp);
}
for(int i=0;i<words.size();i++)
System.out.print(words.get(i)+" ");
}
}
import java.util.ArrayList;
import java.util.List;
public class Reverse {
public static void main(String args[]) {
String sentence = "one world one dream";
List<String> words = new ArrayList<String>();
int j = 0;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ' ') {
words.add(sentence.substring(j,i ));
j=i+1;
}
}
words.add(sentence.substring(j));
for(int i=0;i<words.size()/2;i++)
{
String temp=words.get(i);
words.set(i,words.get(words.size()-i-1));
words.set(words.size()-i-1,temp);
}
for(int i=0;i<words.size();i++)
System.out.print(words.get(i)+" ");
}
}
相关文章推荐
- Given an array of characters. How would you reverse it.
- 24 Given the following steps, which would be the correct order to create a backup of an Oracle datab
- Q.2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only acce
- CareerCup Given an array having positive integers, find a subarray which adds to a given number
- Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
- Given an array of integers, sort the array according to frequency of elements
- Given an array of ages (integers) sorted lowest to highest, output the number of occurrences for eac
- Write an Efficient C Program to Reverse Bits of a Number
- Given an array of size N in which every number is between 1 and N, determine if there are any dupli
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- Algorithm: Reverse a Integer to an array with recursion
- Algorithm: Reverse a Integer to an array with recursion
- Algorithm: efficient way to remove duplicate integers from an array
- Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ
- SDK 组件 Qupaisdk 启动出错,错误消息为 [Qupaisdk], the android stack error message is Fail to start the plugin, which is caused by Failed resolution of: Lcom/duanqu/qupai/recorder/R$array;
- Write a program that gives count of common characters presented in an array of strings..(or array of
- How to programmatically change the default view of an InfoPath form
- Multi select elements which sums upto a certain number from an array(II)
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
- leetcode7. Reverse Integer:Given a 32-bit signed integer, reverse digits of an integer.