求有序整数集合a和b的交集函数

 问题描述:

有两个有序整数集合a和b,写一个函数找出它们的交集?

几种解决方案:

一:

  1. import java.util.Arrays;
  2. public class Test {
  3.     public static void main(String args[]){
  4.         int[] b = {46777788910100130130140150};
  5.         int[] a = {234444788889100130150160};
  6.         int[] c = intersect(a, b);
  7.         System.out.println(Arrays.toString(c));
  8.     }
  9.     public static int[] intersect(int[] a, int[] b) {
  10.         if(a[0] > b[b.length - 1] || b[0] > a[a.length - 1]) {
  11.             return new int[0];
  12.         }
  13.         int[] intersection = new int[Math.max(a.length, b.length)];
  14.         int offset = 0;
  15.         for(int i = 0, s = i; i < a.length && s < b.length; i++) {
  16.             while(a[i] > b[s]) {
  17.                 s++;
  18.             }
  19.             if(a[i] == b[s]) {
  20.                 intersection[offset++] = b[s++];
  21.             }
  22.             while(i < (a.length - 1) && a[i] == a[i + 1]) {
  23.                 i++;
  24.             }
  25.         }
  26.         if(intersection.length == offset) {
  27.             return intersection;
  28.         }
  29.         int[] duplicate = new int[offset];
  30.         System.arraycopy(intersection, 0, duplicate, 0, offset);
  31.         return duplicate;
  32.     }
  33. }

二:

  1. import java.util.ArrayList;
  2. public class jiaoji {
  3.     public static void main(String[] args) {
  4.         // TODO Auto-generated method stub
  5.         int[] b = { 4677788910100130140150 };
  6.         int[] a = { 23444788889100130150160 };
  7.         ArrayList c = sect(a, b);
  8.         for (int i = 0; i < c.size(); i++) {
  9.             System.out.print(c.get(i) + ",");
  10.         }
  11.     }
  12.     public static ArrayList sect(int[] a, int[] b) {
  13.         int k = 0;
  14.         int startposb = 0;
  15.         if (a[0] > b[b.length - 1] || b[0] > a[a.length - 1]) {
  16.             return new ArrayList();
  17.         }
  18.         ArrayList arraylist = new ArrayList();
  19.         for (int i = 0; i < a.length; i++) {
  20.             for (int j = startposb; j < b.length; j++) {
  21.                 if (a[i] == b[j]) {
  22.                     arraylist.add(k, a[i]);
  23.                     k++;
  24.                     startposb = j + 1;
  25.                     break;
  26.                 }
  27.             }
  28.         }
  29.         return arraylist;
  30.     }
  31. }

三:

  1. import java.util.HashSet;
  2. import java.util.Iterator;
  3. import java.util.Set;
  4. public class NumberCross {
  5.     public static void main(String[] args) {
  6.         int a[] = {1,3,4,5,6,7,499,199};
  7.         int b[] = {3,299,199,8,9,499};
  8.         Set<Integer> list = new HashSet<Integer>();
  9.         for(int i=0; i<a.length; i++) {
  10.             list.add(a[i]);
  11.         }
  12.         for(int j=0; j<b.length; j++) {
  13.             list.add(b[j]);
  14.         }
  15.         for(Iterator<Integer> iter=list.iterator(); iter.hasNext();) {
  16.             System.out.print(iter.next() + " ");
  17.         }
  18.     }
  19. }

好了,就这么多吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,这是一个比较具体的问题,我会尽力回答。下面是我的回答: 首先,我们可以定义一个有序链表结构体来表示整数集合: ``` typedef struct node { int value; struct node* next; } Node; typedef struct { Node* head; int size; } LinkedList; ``` 其中,每个节点包含一个整数值和一个指向下一个节点的指针,而链表则包含一个指向头节点的指针和链表大小。 接下来,我们可以编写一些基本的操作函数来对链表进行操作。这些函数包括: - `create_node(int value)`:创建一个新节点 - `insert_node(LinkedList* list, int value)`:将一个新节点插入到有序链表中 - `delete_node(LinkedList* list, int value)`:从有序链表中删除一个节点 - `create_linkedlist()`:创建一个新的有序链表 - `destroy_linkedlist(LinkedList* list)`:销毁一个有序链表 - `print_linkedlist(LinkedList* list)`:打印一个有序链表中的元素 有了这些基本操作函数,我们就可以实现集合的交、并和差运算了。具体来说,对于集合A和B,我们可以别创建两个有序链表,然后进行如下操作: - 并集:遍历A和B两个有序链表,将它们的元素插入到一个新的有序链表中,去重后即可得到A和B的并集。 - 交集:遍历A和B两个有序链表,将它们共有的元素插入到一个新的有序链表中,即可得到A和B的交集。 - 差集:先将B中的元素插入到一个新的有序链表中,然后遍历A中的元素,如果在B中出现过,则从新的链表中删除对应元素,最终得到A和B的差集。 下面是一个简单的实现代码,仅供参考: ``` #include <stdio.h> #include <stdlib.h> typedef struct node { int value; struct node* next; } Node; typedef struct { Node* head; int size; } LinkedList; Node* create_node(int value) { Node* node = (Node*)malloc(sizeof(Node)); node->value = value; node->next = NULL; return node; } void insert_node(LinkedList* list, int value) { Node* node = create_node(value); if (list->head == NULL || value < list->head->value) { node->next = list->head; list->head = node; } else { Node* curr = list->head; while (curr->next != NULL && curr->next->value < value) { curr = curr->next; } node->next = curr->next; curr->next = node; } list->size++; } void delete_node(LinkedList* list, int value) { if (list->head == NULL) { return; } if (list->head->value == value) { Node* tmp = list->head; list->head = list->head->next; free(tmp); list->size--; } else { Node* curr = list->head; while (curr->next != NULL && curr->next->value != value) { curr = curr->next; } if (curr->next != NULL) { Node* tmp = curr->next; curr->next = curr->next->next; free(tmp); list->size--; } } } LinkedList* create_linkedlist() { LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList)); list->head = NULL; list->size = 0; return list; } void destroy_linkedlist(LinkedList* list) { Node* curr = list->head; while (curr != NULL) { Node* tmp = curr; curr = curr->next; free(tmp); } free(list); } void print_linkedlist(LinkedList* list) { printf("{ "); Node* curr = list->head; while (curr != NULL) { printf("%d ", curr->value); curr = curr->next; } printf("}\n"); } LinkedList* set_union(LinkedList* A, LinkedList* B) { LinkedList* C = create_linkedlist(); Node* currA = A->head; Node* currB = B->head; while (currA != NULL || currB != NULL) { if (currB == NULL || (currA != NULL && currA->value < currB->value)) { insert_node(C, currA->value); currA = currA->next; } else if (currA == NULL || (currB != NULL && currB->value < currA->value)) { insert_node(C, currB->value); currB = currB->next; } else { insert_node(C, currA->value); currA = currA->next; currB = currB->next; } } return C; } LinkedList* set_intersection(LinkedList* A, LinkedList* B) { LinkedList* C = create_linkedlist(); Node* currA = A->head; Node* currB = B->head; while (currA != NULL && currB != NULL) { if (currA->value < currB->value) { currA = currA->next; } else if (currB->value < currA->value) { currB = currB->next; } else { insert_node(C, currA->value); currA = currA->next; currB = currB->next; } } return C; } LinkedList* set_difference(LinkedList* A, LinkedList* B) { LinkedList* C = create_linkedlist(); Node* currB = B->head; while (currB != NULL) { insert_node(C, currB->value); currB = currB->next; } Node* currA = A->head; while (currA != NULL) { delete_node(C, currA->value); currA = currA->next; } return C; } int main() { LinkedList* A = create_linkedlist(); LinkedList* B = create_linkedlist(); insert_node(A, 1); insert_node(A, 2); insert_node(A, 3); insert_node(A, 4); insert_node(A, 5); insert_node(A, 6); insert_node(B, 4); insert_node(B, 5); insert_node(B, 6); insert_node(B, 7); insert_node(B, 8); printf("A: "); print_linkedlist(A); printf("B: "); print_linkedlist(B); LinkedList* C = set_union(A, B); printf("A union B: "); print_linkedlist(C); destroy_linkedlist(C); C = set_intersection(A, B); printf("A intersection B: "); print_linkedlist(C); destroy_linkedlist(C); C = set_difference(A, B); printf("A difference B: "); print_linkedlist(C); destroy_linkedlist(C); destroy_linkedlist(A); destroy_linkedlist(B); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值