diff --git a/solution/0200-0299/0240.Search a 2D Matrix II/README.md b/solution/0200-0299/0240.Search a 2D Matrix II/README.md index ef89315aa877a..ed48b27ea4b41 100644 --- a/solution/0200-0299/0240.Search a 2D Matrix II/README.md +++ b/solution/0200-0299/0240.Search a 2D Matrix II/README.md @@ -95,6 +95,25 @@ class Solution { } ``` +### **TypeScript** + +```ts +function searchMatrix(matrix: number[][], target: number): boolean { + let m = matrix.length, n = matrix[0].length; + let i = m - 1, j = 0; + while (i >= 0 && j < n) { + let cur = matrix[i][j]; + if (cur == target) return true; + if (cur > target) { + --i; + } else { + ++j; + } + } + return false; +}; +``` + ### **C++** ```cpp diff --git a/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md b/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md index aa9077a3e2359..9d11d80485fc6 100644 --- a/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md +++ b/solution/0200-0299/0240.Search a 2D Matrix II/README_EN.md @@ -83,6 +83,25 @@ class Solution { } ``` +### **TypeScript** + +```ts +function searchMatrix(matrix: number[][], target: number): boolean { + let m = matrix.length, n = matrix[0].length; + let i = m - 1, j = 0; + while (i >= 0 && j < n) { + let cur = matrix[i][j]; + if (cur == target) return true; + if (cur > target) { + --i; + } else { + ++j; + } + } + return false; +}; +``` + ### **C++** ```cpp diff --git a/solution/0200-0299/0240.Search a 2D Matrix II/Solution.ts b/solution/0200-0299/0240.Search a 2D Matrix II/Solution.ts new file mode 100644 index 0000000000000..e1e8fd71840bd --- /dev/null +++ b/solution/0200-0299/0240.Search a 2D Matrix II/Solution.ts @@ -0,0 +1,14 @@ +function searchMatrix(matrix: number[][], target: number): boolean { + let m = matrix.length, n = matrix[0].length; + let i = m - 1, j = 0; + while (i >= 0 && j < n) { + let cur = matrix[i][j]; + if (cur == target) return true; + if (cur > target) { + --i; + } else { + ++j; + } + } + return false; +}; \ No newline at end of file