@@ -67,13 +67,13 @@ tags:
6767
6868### 方法一:一次遍历
6969
70- 我们用变量 $x$ 记录加减次数的差值,用变量 $ans$ 记录操作次数 。
70+ 我们用两个变量 $a$ 和 $b$ 分别记录将 $\textit{nums1}$ 中的元素增加 $k$ 和减少 $k$ 的次数 。
7171
72- 遍历数组,对于每个位置 $i$,如果存在 $k=0$ 并且 $a_i \neq b_i$,则无法使两个数组相等,返回 $-1$。否则,如果 $k \neq 0$,则 $a_i - b_i$ 必须是 $k$ 的倍数,否则无法使两个数组相等,返回 $-1$。接下来,我们更新 $x$ 和 $ans$ 。
72+ 我们遍历两个数组,如果两个指针指向的元素相等,则继续;如果两个指针指向的元素不相等,则如果 $k$ 等于 $0$ 或者两个元素之差不能被 $k$ 整除,则返回 $-1$;否则将两个元素之差除以 $k$ 得到操作数 $t$,如果 $t$ 小于 $0$,则将 $-t$ 加到 $a$ 上,否则将 $t$ 加到 $b$ 上 。
7373
74- 最后,如果 $x \neq 0$,则无法使两个数组相等,返回 $-1$。否则,返回 $\frac{ans}{2} $。
74+ 最后如果 $a$ 和 $b$ 相等,则返回 $a$,否则返回 $-1 $。
7575
76- 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度 。
76+ 时间复杂度 $O(n)$,其中 $n$ 是两个数组的长度。 空间复杂度 $O(1)$。
7777
7878<!-- tabs:start -->
7979
@@ -82,42 +82,42 @@ tags:
8282``` python
8383class Solution :
8484 def minOperations (self , nums1 : List[int ], nums2 : List[int ], k : int ) -> int :
85- ans = x = 0
86- for a, b in zip (nums1, nums2):
87- if k == 0 :
88- if a != b:
89- return - 1
85+ a = b = 0
86+ for x, y in zip (nums1, nums2):
87+ if x == y:
9088 continue
91- if (a - b ) % k:
89+ if k == 0 or (x - y ) % k:
9290 return - 1
93- y = (a - b) // k
94- ans += abs (y)
95- x += y
96- return - 1 if x else ans // 2
91+ t = (x - y) // k
92+ if t < 0 :
93+ a += - t
94+ else :
95+ b += t
96+ return a if a == b else - 1
9797```
9898
9999#### Java
100100
101101``` java
102102class Solution {
103103 public long minOperations (int [] nums1 , int [] nums2 , int k ) {
104- long ans = 0 , x = 0 ;
104+ long a = 0 , b = 0 ;
105105 for (int i = 0 ; i < nums1. length; ++ i) {
106- int a = nums1[i], b = nums2[i];
107- if (k == 0 ) {
108- if (a != b) {
109- return - 1 ;
110- }
106+ int x = nums1[i], y = nums2[i];
107+ if (x == y) {
111108 continue ;
112109 }
113- if ((a - b ) % k != 0 ) {
110+ if (k == 0 || (x - y ) % k != 0 ) {
114111 return - 1 ;
115112 }
116- int y = (a - b) / k;
117- ans += Math . abs(y);
118- x += y;
113+ int t = (x - y) / k;
114+ if (t < 0 ) {
115+ a += - t;
116+ } else {
117+ b += t;
118+ }
119119 }
120- return x == 0 ? ans / 2 : - 1 ;
120+ return a == b ? a : - 1 ;
121121 }
122122}
123123```
@@ -128,23 +128,23 @@ class Solution {
128128class Solution {
129129public:
130130 long long minOperations(vector<int >& nums1, vector<int >& nums2, int k) {
131- long long ans = 0, x = 0;
131+ long long a = 0, b = 0;
132132 for (int i = 0; i < nums1.size(); ++i) {
133- int a = nums1[ i] , b = nums2[ i] ;
134- if (k == 0) {
135- if (a != b) {
136- return -1;
137- }
133+ int x = nums1[ i] , y = nums2[ i] ;
134+ if (x == y) {
138135 continue;
139136 }
140- if ((a - b ) % k != 0) {
137+ if (k == 0 || (x - y ) % k != 0) {
141138 return -1;
142139 }
143- int y = (a - b) / k;
144- ans += abs(y);
145- x += y;
140+ int t = (x - y) / k;
141+ if (t < 0) {
142+ a += -t;
143+ } else {
144+ b += t;
145+ }
146146 }
147- return x == 0 ? ans / 2 : -1;
147+ return a == b ? a : -1;
148148 }
149149};
150150```
@@ -153,58 +153,50 @@ public:
153153
154154```go
155155func minOperations(nums1 []int, nums2 []int, k int) int64 {
156- ans, x := 0, 0
157- for i, a := range nums1 {
158- b := nums2[i]
159- if k == 0 {
160- if a != b {
161- return -1
162- }
156+ var a, b int64
157+ for i, x := range nums1 {
158+ y := nums2[i]
159+ if x == y {
163160 continue
164161 }
165- if (a-b )%k != 0 {
162+ if k == 0 || (x-y )%k != 0 {
166163 return -1
167164 }
168- y := (a - b ) / k
169- ans += abs(y)
170- x += y
171- }
172- if x != 0 {
173- return -1
165+ t := (x - y ) / k
166+ if t < 0 {
167+ a += int64(-t)
168+ } else {
169+ b += int64(t)
170+ }
174171 }
175- return int64(ans / 2)
176- }
177-
178- func abs(x int) int {
179- if x < 0 {
180- return -x
172+ if a == b {
173+ return a
181174 }
182- return x
175+ return -1
183176}
184177```
185178
186179#### TypeScript
187180
188181``` ts
189182function minOperations(nums1 : number [], nums2 : number [], k : number ): number {
190- const n = nums1 .length ;
191- if (k === 0 ) {
192- return nums1 .every ((v , i ) => v === nums2 [i ]) ? 0 : - 1 ;
193- }
194- let sum1 = 0 ;
195- let sum2 = 0 ;
196- for (let i = 0 ; i < n ; i ++ ) {
197- const diff = nums1 [i ] - nums2 [i ];
198- sum1 += diff ;
199- if (diff % k !== 0 ) {
183+ let [a, b] = [0 , 0 ];
184+ for (let i = 0 ; i < nums1 .length ; ++ i ) {
185+ const [x, y] = [nums1 [i ], nums2 [i ]];
186+ if (x === y ) {
187+ continue ;
188+ }
189+ if (k === 0 || (x - y ) % k !== 0 ) {
200190 return - 1 ;
201191 }
202- sum2 += Math .abs (diff );
203- }
204- if (sum1 !== 0 ) {
205- return - 1 ;
192+ const t = (x - y ) / k ;
193+ if (t < 0 ) {
194+ a += - t ;
195+ } else {
196+ b += t ;
197+ }
206198 }
207- return sum2 / ( k * 2 ) ;
199+ return a === b ? a : - 1 ;
208200}
209201```
210202
@@ -213,29 +205,27 @@ function minOperations(nums1: number[], nums2: number[], k: number): number {
213205``` rust
214206impl Solution {
215207 pub fn min_operations (nums1 : Vec <i32 >, nums2 : Vec <i32 >, k : i32 ) -> i64 {
216- let k = k as i64 ;
217- let n = nums1 . len ();
218- if k == 0 {
219- return if nums1 . iter (). enumerate (). all (| (i , & v )| v == nums2 [i ]) {
220- 0
221- } else {
222- - 1
223- };
224- }
225- let mut sum1 = 0 ;
226- let mut sum2 = 0 ;
227- for i in 0 .. n {
228- let diff = (nums1 [i ] - nums2 [i ]) as i64 ;
229- sum1 += diff ;
230- if diff % k != 0 {
208+ let mut a : i64 = 0 ;
209+ let mut b : i64 = 0 ;
210+ for (& x , & y ) in nums1 . iter (). zip (nums2 . iter ()) {
211+ if x == y {
212+ continue ;
213+ }
214+ if k == 0 || (x - y ) % k != 0 {
231215 return - 1 ;
232216 }
233- sum2 += diff . abs ();
217+ let t = (x - y ) / k ;
218+ if t < 0 {
219+ a += (- t ) as i64 ;
220+ } else {
221+ b += t as i64 ;
222+ }
234223 }
235- if sum1 != 0 {
236- return - 1 ;
224+ if a == b {
225+ a
226+ } else {
227+ - 1
237228 }
238- sum2 / (k * 2 )
239229 }
240230}
241231```
@@ -244,28 +234,23 @@ impl Solution {
244234
245235``` c
246236long long minOperations (int* nums1, int nums1Size, int* nums2, int nums2Size, int k) {
247- if (k == 0) {
248- for (int i = 0; i < nums1Size; i++ ) {
249- if ( nums1[ i] ! = nums2[ i] ) {
250- return -1;
251- }
237+ long long a = 0, b = 0;
238+ for (int i = 0; i < nums1Size; ++i ) {
239+ int x = nums1[ i] , y = nums2[ i] ;
240+ if (x == y) {
241+ continue;
252242 }
253- return 0;
254- }
255- long long sum1 = 0;
256- long long sum2 = 0;
257- for (int i = 0; i < nums1Size; i++) {
258- long long diff = nums1[ i] - nums2[ i] ;
259- sum1 += diff;
260- if (diff % k != 0) {
243+ if (k == 0 || (x - y) % k != 0) {
261244 return -1;
262245 }
263- sum2 += llabs(diff);
264- }
265- if (sum1 != 0) {
266- return -1;
246+ int t = (x - y) / k;
247+ if (t < 0) {
248+ a += -t;
249+ } else {
250+ b += t;
251+ }
267252 }
268- return sum2 / (k * 2) ;
253+ return a == b ? a : -1 ;
269254}
270255```
271256
0 commit comments