笔试练习

目录

1、完成find函数---在一个二维数组(vector对象)中查找有无一个数字,难点在于我不知道如何获取该二维数组的行数和列数

2、补充:关于C++中vector<vector<int>> A的使用 ****

1、完成find函数---在一个二维数组(vector对象)中查找有无一个数字,难点在于我不知道如何获取该二维数组的行数和列数

 1 class Solution {
 2 public:
 3     bool Find(int target, vector<vector<int> > array) {
 4         //用rowCount、colCount分别代表数组的行数和列数
 5         int rowCount = array.size();
 6         int colCount = array[0].size();
 7         /*判断是否比数组最小的值小,或者比最大的值大,这样在数组很大时可以提高效率。
 8           数组长度为0也是合法的,所以为了避免越界加上列长为0的判断*/
 9         if(colCount == 0 || target < array[0][0] || target > array[rowCount-1][colCount-1]){
10             return false;
11         }
12         //当row大等于0且col小于列长colCount时,查找数组中是否有target
13         for(int row = rowCount-1, col = 0; row >= 0 && col < colCount;){
14             if(target == array[row][col]){
15                 return true;
16             }else if(target < array[row][col]){
17                 row--;
18             }else if(target > array[row][col]){
19                 col++;
20             }
21         }
22         return false;
23     }
24 };
View Code

方法二(遍历法):

 1 链接:https://www.nowcoder.com/questionTerminal/abc3fe2ce8e146608e868a70efebf62e?answerType=1&f=discussion
 2 来源:牛客网
 3 
 4 public class Solution {
 5     public boolean Find(int target, int [][] array) {
 6         //用rowCount、colCount分别代表数组的行数和列数
 7         int rowCount = array.length;
 8         int colCount = array[0].length;
 9         /*判断是否比数组最小的值小,或者比最大的值大,这样在数组很大时可以提高效率。
10           数组长度为0也是合法的,所以为了避免越界加上列长为0的判断*/
11         if(colCount == 0 || target < array[0][0] || target > array[rowCount-1][colCount-1]){
12             return false;
13         }
14         //当row大等于0且col小于列长colCount时,查找数组中是否有target
15         for(int row = rowCount-1, col = 0; row >= 0 && col < colCount;){
16             if(target == array[row][col]){
17                 return true;
18             }else if(target < array[row][col]){
19                 row--;
20             }else if(target > array[row][col]){
21                 col++;
22             }
23         }
24         return false;
25     }
26 }
View Code

2、补充:关于C++中vector<vector<int>> A的使用

以下代码包含了如何向A中插入数据和如何获取A矩阵的行数和列数

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using std::vector;
 5 using std::cout;
 6 using std::endl;
 7 
 8 int main()
 9 {
10     vector<vector<int>> A;
11     //若想定义A = [[0,1,2],[3,4]],有两种方法
12     //方法一:定义vector B分别为[0,1,2]和[3,4],然后放入vector A。
13     //vector<int> B;
14     //B.push_back(0); 
15     //B.push_back(1);
16     //B.push_back(2);
17     //A.push_back(B);  //将B放入A中
18 
19     //B.clear();  //清除B中的元素
20     //B.push_back(3);
21     //B.push_back(4);
22     //A.push_back(B);  //将B放入A中
23 
24     //方法二
25     for (int i = 0; i < 2; ++i)  A.push_back(vector<int>());  //相当于告诉编译器有两行(或分配两个vector对象的空间)
26     A[0].push_back(0);
27     A[0].push_back(1);
28     A[0].push_back(2);
29     A[1].push_back(3);
30     A[1].push_back(4);
31 
32     int rowCount = A.size();  //获取A的行数
33     for (int i = 0; i < rowCount; i++)
34     {
35         for (int j = 0; j < A[i].size(); j++)  //A[i].size()为获取第i行的列数
36             cout << A[i][j] << "\t";
37         cout << endl;
38     }
39 
40 
41     system("pause");
42     return 0;
43 }
View Code

执行结果:

猜你喜欢

转载自www.cnblogs.com/YiYA-blog/p/11516669.html