File tree Expand file tree Collapse file tree 3 files changed +57
-43
lines changed Expand file tree Collapse file tree 3 files changed +57
-43
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public int [][] flipAndInvertImage (int [][] A ) {
3
- int [][] ans = new int [A .length ][A [0 ].length ];
4
-
5
- for (int i =0 ; i <A .length ; i ++) {
6
- List <Integer > temp = Arrays .stream (A [i ]).
7
- boxed ().
8
- collect (Collectors .toList ());
9
-
10
- Collections .reverse (temp );
11
- ans [i ] = temp .stream ().mapToInt (e -> e ).toArray ();
12
- }
13
-
14
- for (int i =0 ;i <ans .length ;i ++) {
15
- for (int j =0 ;j <ans [i ].length ;j ++) {
16
- ans [i ][j ] = ans [i ][j ] == 1 ? 0 : 1 ;
17
- }
18
- }
19
-
20
- return ans ;
2
+ public int [][] flipAndInvertImage (int [][] A ) {
3
+ for (int [] arr : A ) {
4
+ reverse (arr );
5
+ invert (arr );
21
6
}
7
+ return A ;
8
+ }
9
+
10
+ private void reverse (int [] arr ) {
11
+ int start = 0 ;
12
+ int end = arr .length - 1 ;
13
+ while (start < end ) {
14
+ int temp = arr [start ];
15
+ arr [start ] = arr [end ];
16
+ arr [end ] = temp ;
17
+ start ++;
18
+ end --;
19
+ }
20
+ }
21
+
22
+ private void invert (int [] arr ) {
23
+ for (int i = 0 ; i < arr .length ; i ++) {
24
+ arr [i ] = arr [i ] ^ 1 ;
25
+ }
26
+ }
22
27
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public int lastStoneWeight (int [] stones ) {
3
- PriorityQueue <Integer > pq = new PriorityQueue <>(new Comparator <Integer >() {
4
- @ Override
5
- public int compare (Integer o1 , Integer o2 ) {
6
- return o2 .compareTo (o1 );
7
- }
8
- });
9
-
10
- for (int stone : stones ) {
11
- pq .add (stone );
12
- }
13
-
14
- while (pq .size () > 1 ) {
15
- int stone1 = pq .poll ();
16
- int stone2 = pq .poll ();
17
-
18
- int resSize = Math .max (stone1 , stone2 ) - Math .min (stone1 , stone2 );
19
-
20
- if (resSize > 0 ) {
21
- pq .add (resSize );
22
- }
23
- }
24
-
25
- return pq .size () == 1 ? pq .poll () : 0 ;
2
+ public int lastStoneWeight (int [] stones ) {
3
+ PriorityQueue <Integer > pq = new PriorityQueue <>(new Comparator <Integer >(){
4
+ public int compare (Integer o1 , Integer o2 ) {
5
+ return o2 - o1 ;
6
+ }
7
+ });
8
+ for (int stone : stones ) {
9
+ pq .add (stone );
26
10
}
11
+ while (pq .size () > 1 ) {
12
+ int y = pq .poll ();
13
+ int x = pq .poll ();
14
+ if (x != y ) {
15
+ pq .add (y - x );
16
+ }
17
+ }
18
+ return pq .isEmpty () ? 0 : pq .poll ();
19
+ }
27
20
}
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] sortArrayByParityII (int [] A ) {
3
+ int start = 1 ;
4
+ for (int i = 0 ; i < A .length ; i += 2 ) {
5
+ if (A [i ] % 2 == 1 ) {
6
+ while (A [start ] % 2 != 0 ) {
7
+ start += 2 ;
8
+ }
9
+ int temp = A [i ];
10
+ A [i ] = A [start ];
11
+ A [start ] = temp ;
12
+ }
13
+ }
14
+ return A ;
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments