diff --git a/CS notes/pages/Leetcode Search-a-2D-Matrix.md b/CS notes/pages/Leetcode Search-a-2D-Matrix.md index e494bb4..eda580f 100644 --- a/CS notes/pages/Leetcode Search-a-2D-Matrix.md +++ b/CS notes/pages/Leetcode Search-a-2D-Matrix.md @@ -62,6 +62,7 @@ Binary search algorithm, with simple 2-d to 1-d conversion > And use: > - l = mid + 1 > - r = mid - 1 +> Init r as m * n - 1 to avoid mid being OOB. ### Solution ==note how I calculated the mid, i, j, and how I changed r and l== @@ -72,6 +73,7 @@ public: int m = matrix.size(); int n = matrix[0].size(); int l = 0; + // set r to len - 1 to avoid mid being bigger than len. int r = m * n - 1; int i; // the key to speed is to precaculate i and j, to save time. int j;