-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 5 solutions leet code challenge
71 lines (62 loc) · 1.89 KB
/
Day 5 solutions leet code challenge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Problem 1 - Valid sudoku- leetcode // c++ solutions
valid sudoku-class Solution {
public:
bool valid(int i,int j,vector<vector<char>>& board){
unordered_set<char> temp;
for(int x=i;x<i+3;x++){
for(int y=j;y<j+3;y++){
if(board[x][y]!='.' and temp.count(board[x][y]))
return false;
temp.insert(board[x][y]);
}
}
return true;
}
bool isValidSudoku(vector<vector<char>>& board) {
for(int i=0;i<9;i+=3){
for(int j=0;j<9;j+=3)
if(!valid(i,j,board))
return false;
}
unordered_set<char> temp;
for(int i=0;i<9;i++){
temp.clear();
for(int j=0;j<9;j++){
if(board[i][j]!='.' and temp.count(board[i][j]))
return false;
temp.insert(board[i][j]);
}
}
for(int j=0;j<9;j++){
temp.clear();
for(int i=0;i<9;i++){
if(board[i][j]!='.' and temp.count(board[i][j]))
return false;
temp.insert(board[i][j]);
}
}
return true;
}
};
// Problem 2 search 2d matrix solution leetcode c++
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int low= 0;
if(!matrix.size()) return false;
int high = (matrix.size() * matrix[0].size()) - 1;
while(low <= high) {
int mid = (low + (high - low) / 2);
if(matrix[mid/matrix[0].size()][mid % matrix[0].size()] == target) {
return true;
}
if(matrix[mid/matrix[0].size()][mid % matrix[0].size()] < target) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return false;
}
};