g0201_0300.s0240_search_a_2d_matrix_ii.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0201_0300.s0240_search_a_2d_matrix_ii
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Matrix
// #Divide_and_Conquer #Data_Structure_II_Day_4_Array #Binary_Search_II_Day_8
// #Big_O_Time_O(n+m)_Space_O(1) #2022_09_10_Time_460_ms_(66.08%)_Space_55.9_MB_(65.19%)
class Solution {
fun searchMatrix(matrix: Array, target: Int): Boolean {
var r = 0
var c: Int = matrix[0].size - 1
while (r < matrix.size && c >= 0) {
if (matrix[r][c] == target) {
return true
} else if (matrix[r][c] > target) {
c--
} else {
r++
}
}
return false
}
}