LeetCode 74. Search a 2D Matrix in F#
URL
leetcode.com/problems/search-a-2d-matrix
Code
github.com/syohex/dotnet-study/blob/master/..
let searchMatrix (matrix: int list list) (target: int) : bool =
let rec toSingleDimentionArray matrix acc : int [] =
match matrix with
| [] -> acc |> List.rev |> List.toArray
| h :: t ->
let acc' = (h |> List.rev) @ acc
toSingleDimentionArray t acc'
let rec searchMatrix' (matrix: int []) left right target =
if left > right then
false
else
let mid = left + (right - left) / 2
if matrix.[mid] = target then
true
elif target < matrix.[mid] then
searchMatrix' matrix left (mid - 1) target
else
searchMatrix' matrix (mid + 1) right target
let right = matrix.Length * matrix.Head.Length - 1
let matrix' = toSingleDimentionArray matrix []
searchMatrix' matrix' 0 right target