部分翻转数组golang版本
2023-08-20
1分钟阅读时长
部分翻转数组
func searchMatrix(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{
return true
}
if matrix[x][y] > target{
y--
}else{
x++
}
}
return false
}
func searchMatrix(matrix [][]int,target int) bool{
for _,v := range matrix{
if v[0] == target {
return true
}
if v[0] < target{
if search(v,target) != -1{
return true
}
}
}
return false
}
func search(nums []int,target int) int{
l := 0
r := len(nums) - 1
var mid int
for l <= r{
mid = l + (r - l) / 2
if target == nums[mid]{
return mid
}
if nums[mid] >= nums[l]{
if target < nums[mid] && target >= nums[l]{
r = mid - 1
}else{
l = mid + 1
}
}else{
if target > nums[mid] && target <= nums[r]{
l = mid + 1
}else{
r = mid - 1
}
}
}
return -1
}