File tree Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ // time: O(m*n)
2+ // space: O(m+n)
3+ class Solution {
4+
5+ public void setZeroes (int [][] matrix ) {
6+ int m = matrix .length ;
7+ int n = matrix [0 ].length ;
8+
9+ int [] rows = new int [m ];
10+ int [] cols = new int [n ];
11+
12+ for (int i = 0 ; i < m ; i ++) {
13+ for (int j = 0 ; j < n ; j ++) {
14+ if (matrix [i ][j ] == 0 ) {
15+ rows [i ] = 1 ;
16+ cols [j ] = 1 ;
17+ }
18+ }
19+ }
20+
21+ for (int i = 0 ; i < m ; i ++) {
22+ for (int j = 0 ; j < n ; j ++) {
23+ if (rows [i ] == 1 || cols [j ] == 1 ) {
24+ matrix [i ][j ] = 0 ;
25+ }
26+ }
27+ }
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ // time: O(m*n)
2+ // space: O(m*n)
3+ class Solution {
4+
5+ public List <Integer > spiralOrder (int [][] matrix ) {
6+ int m = matrix .length ;
7+ int n = matrix [0 ].length ;
8+ int dir = 1 ;
9+ int i = 0 ;
10+ int j = -1 ;
11+
12+ List <Integer > output = new ArrayList <>();
13+
14+ while (m * n > 0 ) {
15+ for (int k = 0 ; k < n ; k ++) {
16+ j += dir ;
17+ output .add (matrix [i ][j ]);
18+ }
19+
20+ m --;
21+
22+ for (int k = 0 ; k < m ; k ++) {
23+ i += dir ;
24+ output .add (matrix [i ][j ]);
25+ }
26+
27+ n --;
28+ dir *= -1 ;
29+ }
30+
31+ return output ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ // time: O(1)
2+ // space: O(1)
3+ class Solution {
4+
5+ public int getSum (int a , int b ) {
6+ while (b != 0 ) {
7+ int ans = a ^ b ;
8+ int carry = (a & b ) << 1 ;
9+ a = ans ;
10+ b = carry ;
11+ }
12+
13+ return a ;
14+ }
15+ }
You can’t perform that action at this time.
0 commit comments