Detailed Explanation of Problem 1 of "Sword Finger Offer"
Title: "In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function , Input such a two-dimensional array and an integer, and judge whether the integer is contained in the array."
Article Directory
1. Python implementation
1.1 Violent realization
Traverse the array directly to determine whether the target exists.
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
l1=len(array)
for i in range(l1):
for j in range(len(array[i])):
if array[i][j]==target:
return True
Or the following
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
l1=len(array)
for i in range(l1):
if target in array[i]:
return True
Complexity analysis
Time complexity: O(n^2), because in the worst case, all elements in the array need to be traversed once.
Space complexity: O(1)
1.2 Binary search (different from one-dimensional binary search)
The array increases from left to right and from top to bottom.
In other words, this is an ordered array, and dichotomy is the best choice.
Assume that the arr array, val, and tar are as shown in the following figure:
if we set the dichotomous value at the upper right corner or the lower left corner, we can perform dichotomies.
1.2.1 The initial value is in the upper right corner
1) Set the initial value to the upper right element, arr[0][5] = val, target tar = arr[3][1]
2) Next, perform a dichotomous operation:
3) If val == target, return directly to
4) If tar> val, it means that the target is at a larger position, and the elements to the left of val are obviously <val, indirectly <tar, indicating that the 0th line is invalid, so val moves down to arr[1][5]
5) If tar <val, it means that the target is at a smaller position, and the elements under val are obviously> val, indirectly> tar, indicating that the fifth column is invalid, so val is moved to the left to arr[0][4]
6) Continue to step 2)
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
l1 = len(array)
l2 = len(array[0])
if l1 == 0 | l2 == 0:
return False
i = 0
r = l2 - 1
while r >= 0 and i < l1:
if target < array[i][r]:
r-=1
elif target > array[i][r]:
i+=1
else:
return True
return False
Complexity analysis
Time complexity: O(m+n), where m is the number of rows and n is the number of columns. In the worst case, it needs to traverse m+n times.
Space complexity: O(1)
1.2.2 The initial value is in the lower right corner
1) Set the initial value to the lower left element, arr[4][0] = val, target tar = arr[3][1]
2) Next, perform a dichotomous operation:
3) If val == target, return directly to
4) If tar> val, it means that the target is in a larger position, and the elements to the right of val are obviously> val, and indirectly> tar, which means that it is either in the last line or not, so val is moved right to arr[4][1]
5) If tar <val, it means that the target is in a smaller position. The elements above val are obviously <val, and indirectly <tar, which means that the last column is invalid, so val is moved up to arr[3][1]
6) Continue the steps 2)
def Find(target, array):
# write code here
l1 = len(array)
l2 = len(array[0])
if l1 == 0 | l2 == 0:
return False
i = l1-1
r = 0
while r < l2 and i >= 0:
if target < array[i][r]:
i-=1
elif target > array[i][r]:
r+=1
else:
return True
return False
print(Find(8,[[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]))
2. Java binary implementation
2.1 The initial value is in the upper right corner
public class Solution {
public boolean Find(int target, int [][] array) {
int l=0;
int r=array[0].length-1;
if (array.length==0 || r==0) {
return false;
}
while (l<array.length && r>=0) {
if (target<array[l][r]) {
r--;
}else if (target>array[l][r]) {
l++;
}else {
return true;
}
}
return false;
}
}
运行时间:95ms
占用内存:17428k
2.2 The initial value is in the lower left corner
public class Solution {
public boolean Find(int target, int [][] array) {
int l = array.length-1;
int r = 0;
if (array.length == 0 || array[0].length == 0) {
return false;
}
while (l>=0 && r<array[0].length) {
if (target<array[l][r]) {
l--;
}else if (target>array[l][r]) {
r++;
}else {
return true;
}
}
return false;
}
}