您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> JAVA >> 辅导 >> 正文    
  JAVA技巧(有序整数集合a和b的交集函数) 【注册男护士专用博客】          

JAVA技巧(有序整数集合a和b的交集函数)

www.nanhushi.com     佚名   不详 

  问题描述:
  有两个有序整数集合a和b,写一个函数找出它们的交集?
  几种解决方案:
  一:
  import java.util.Arrays;
  public class Test {
  public static void main(String args[]){
  int[] b = {4, 6, 7, 7, 7, 7, 8, 8, 9, 10, 100, 130, 130, 140, 150};
  int[] a = {2, 3, 4, 4, 4, 4, 7, 8, 8, 8, 8, 9, 100, 130, 150, 160};
  int[] c = intersect(a, b);
  System.out.println(Arrays.toString(c));
  }
  public static int[] intersect(int[] a, int[] b) {
  if(a[0] > b[b.length - 1] || b[0] > a[a.length - 1]) {
  return new int[0];
  }
  int[] intersection = new int[Math.max(a.length, b.length)];
  int offset = 0;
  for(int i = 0, s = i; i < a.length && s < b.length; i++) {
  while(a[i] > b[s]) {
  s++;
  }
  if(a[i] == b[s]) {
  intersection[offset++] = b[s++];
  }
  while(i < (a.length - 1) && a[i] == a[i + 1]) {
  i++;
  }
  }
  if(intersection.length == offset) {
  return intersection;
  }
  int[] duplicate = new int[offset];
  System.arraycopy(intersection, 0, duplicate, 0, offset);
  return duplicate;
  }
  }
  二:
  import java.util.ArrayList;
  public class jiaoji {
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  int[] b = { 4, 6, 7, 7, 7, 8, 8, 9, 10, 100, 130, 140, 150 };
  int[] a = { 2, 3, 4, 4, 4, 7, 8, 8, 8, 8, 9, 100, 130, 150, 160 };
  ArrayList c = sect(a, b);
  for (int i = 0; i < c.size(); i++) {
  System.out.print(c.get(i) + ",");
  }
  }
  public static ArrayList sect(int[] a, int[] b) {
  int k = 0;
  int startposb = 0;
  if (a[0] > b[b.length - 1] || b[0] > a[a.length - 1]) {
  return new ArrayList();
  }
  ArrayList arraylist = new ArrayList();
  for (int i = 0; i < a.length; i++) {
  for (int j = startposb; j < b.length; j++) {
  if (a[i] == b[j]) {
  arraylist.add(k, a[i]);
  k++;
  startposb = j + 1;
  break;
  }
  }
  }
  return arraylist;
  }
  }
  三:
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
  public class NumberCross {
  public static void main(String[] args) {
  int a[] = {1,3,4,5,6,7,499,199};
  int b[] = {3,299,199,8,9,499};
  Set<Integer> list = new HashSet<Integer>();
  for(int i=0; i<a.length; i++) {
  list.add(a[i]);
  }
  for(int j=0; j<b.length; j++) {
  list.add(b[j]);
  }
  for(Iterator<Integer> iter=list.iterator(); iter.hasNext();) {
  System.out.print(iter.next() + " ");
  }
  }
  }
  好了,就这么多吧! 考试大等级站收集整理!

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目

      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)                            【进男护士社区逛逛】
    姓 名:
    * 游客填写  ·注册用户 ·忘记密码
    主 页:

    评 分:
    1分 2分 3分 4分 5分
    评论内容:
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。