classSolution { public: boolsearchMatrix(vector<vector<int>>& matrix, int target){ for (constauto& row: matrix) { for (int element: row) { if (element == target) { returntrue; } } } returnfalse; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { publicbooleansearchMatrix(int[][] matrix, int target) { for (int[] row : matrix) { for (int element : row) { if (element == target) { returntrue; } } } returnfalse; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12
publicclassSolution { publicboolSearchMatrix(int[][] matrix, int target) { foreach (int[] row in matrix) { foreach (int element in row) { if (element == target) { returntrue; } } } returnfalse; } }
[sol1-Python3]
1 2 3 4 5 6 7
classSolution: defsearchMatrix(self, matrix: List[List[int]], target: int) -> bool: for row in matrix: for element in row: if element == target: returnTrue returnFalse
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10
funcsearchMatrix(matrix [][]int, target int)bool { for _, row := range matrix { for _, v := range row { if v == target { returntrue } } } returnfalse }
classSolution { public: boolsearchMatrix(vector<vector<int>>& matrix, int target){ int m = matrix.size(), n = matrix[0].size(); int x = 0, y = n - 1; while (x < m && y >= 0) { if (matrix[x][y] == target) { returntrue; } if (matrix[x][y] > target) { --y; } else { ++x; } } returnfalse; } };
[sol3-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { publicbooleansearchMatrix(int[][] matrix, int target) { intm= matrix.length, n = matrix[0].length; intx=0, y = n - 1; while (x < m && y >= 0) { if (matrix[x][y] == target) { returntrue; } if (matrix[x][y] > target) { --y; } else { ++x; } } returnfalse; } }
[sol3-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassSolution { publicboolSearchMatrix(int[][] matrix, int target) { int m = matrix.Length, n = matrix[0].Length; int x = 0, y = n - 1; while (x < m && y >= 0) { if (matrix[x][y] == target) { returntrue; } if (matrix[x][y] > target) { --y; } else { ++x; } } returnfalse; } }
[sol3-Python3]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defsearchMatrix(self, matrix: List[List[int]], target: int) -> bool: m, n = len(matrix), len(matrix[0]) x, y = 0, n - 1 while x < m and y >= 0: if matrix[x][y] == target: returnTrue if matrix[x][y] > target: y -= 1 else: x += 1 returnFalse
[sol3-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var searchMatrix = function(matrix, target) { const m = matrix.length, n = matrix[0].length; let x = 0, y = n - 1; while (x < m && y >= 0) { if (matrix[x][y] === target) { returntrue; } if (matrix[x][y] > target) { --y; } else { ++x; } } returnfalse; };
[sol3-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcsearchMatrix(matrix [][]int, target int)bool { m, n := len(matrix), len(matrix[0]) x, y := 0, n-1 for x < m && y >= 0 { if matrix[x][y] == target { returntrue } if matrix[x][y] > target { y-- } else { x++ } } returnfalse }