日期:2014-05-20 浏览次数:20839 次
import java.util.*;
import static java.lang.System.out;
public class Test24
{
static ArrayList<Integer> getAdd1Type(final int M ,final ArrayList<Integer> Ns)
{
int size = Ns.size();
int index = size/2;
ArrayList<Integer> returnList = new ArrayList<Integer>();
//这种情况,就要求前部遍历,因为无法定位
if (Ns.get(index) == M)
{
for (int i =0; i<size;i++ )
{
if (Ns.get(i) == M)
{
returnList.add(Ns.get(i));
}
}
return returnList;
}
if (Ns.get(index) > M)
{
//0~index-1
for (int i = 0; i< index; i++ )
{
if (Ns.get(i) == M)
{
returnList.add(Ns.get(i));
}
}
return returnList;
}
else
{
//index+1~size
for (int i = index+1; i< size; i++ )
{
if (Ns.get(i) == M)
{
returnList.add(Ns.get(i));
}
}
return returnList;
}
}
static ArrayList<String> getAdd2Type(final int M ,final ArrayList<Integer> Ns)
{
ArrayList<String> listReturn = new ArrayList<String>();
//这里应该可以选择反向遍历的方式进行比较
int count = Ns.size();
for (int i = 0; i<count ;i++ )
{
if (Ns.get(i)>=M)
{
continue;
}
for (int j = i+1; j<count ;j++ )
{
if (Ns.get(j)>=M)
{
continue;
}
if ((Ns.get(i) +Ns.get(j))==M )
{
listReturn.add("{"+Ns.get(i)+","+Ns.get(j)+"}");
}
}
}
return listReturn;
}
static ArrayList<String> getAdd3Type(final int M ,final ArrayList<Integer> Ns)
{
ArrayList<String> listReturn = new ArrayList<String>();
//这里应该可以选择反向遍历的方式进行比较
int count = Ns.size();
for (int i = 0; i<count ;i++ )