[每日练习] write a function to find the key in a m*n matrix, where each line and column is incremental
2017-01-20 11:00
931 查看
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
解题思路:从二维数组的右上角的元素开始判断,因为此元素是它所在行的最大数,是它所在的列的最小数。如果它等于要查找的数字,则查找过程结束。如果它大于要查找的数字,则可以排除它所在的列。如果它小于要查找的数字,则可排除它所在的行。这样如果要查找的数字不在数组的右上角,则每次判断都可以排除一行或一列以缩小查找范围,直到找到要查找的数字,或者查找范围为空。
下图是在二维数组中查找7的示意图:
参考代码:
解题思路:从二维数组的右上角的元素开始判断,因为此元素是它所在行的最大数,是它所在的列的最小数。如果它等于要查找的数字,则查找过程结束。如果它大于要查找的数字,则可以排除它所在的列。如果它小于要查找的数字,则可排除它所在的行。这样如果要查找的数字不在数组的右上角,则每次判断都可以排除一行或一列以缩小查找范围,直到找到要查找的数字,或者查找范围为空。
下图是在二维数组中查找7的示意图:
参考代码:
#include int find_in_ordered_2_dimension_array(int *arr, int line_num, int column_num, int key) { int found = 0, ret_line = -1, ret_column = -1; int i = 0, j = column_num - 1; int current_idx; while (i=0) { current_idx = i*column_num + j; if (arr[current_idx] == key) { ret_line = i; ret_column = j; found = 1; break; } else if(arr[current_idx] > key) { j--; } else { i++; } } if (found == 1) { printf("%d has been found at [%d][%d]\n", key, ret_line, ret_column); } else { printf("%d not found\n", key); } return found; } int main() { int a[4][4] = {{1,2,8,9}, {2,4,9,12}, {4,7,10,13}, {6,8,11,15}}; int found = 0; printf("find-in-ordered-2-dimension-array begin\n"); found = find_in_ordered_2_dimension_array((int *)a, 4, 4, 1); found = find_in_ordered_2_dimension_array((int *)a, 4, 4, 2); found = find_in_ordered_2_dimension_array((int *)a, 4, 4, 13); found = find_in_ordered_2_dimension_array((int *)a, 4, 4, 15); found = find_in_ordered_2_dimension_array((int *)a, 4, 4, 7); printf("find-in-ordered-2-dimension-array end\n"); return 0; }
相关文章推荐
- Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
- 【MySQL笔记】解除输入的安全模式,Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.
- MySQL错误:Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL easonjim
- rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnec
- The transaction log for database 'wcadmin' is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.datab
- How to find the exact line position which causes “undefined is not a function" error
- sql: Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database
- The report is used to find enhancement and badi in sap.
- Write one line C function to find whether a no is power of two
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- DeprecationWarning: Calling an asynchronous function without callback is deprecated. - how to find where the “function:” is?
- How to separate the implementation and definition for template function in c++
- SSISThe Address in the "To" line is malformed
- Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s are also using JDK installs
- Matrix and linear algebra in F#, Part IV: profile your program, find the bottleneck and speed it up: using matrix multiplication as an example[z]
- Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s
- EJB3: Mapping of 1-to-1 relationships when primary key in the source table is also a foreign key for the target entity
- In PHP5, what is the difference between using self and $this? When is each appropriate?
- Write a function to find the depth of a binary tree
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.