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

1 .将一个数组逆序输出

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

1 #include<stdio.h>
 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<N/2;i++)
 9     {
10         int temp = a[i];
11         a[i] = a[N-1-i];
12         a[N-1-i] = temp;
13     }
14     
15     for(int i = 0;i<N;i++)
16     {
17         printf("%d ",a[i]);
18     }
19     
20     return 0;
21 }

C++实现:

1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int main() {
 7    
 8    vector<int> ans = {1,2,3,4,5,6};
 9    reverse(ans.begin(),ans.end());
10    for(int i = 0;i<ans.size();i++){
11       cout << ans[i] << " ";
12    }
13 
14    return 0;
15 }

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

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

 1 #include<stdio.h>
 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<stdio.h>
 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<N;i++)
11     {
12         for(int j = i;j<M;j++)
13         {
14             int temp = a[i][j];
15             a[i][j] = a[j][i];
16             a[j][i] = temp;
17         }
18     }
19     
20     for(int i = 0;i<N;i++)
21     {
22         for(int j = 0;j<M/2;j++)
23         {
24             int temp = a[i][j];
25             a[i][j] = a[i][M-1-j];
26             a[i][M-1-j] = temp;
27         }
28     }
29     
30     for(int i = 0;i<N;i++)
31     {
32         for(int j = 0;j<M;j++)
33         {
34             printf("%d ",a[i][j]);
35         }
36         printf("\n");
37     }
38     
39     return 0;
40 }

C++实现:

1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 class node{
 6 public:
 7     vector<vector<int>> longset(vector<vector<int>>& ans){
 8          int x = 0,y = ans.size()-1;
 9          while(x<y){
10            swap(ans[x++],ans[y--]);
11          }
12          for(int i = 0;i<ans.size();i++){
13              for(int j = 0;j<i;j++){
14                  swap(ans[i][j],ans[j][i]);
15              }
16          }
17          return ans;
18     }
19 };
20 
21 int main() {
22    
23    node n;
24    vector<vector<int>> 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<ans.size();i++){
30        for(int j = 0;j<ans[0].size();j++){
31           cout << ans[i][j] << " ";
32        }
33        cout << endl;
34    }
35 
36    return 0;
37 }

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

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

1 #include <stdio.h>
 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<N;i++)
11     {
12         for(int j = 0;j<M;j++)
13         {
14             if(a[i][j] == 0)
15             {
16                 b[i] = 1;
17                 c[j] = 1;
18             }
19         }
20     }
21     
22     for(int i = 0;i<N;i++)
23     {
24         if(1 == b[i])
25         {
26             for(int j = 0;j<M;j++)
27             {
28                 a[i][j] = 0;
29                 if(1 == c[j])
30                 {
31                     for(int k = 0;k<M;k++)
32                     {
33                         a[k][j] = 0;
34                     }
35                 }
36             }
37         }
38     }
39     
40     for(int i = 0;i<N;i++)
41     {
42         for(int j = 0;j<M;j++)
43         {
44             printf("%d ",a[i][j]);
45         }
46         printf("\n");
47     }
48 
49     return 0;
50 }

C++实现:

1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 class node{
 6 public:
 7     vector<vector<int>> longget(vector<vector<int>>& ans){
 8          int m = ans.size(),n = ans[0].size();
 9          for(int i = 0;i<m;i++){
10              for(int j = 0;j<n;j++){
11                  if(0 == ans[i][j]){
12                     ans[0][j] = ans[i][0] = 0;
13                  } 
14              }
15          }
16          for(int i = 1;i<m;i++){
17              for(int j = 1;j<n;j++){
18                  if(ans[0][j]==0 || ans[i][0]==0){
19                     ans[i][j] = 0;    
20                  }
21              }
22          }
23          return ans;
24     }
25 };
26 
27 int main() {
28    
29     node n;
30     vector<vector<int>> 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<ans.size();i++){
36         for(int j = 0;j<ans[0].size();j++){
37            cout << ans[i][j] << " ";
38         }
39         cout << endl;
40     }
41 
42     return 0;
43 }

5.搜索二维矩阵

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

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

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

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

    1 #include <stdio.h>
    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 <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 class node{
 6 public:
 7      bool search(vector<vector<int>>& matrix,int target){
 8         int m = matrix.size(),n = matrix[0].size();
 9         int l = 0,r = m*n-1;
10         while(l<r){
11            int mid = l + (r-l)/2;
12            if(matrix[mid/n][mid%n]>=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<vector<int>> 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 <stdio.h>
 2 #include <string.h>
 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<N-1;i++)
13     {
14         for(j = i+1;j<N-1;j++)
15         {
16             if(x == a[i]+a[j])
17             {
18                 z = i;
19                 y = j;
20                 break;
21             }
22         }
23     }
24     printf("%d+%d之和为%d\n",a[z],a[y],x);
25     
26     return 0;
27 }

C++实现:

1 #include<iostream>
 2 #include<vector>
 3 #include<unordered_map>
 4 using namespace std;
 5 
 6 class node{
 7 public:
 8     vector<int> twosun(vector<int>& nums,int target)
 9     {
10         unordered_map<int,int> record;
11         for(int i = 0;i<nums.size();i++){
12             int num = target - nums[i];
13             if(record.find(num) != record.end()){
14                 return {record[num],i};
15             }
16             record[nums[i]] = i;
17         }
18         return {-1,-1};
19     }
20 };
21 
22 int main()
23 {
24     node n;
25     vector<int> cur;
26     vector<int> 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 <stdio.h>
 2 #include <string.h>
 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 <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 class node{
 6 public:
 7      vector<int> setzore(vector<vector<int>>& cur){
 8         int m = cur.size(),n = cur[0].size();
 9         int up = 0,down = m-1,left = 0,right = n-1;
10         vector<int> 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) break;
16            for(int i = right;i>=left;i--) ans.push_back(cur[down][i]);
17            if(--down<up) break;
18            for(int i = down;i>=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<int> ans;
29    vector<vector<int>> 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<ans.size();i++)
35        cout << ans[i] << " ";
36    
37    return 0;
38 }

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

1 #include <iostream>
 2 #include <vector>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 class node{
 7 public:
 8     vector<vector<int>> threesum(vector<int>& nums){
 9         vector<vector<int>>ans;
10         sort(nums.begin(),nums.end());
11         for(int i = 0;i<nums.size();i++){
12             if(i>0 && nums[i] == nums[i+1]) continue;
13             int l = i+1,r = nums.size() -1;
14             int target = 0 - nums[i];
15             while(l<r){
16                 if(nums[l]+nums[r]==target){
17                     ans.push_back({nums[i],nums[l],nums[r]});
18                     while(l<r && nums[l] == nums[l+1]) l++;
19                     while(l<r && nums[r] == nums[r-1]) r--;
20                     l++,r--;
21                 }else if(nums[l]+nums[r]>target){
22                     r--;
23                 }else{
24                     l++;
25                 }
26             }
27         }
28         return ans;
29     }
30 };
31 
32 int main() 
33 {
34     node n;
35     vector<vector<int>> cur;
36     vector<int> 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<y;i++){
40         for(int j = 0;j<x;j++){
41             cout << cur[i][j];
42         }
43         cout << endl;
44     }
45     
46     return 0;
47 }

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

1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 class node{
 7 public:
 8     vector<vector<int>> forsun(vector<int>& nums,int target){
 9         vector<vector<int>> ans;
10         sort(nums.begin(),nums.end());
11         for(int i = 0;i<nums.size();i++){
12             if(i>0 && nums[i] == nums[i-1]) continue;
13             for(int j = i+1;j<nums.size();j++){
14                 int l = j+1,r = nums.size()-1;
15                 int sum = target - nums[i] - nums[j];
16                 while(l<r){
17                      if(nums[l]+nums[r] == sum){
18                         ans.push_back({nums[i],nums[j],nums[l],nums[r]});
19                         while(l<r && nums[l] == nums[l+1]) l++;
20                         while(l<r && nums[r] == nums[r-1]) r--;
21                         l++,r--;
22                      }else if(nums[l]+nums[r] > 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<vector<int>> top;
38    vector<int> 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<x;i++){
42       for(int j = 0;j<y;j++){
43           cout << top[i][j] << " ";
44       }
45        cout << endl;
46    }
47     
48    return 0;
49 }

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

1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 class node1{
 6 public:
 7     int remove(vector<int>& nums){
 8        if(nums.size() <= 2) return nums.size();
 9        int idx = 1;
10        for(int i = 2;i<nums.size();i++){
11           if(nums[i] != nums[idx-1]){
12               nums[++idx] = nums[i];
13           }
14        }
15         return idx+1;
16     }
17 };
18 
19 int main()
20 {
21    node1 n;
22    vector<int> ans = {1,1,1,2,2,3};
23    cout << n.remove(ans) << endl;
24     
25    return 0;
26 }

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

1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 class node1{
 6 public:
 7     int remove(vector<int>& nums){
 8        if(nums.size() <= 2) return nums.size();
 9        int idx = 0;
10        for(int i = 1;i<nums.size();i++){
11           if(nums[i] != nums[idx]){
12               nums[++idx] = nums[i];
13           }
14        }
15         return idx+1;
16     }
17 };
18 
19 int main()
20 {
21    node1 n;
22    vector<int> ans = {1,1,1,2,2,3};
23    cout << n.remove(ans) << endl;
24     
25    return 0;
26 }
相关推荐
咕咕吖3 分钟前
对称二叉树(力扣101)
算法·leetcode·职场和发展
理想不理想v9 分钟前
‌Vue 3相比Vue 2的主要改进‌?
前端·javascript·vue.js·面试
九圣残炎35 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
lulu_gh_yu41 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
丫头,冲鸭!!!1 小时前
B树(B-Tree)和B+树(B+ Tree)
笔记·算法
Re.不晚1 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
sszmvb12341 小时前
测试开发 | 电商业务性能测试: Jmeter 参数化功能实现注册登录的数据驱动
jmeter·面试·职场和发展
测试杂货铺1 小时前
外包干了2年,快要废了。。
自动化测试·软件测试·python·功能测试·测试工具·面试·职场和发展
王佑辉1 小时前
【redis】redis缓存和数据库保证一致性的方案
redis·面试
真忒修斯之船1 小时前
大模型分布式训练并行技术(三)流水线并行
面试·llm·aigc