File tree Expand file tree Collapse file tree 3 files changed +77
-22
lines changed Expand file tree Collapse file tree 3 files changed +77
-22
lines changed Original file line number Diff line number Diff line change
1
+ // Name : Search_a_2D_Matrix_II
2
+ // Leetcode Problem 240 (Medium)
3
+ // Url : https://leetcode.com/problems/search-a-2d-matrix-ii/
4
+
5
+ // Approach 1 (Best Approach)
6
+ class Solution
7
+ {
8
+ public:
9
+ bool searchMatrix (vector<vector<int >> &matrix, int target)
10
+ {
11
+ int j = matrix[0 ].size () - 1 , i = 0 ;
12
+ while (1 )
13
+ {
14
+ if (j < 0 || i > matrix.size () - 1 )
15
+ return 0 ;
16
+ if (matrix[i][j] > target)
17
+ j--;
18
+ else if (matrix[i][j] < target)
19
+ i++;
20
+ else
21
+ return 1 ;
22
+ }
23
+ if (matrix[i][j] == target)
24
+ return 1 ;
25
+ return 0 ;
26
+ }
27
+ };
28
+
29
+ // Approach 2
30
+
31
+ class Solution {
32
+ public:
33
+ bool searchMatrix (vector<vector<int >>& matrix, int target) {
34
+ for (int i=0 ;i<matrix[0 ].size ();i++)
35
+ {
36
+ if (matrix[0 ][i]>target)
37
+ return 0 ;
38
+ int low=0 ,high=matrix.size ()-1 ;
39
+ while (low<high){
40
+ int mid=low+(high-low+1 )/2 ;
41
+ if (matrix[mid][i]<=target)
42
+ low=mid;
43
+ else
44
+ high=mid-1 ;
45
+ }
46
+ if (matrix[low][i]==target)
47
+ return 1 ;
48
+ }
49
+ return 0 ;
50
+ }
51
+ };
Original file line number Diff line number Diff line change
1
+ // Name: Rotate Image(Medium)
2
+ // Url : https://leetcode.com/problems/rotate-image
3
+ class Solution
4
+ {
5
+ public:
6
+ void rotate (vector<vector<int >> &mt)
7
+ {
8
+ int n = mt.size ();
9
+ int tmp = 0 ;
10
+ for (int i = 0 ; i < n; i++)
11
+ {
12
+ for (int j = tmp; j < n; j++)
13
+ {
14
+ swap (mt[i][j], mt[j][i]);
15
+ }
16
+ tmp++;
17
+ }
18
+ for (int i = 0 ; i < n; i++)
19
+ {
20
+ for (int j = 0 ; j < (n / 2 ); j++)
21
+ {
22
+ swap (mt[i][j], mt[i][n - j - 1 ]);
23
+ }
24
+ }
25
+ }
26
+ };
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments