Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created June 9, 2020 04:47
Show Gist options
  • Save misterpoloy/d25155b8f9da1a591ff1aec936a1fcc7 to your computer and use it in GitHub Desktop.
Save misterpoloy/d25155b8f9da1a591ff1aec936a1fcc7 to your computer and use it in GitHub Desktop.
#CodeChallenge Search in sorted matrix use of column index and row index.
#include <vector>
using namespace std;
vector<int> searchInSortedMatrix(vector<vector<int>> matrix, int target) {
int col = matrix[0].size() - 1;
int row = 0;
while (row < matrix.size() && col >= 0) {
if (target < matrix[row][col]) {
col--;
} else if (target > matrix[row][col]) {
row++;
} else {
return { row, col };
}
}
return { -1, -1 };
}
@misterpoloy
Copy link
Author

challenge

Search in sorted matrix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment