2013年12月13日星期五

List of combination

Assuming that there are N list, list which has M integers . How kind these N list seamless stitching together . For example aList have 123456, bList have 45678, cList 789 . This time , aList with cList on seamless stitching up into dList: 123456789.
------ Solution ---------------------------------------- ----
cycle to repeat , but the efficiency is not the same in different ways
List<Integer> l1 = new ArrayList<Integer>();
List<Integer> l2 = new ArrayList<Integer>();
for (int i = 1; i < 7; i++) {
l1.add(i);
}
for (int i = 4; i < 9; i++) {
l2.add(i);
}
l1.addAll(l2);

Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = l1.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
for (Object o : newList) {
System.out.print(o);
}
System.out.println("\n---");
List<Integer> listInt = new ArrayList<Integer>(
new LinkedHashSet<Integer>(l1));
for (Integer o : listInt) {
System.out.print(o);
}

------ Solution ------------------------------------- -------
list sorting relatively combination
------ Solution ------------------- -------------------------
1, HashSet, the List which in turn put a number to it they will go heavy.
2, if you have to synthesize a List, then on the basis of a through ArrayList (Collection c) constructor new one List
3, or create an empty ArrayList, when the other side of the inside contains comparisons are included with
------ Solution ------------- -------------------------------
determine whether the elements under both meets plus a principle would be all right . The first tail +1 is equal to the second one first . Intermediate elements would not have considered .
------ Solution ---------------------------------------- ----
123 and 789 count seamlessly ? Or , it must be completely linked , orderly integer it?

------ Solution ------------------------------------ --------
you list all of what you put into a string a good amount . index is not any stitching , and vice versa on the fight .
------ Solution ---------------------------------------- ----
public static void main(String args[]) throws Exception{
List list1=new ArrayList();
List list2=new ArrayList();

list1.add("1");
list1.add("2");
list2.add("2");
List<String> newChar=new ArrayList();
concat(newChar,list1,list2);
for(int i=0;i<newChar.size();i++){
System.out.println(newChar.get(i));
}
}

static List concat(List<String> newChar,List<String> list1,List<String> list2){
String[] char1=list1.toArray(new String[0]);
String[] char2=list2.toArray(new String[0]);

for(int i=0;i<char1.length;i++){
newChar.add(char1[i]);
}
for(int j=0;j<char2.length;j++){
if(!newChar.contains(char2[j])) newChar.add(char2[j]);
else {
System.out.println("有重复元素,不能拼接");
newChar.removeAll(newChar);
break;
}
}
return newChar;
}

------ Solution ------------------------------ --------------
premise list already is uninterrupted integer combinations, try the first one that +1 is equal to the second end of the first , was then on for the value to the list , and not on the continue;

------ Solution ------------------------------------ --------

  
completely linked , orderly   .
ah , ideas probably do two operations: one new to the list to be seamless with the original list ( refer to 5L ideas ) ; 2 passed a judgment, but also new to the list . as ordered. This long traverse once , kept equal to +1 .
------ For reference only -------------------------------------- -


I express is not very clear, I mean, stitching together the two list .
example aList have 123456, bList have 45678, cList 789 . This time , aList with cList on seamless stitching up into dList: 123456789. And aList and bList is not seamless stitching together because there are duplicate 456 between them. bList and cList can not be spliced ​​together , they exist duplicate 78.


------ For reference only ---------------------------------- -----



completely linked , orderly

没有评论:

发表评论