您的位置:首页 > 其它

given two unordered list find the greatest common integer

2013-07-09 11:22 465 查看
given two unordered list find the greatest common integer.

package com.zhuyu_deng.test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test
{
public static int biggestCommonInteger(List<Integer> a, List<Integer> b)
{
Set<Integer> set = new HashSet<Integer>();
int biggestNum = Integer.MIN_VALUE;

for (Integer i : a)
set.add(i);
for (Integer j : b)
{
if (set.contains(j))
{
if (j > biggestNum)
biggestNum = j;
}
}

return biggestNum;
}

public static void main(String args[])
{
List<Integer> aList = new ArrayList<Integer>();
aList.add(0);
aList.add(4);
aList.add(1);
aList.add(5);
List<Integer> bList = new ArrayList<Integer>();
bList.add(0);
bList.add(3);
bList.add(2);
bList.add(1);
bList.add(4);
bList.add(5);
int ans = biggestCommonInteger(aList, bList);
System.out.println(ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法
相关文章推荐