这是我在做什么:
字符串一个=“一些字符串”
String中的两个=“一些字符串”Set操作的复杂性
我想知道的一切都在字符串中的字符一个和两个和他们应该按顺序排列,因为他们在字符串之一
我写了一个Java程序,它通过使用集合执行集合上的集操作。
我想知道什么是执行一系列操作的复杂性是什么,是多项式时间或线性时间
我的计划是在这里
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package careercup.google;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
*
* @author learner
*/
public class CharaterStringsIntersection {
private static final String one = "abcdefgabcfmnx";
private static final String two = "xbcg";
public static void main(String args[]){
List<Character> l_one = new ArrayList<Character>();
List<Character> l_two = new ArrayList<Character>();
for(int i=0; i<one.length(); i++){
l_one.add(one.charAt(i));
}
for(int j=0; j<two.length(); j++){
l_two.add(two.charAt(j));
}
l_one.retainAll(l_two);
Iterator iter = l_one.iterator();
while(iter.hasNext()){
System.out.println(" > " + iter.next());
}
}
}
输出:
run:
> b
> c
> g
> b
> c
> x
您可以使用LinkedHashSet在保留订单的同时获取Set的性能。注意:您只需要在第一个集合中保留订单,第二个订单的顺序并不重要?你需要保持重复吗?如果是这样,第一个集合必须是一个列表,但第二个集合仍然可以是一个集合。 – 2010-10-24 23:44:23