/
IntersectionOfTwoArrays_349.m
43 lines (32 loc) · 1.06 KB
/
IntersectionOfTwoArrays_349.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// IntersectionOfTwoArrays_349.m
// LeetCode_Solutions
//
// Created by 来自遥远星系的核心巡洋舰 on 16/10/13.
// Copyright © 2016年 KnightJoker. All rights reserved.
//
#import "IntersectionOfTwoArrays_349.h"
@implementation IntersectionOfTwoArrays_349
- (NSArray *)intersection:(NSArray *)nums1 andNun2:(NSArray *)nums2 {
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (int i = 0; i < nums1.count; i++) {
for (int j = 0; j <nums2.count; j++) {
if ([nums1[i] integerValue] == [nums2[j] integerValue]) {
[tempArray addObject:nums2[j]];
}
}
}
[self removeArraySameObject:tempArray];
return tempArray;
}
- (void)removeArraySameObject:(NSMutableArray *)array {
for (int i = 0; i <array.count; i++) {
for (int j = i + 1; j < array.count; j++) {
if ([array[i] integerValue] == [array[j] integerValue]) {
[array removeObjectAtIndex:j];
[self removeArraySameObject:array];
}
}
}
}
@end