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);
}
}
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);
}
}
相关文章推荐
- P32 (**) Determine the greatest common divisor of two positive integer numbers.
- 35.两链表的第一个公共结点[Find the first common node of two linked list]
- P32 (**) Determine the greatest common divisor of two positive integer numbers.
- Ch2-5: find the beginning of loop in a circular linked list---two solutions
- Find the nearest common ancestor of any two nodes in a binary search tree
- Find the common ancestor of two nodes in a binary tree
- 最大公约数计算(Find the Greatest Common Divisior)
- Un-sorted list, find the "k" largest value, the original list can be changed
- 1. MissingInteger 最小遗失整数 Find the minimal positive integer not occurring in a given sequence.
- Could not find a price list in Ordered UOM xxx and Primary UOM of the item
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- Could not find a price list in Ordered UOM xxx and Primary UOM of the item
- P17 (*) Split a list into two parts; the length of the first part is given.
- P17 (*) Split a list into two parts; the length of the first part is given.
- CareerCup Find all the conflicting appointments from a given list of n appointments.
- Given an unsorted integer array, find the first missing positive integer.
- python: find the index of a given value in a list
- given two directory trees how to find which files are the same?
- Calculate the Greatest Commom Divisor (GCD) and Lowest Common Multiple (LCP) of Two Integers
- The Unordered List Abstract Data Type