leetcode : Search a 2D Matrix
runtime 0 ms, beats 100% of cpp submissions
O(log(mn)) solution with explanation
tags: binary search
๐ link
๐ description
็ตฆๅฎไธๅ่ขซๆๅบ้็ 2 ็ถญ้ฃๅ๏ผๆพๅฐ็ฎๆจๅ ็ด ใ
ex. Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true
๐ง solution
ๆฌ้กๅฐฑๆฏไธๅ binary search ็่ฎๅฝข๏ผ่ไธ่ฌๆนๆณไธๅ็ๅชๆๅจๆๅฐๆ้่ฆๅฐ็ดขๅผๅผ่ฝๆๆๅ ฉๅๅผๆธใ
โณ time complexity
ๅ
ฑๆ mn ๅๅ
็ด ๏ผ binary search ็ๆ้่ค้ๅบฆ็บ O(log(mn))
็ธฝๆ้่ค้ๅบฆ O(log(mn))
๐ code
1 | class Solution { |