C/C++ 数组面试算法题

1.将一个数组逆序输出

https://blog.csdn.net/qq_45385706/article/details/110739961

1 #include
 2 
 3 #define N 9
 4 
 5 int main()
 6 {
 7     int a[N] = {1,2,3,4,5,6,7,8,9};
 8     for(int i = 0;i

C++实现:

1 #include 
 2 #include 
 3 #include 
 4 using namespace std;
 5 
 6 int main() {
 7    
 8    vector ans = {1,2,3,4,5,6};
 9    reverse(ans.begin(),ans.end());
10    for(int i = 0;i

2.有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。

 https://blog.csdn.net/qq_45385706/article/details/110739053

 1 #include
 2 
 3 int main()
 4 {
 5     int x,temp,top;
 6     int a[11]={1,4,6,9,13,16,19,28,40,100};
 7     printf("输入一个数\n");
 8     scanf("%d",&x);
 9     for(int i = 0;i<10;i++)
10     {
11         if(x < a[i])
12         {
13             temp = a[i];
14             a[i] = x;
15             for(int j = i+1;j<11;j++)
16             {
17                 top = a[j];
18                 a[j] = temp;
19                 temp = top;
20             }
21             break;
22         }
23     }
24     
25     for(int i = 0;i<11;i++)
26     {
27         printf("%d ",a[i]);
28     }
29     
30     return 0;
31 }

3.九十度旋转矩阵

 https://blog.csdn.net/weixin_46617214/article/details/124230458

1 #include
 2 
3 #define N 3
4 #define M 3
5 
6 int main()
7 {
8     int a[M][N] = {{1,2,3},{4,5,6},{7,8,9}};
9     
10     for(int i = 0;i

C++实现:

1 #include 
 2 #include 
 3 using namespace std;
 4 
 5 class node{
 6 public:
 7     vector> longset(vector>& ans){
 8          int x = 0,y = ans.size()-1;
 9          while(x> ans;
25    ans.push_back({1,2,3});
26    ans.push_back({4,5,6});
27    ans.push_back({7,8,9});
28    ans = n.longset(ans);
29    for(int i = 0;i

4.给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。

https://blog.csdn.net/Mu_Muxi_/article/details/120708455

1 #include 
 2 
 3 #define N 3
 4 #define M 3
 5 
 6 int main()
 7 {
 8     int a[N][M] = {{1,1,1},{1,0,1},{1,1,1}};
 9     int b[10],c[10];
10     for(int i = 0;i

C++实现:

1 #include 
 2 #include 
 3 using namespace std;
 4 
 5 class node{
 6 public:
 7     vector> longget(vector>& ans){
 8          int m = ans.size(),n = ans[0].size();
 9          for(int i = 0;i> ans;
31     ans.push_back({1,1,1});
32     ans.push_back({1,0,1});
33     ans.push_back({1,1,1});
34     ans = n.longget(ans);
35     for(int i = 0;i

5.搜索二维矩阵

https://blog.csdn.net/qq_47406941/article/details/110091759

编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

  • 每行中的整数从左到右按升序排列。

  • 每行的第一个整数大于前一行的最后一个整数。

1 #include 
 2 
 3 #define N 3
 4 #define M 4
 5 
 6 int main()
 7 {
 8     int x,i = 0,j = M -1;
 9     int a[N][M] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
10     printf("输入一个要查找的数\n");
11     scanf("%d",&x);
12     while(1)
13     {
14         if(x == a[i][j])
15         {
16             printf("找到了\n");
17             break;
18         }
19         else if(x > a[i][j])
20         {
21             if(i < N-1)
22             {
23                 i++;
24             }
25             else
26             {
27                 printf("没找到\n");
28                 break;   
29             }
30         }
31         else
32         {
33             if(j > 0)
34             {
35                 j--;
36             }
37             else
38             {
39                 printf("没找到\n");
40                 break;   
41             }
42         }
43     }
44 
45     return 0;
46 }

c++实现:

1 #include 
 2 #include 
 3 using namespace std;
 4 
 5 class node{
 6 public:
 7      bool search(vector>& matrix,int target){
 8         int m = matrix.size(),n = matrix[0].size();
 9         int l = 0,r = m*n-1;
10         while(l=target) r = mid;
13            else l = mid + 1;
14         }
15         return matrix[r/n][r%n] == target;
16      }
17 };
18 
19 int main(){
20 
21    node n;
22    vector> cur;
23    cur.push_back({1,3,5,7});
24    cur.push_back({10,11,16,20});
25    cur.push_back({23,30,34,60});
26    cout << n.search(cur,11) << endl;
27    
28    return 0;
29 }

6.两数之和

输入一个数为数组里面两个数之和。

https://blog.csdn.net/weixin_43176384/article/details/86373778

1 #include 
 2 #include 
 3 
 4 #define N 4
 5 
 6 int main() 
 7 {
 8     int i = 0,j = 0,x = 0,z = 0,y = 0;
 9     int a[N]={2,7,11,15};
10     printf("输入一个数是数组两数之和\n");
11     scanf("%d",&x);
12     for(i = 0;i

 C++实现:

1 #include
 2 #include
 3 #include
 4 using namespace std;
 5 
 6 class node{
 7 public:
 8     vector twosun(vector& nums,int target)
 9     {
10         unordered_map record;
11         for(int i = 0;i cur;
26     vector nums = {2,7,11,15};
27     cur = n.twosun(nums,9);
28     for (auto i : cur)
29         cout << i << endl;
30 
31     return 0;
32 }

7.顺时针打印二维数组

https://blog.csdn.net/qq_44443986/article/details/109738061

1 #include 
 2 #include 
 3 
 4 #define N 3
 5 #define M 3
 6 
 7 int main() 
 8 {
 9     int arr[N][M] = {{1,2,3},{4,5,6},{7,8,9}};
10     int a = 0,b = N-1,c = 0,d = M-1,k = 0;
11     while(1)
12     {
13         for(int i = a;i<=b;i++)
14         {
15             printf("%d ",arr[c][i]);
16         }
17         if(c++ > d)break;
18         
19         for(int i = c;i<=d;i++)
20         {
21             printf("%d ",arr[i][b]);
22         }
23         if(b-- < a)break;
24         
25         for(int i = b;i>=a;i--)
26         {
27             printf("%d ",arr[d][i]);
28         }
29         if(d-- < c)break;
30         
31         for(int i = d;i>=c;i--)
32         {
33             printf("%d ",arr[i][a]);
34         }
35         if(a++ > b)break;
36     }
37     
38     return 0;
39 }

c++实现:

1 #include 
 2 #include 
 3 using namespace std;
 4 
 5 class node{
 6 public:
 7      vector setzore(vector>& cur){
 8         int m = cur.size(),n = cur[0].size();
 9         int up = 0,down = m-1,left = 0,right = n-1;
10         vector ans;
11         while(1){
12            for(int i = left;i<=right;i++) ans.push_back(cur[up][i]);
13            if(++up>down) break;
14            for(int i = up;i<=down;i++) ans.push_back(cur[i][right]);
15            if(--right=left;i--) ans.push_back(cur[down][i]);
17            if(--down=up;i--) ans.push_back(cur[i][left]);
19            if(++left>right) break;
20         }
21          return ans;
22      }
23 };
24 
25 int main(){
26 
27    node n;
28    vector ans;
29    vector> cur;
30    cur.push_back({1,2,3});
31    cur.push_back({4,5,6});
32    cur.push_back({7,8,9});
33    ans = n.setzore(cur);
34    for(int i = 0;i

8.三数之和(力扣15题) 

1 #include 
 2 #include 
 3 #include
 4 using namespace std;
 5 
 6 class node{
 7 public:
 8     vector> threesum(vector& nums){
 9         vector>ans;
10         sort(nums.begin(),nums.end());
11         for(int i = 0;i0 && nums[i] == nums[i+1]) continue;
13             int l = i+1,r = nums.size() -1;
14             int target = 0 - nums[i];
15             while(ltarget){
22                     r--;
23                 }else{
24                     l++;
25                 }
26             }
27         }
28         return ans;
29     }
30 };
31 
32 int main() 
33 {
34     node n;
35     vector> cur;
36     vector nums = {-1,0,1,2,-1,-4};
37     cur = n.threesum(nums);
38     int x = cur[0].size(),y = cur.size();
39     for(int i = 0;i

9.四数之和(力扣18题) 

1 #include 
 2 #include 
 3 #include 
 4 using namespace std;
 5 
 6 class node{
 7 public:
 8     vector> forsun(vector& nums,int target){
 9         vector> ans;
10         sort(nums.begin(),nums.end());
11         for(int i = 0;i0 && nums[i] == nums[i-1]) continue;
13             for(int j = i+1;j sum){
23                          r--;
24                      }else{
25                          l++;
26                      }
27                 }
28             }
29         }
30         return ans;
31     }
32 };
33 
34 int main()
35 {
36    node n;
37    vector> top;
38    vector cur = {1,0,-1,0,-2,2};
39    top = n.forsun(cur,0);
40    int x = top.size(),y = top[0].size();
41    for(int i = 0;i

10.删除数组中的重复元素,使每个元素最毒只能出现2次。(力扣80题)

1 #include 
 2 #include 
 3 using namespace std;
 4 
 5 class node1{
 6 public:
 7     int remove(vector& nums){
 8        if(nums.size() <= 2) return nums.size();
 9        int idx = 1;
10        for(int i = 2;i ans = {1,1,1,2,2,3};
23    cout << n.remove(ans) << endl;
24     
25    return 0;
26 }

11.删除数组中的重复元素,使每个元素最多出现一次(力扣26题)

1 #include 
 2 #include 
 3 using namespace std;
 4 
 5 class node1{
 6 public:
 7     int remove(vector& nums){
 8        if(nums.size() <= 2) return nums.size();
 9        int idx = 0;
10        for(int i = 1;i ans = {1,1,1,2,2,3};
23    cout << n.remove(ans) << endl;
24     
25    return 0;
26 }

你可能感兴趣的:(面试,c语言,面试,算法)