File tree 4 files changed +100
-5
lines changed
4 files changed +100
-5
lines changed Original file line number Diff line number Diff line change 28
28
29
29
## 解法
30
30
<!-- 这里可写通用的实现逻辑 -->
31
-
31
+ 先计算两个数组的差值 ` diff ` ,若 ` diff ` 为奇数,则说明无满足条件的数值,返回空数组。否则,将 ` array2 ` 转为 ` set ` 。然后遍历 ` array1 ` 中的每个数 ` e ` ,若值 ` e - diff ` 在 ` set ` 中,则说明找到满足条件的数值对。
32
32
33
33
### Python3
34
34
<!-- 这里可写当前语言的特殊实现逻辑 -->
35
35
36
36
``` python
37
-
37
+ class Solution :
38
+ def findSwapValues (self , array1 : List[int ], array2 : List[int ]) -> List[int ]:
39
+ diff = sum (array1) - sum (array2)
40
+ if diff & 1 : return []
41
+ diff >>= 1
42
+ s = set (array2)
43
+ for e in array1:
44
+ if (e - diff) in s: return [e, e - diff]
45
+ return []
38
46
```
39
47
40
48
### Java
41
49
<!-- 这里可写当前语言的特殊实现逻辑 -->
42
50
43
51
``` java
44
-
52
+ class Solution {
53
+ public int [] findSwapValues (int [] array1 , int [] array2 ) {
54
+ int diff = sum(array1) - sum(array2);
55
+ if ((diff & 1 ) == 1 ) {
56
+ return new int []{};
57
+ }
58
+ diff >> = 1 ;
59
+ Set<Integer > s = Arrays . stream(array2). boxed(). collect(Collectors . toSet());
60
+ for (int e : array1) {
61
+ if (s. contains((e - diff))) {
62
+ return new int []{e, e - diff};
63
+ }
64
+ }
65
+ return new int []{};
66
+ }
67
+
68
+ private int sum (int [] array ) {
69
+ int res = 0 ;
70
+ for (int e : array) {
71
+ res += e;
72
+ }
73
+ return res;
74
+ }
75
+ }s
45
76
```
46
77
47
78
### ...
Original file line number Diff line number Diff line change 54
54
### Python3
55
55
56
56
``` python
57
-
57
+ class Solution :
58
+ def findSwapValues (self , array1 : List[int ], array2 : List[int ]) -> List[int ]:
59
+ diff = sum (array1) - sum (array2)
60
+ if diff & 1 : return []
61
+ diff >>= 1
62
+ s = set (array2)
63
+ for e in array1:
64
+ if (e - diff) in s: return [e, e - diff]
65
+ return []
58
66
```
59
67
60
68
### Java
61
69
62
70
``` java
63
-
71
+ class Solution {
72
+ public int [] findSwapValues (int [] array1 , int [] array2 ) {
73
+ int diff = sum(array1) - sum(array2);
74
+ if ((diff & 1 ) == 1 ) {
75
+ return new int []{};
76
+ }
77
+ diff >> = 1 ;
78
+ Set<Integer > s = Arrays . stream(array2). boxed(). collect(Collectors . toSet());
79
+ for (int e : array1) {
80
+ if (s. contains((e - diff))) {
81
+ return new int []{e, e - diff};
82
+ }
83
+ }
84
+ return new int []{};
85
+ }
86
+
87
+ private int sum (int [] array ) {
88
+ int res = 0 ;
89
+ for (int e : array) {
90
+ res += e;
91
+ }
92
+ return res;
93
+ }
94
+ }
64
95
```
65
96
66
97
### ...
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] findSwapValues (int [] array1 , int [] array2 ) {
3
+ int diff = sum (array1 ) - sum (array2 );
4
+ if ((diff & 1 ) == 1 ) {
5
+ return new int []{};
6
+ }
7
+ diff >>= 1 ;
8
+ Set <Integer > s = Arrays .stream (array2 ).boxed ().collect (Collectors .toSet ());
9
+ for (int e : array1 ) {
10
+ if (s .contains ((e - diff ))) {
11
+ return new int []{e , e - diff };
12
+ }
13
+ }
14
+ return new int []{};
15
+ }
16
+
17
+ private int sum (int [] array ) {
18
+ int res = 0 ;
19
+ for (int e : array ) {
20
+ res += e ;
21
+ }
22
+ return res ;
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findSwapValues (self , array1 : List [int ], array2 : List [int ]) -> List [int ]:
3
+ diff = sum (array1 ) - sum (array2 )
4
+ if diff & 1 : return []
5
+ diff >>= 1
6
+ s = set (array2 )
7
+ for e in array1 :
8
+ if (e - diff ) in s : return [e , e - diff ]
9
+ return []
You can’t perform that action at this time.
0 commit comments