当前位置: 首页 > news >正文

前端面试常考算法

快速排序

#include<iostream>
#include<cstdio>
using namespace std;
const int N = 100005;
int a[N];void quick_sort(int a[], int l, int r) {if (l >= r) return;int x = a[l + r >> 1];int i = l - 1, j = r + 1;while (i < j) {while (a[++i] < x);while (a[--j] > x);if (i < j) swap(a[i], a[j]);}quick_sort(a, l, j);quick_sort(a, j+1, r);
} int main() {int n;cin >> n;for (int i = 0; i < n; i++) {scanf("%d", &a[i]);}quick_sort(a, 0, n - 1);for (int i = 0; i < n; i++) {printf("%d ", a[i]);}return 0;
}

归并排序

#include<iostream>
#include<cstdio>
using namespace std;const int N = 100005;
int a[N], tmp[N];void merge_sort(int a[], int l, int r) {if (l >= r) return;int mid = l + r >> 1;merge_sort(a, l, mid);merge_sort(a, mid+1, r);int i = l, j = mid + 1, k = i;while (i <= mid && j <= r) {if (a[i] < a[j])tmp[k++] = a[i++];elsetmp[k++] = a[j++];}while (i <= mid)tmp[k++] = a[i++];while (j <= r)tmp[k++] = a[j++];for (int i = l; i <= r; i++) {a[i] = tmp[i];}
}int main(){int n;cin >> n;for (int i = 0; i < n; i++) {scanf("%d", &a[i]);}merge_sort(a, 0, n-1);for (int i = 0; i < n; i++) {printf("%d ", a[i]);}cout << endl;return 0;
}

最长上升子序列

#include<iostream>
#include<algorithm>
using namespace std;const int N = 1010;
int a[N], f[N];int main(){int n;cin >> n;for (int i = 1; i <= n; i++)cin >> a[i];for (int i = 1; i <= n; i++) {f[i] = 1;for (int j = 1; j < i; j++) {if (a[j] < a[i])f[i] = max(f[i], f[j] + 1);}}int res = 0;for (int i = 1; i <= n; i++) {res = max(res, f[i]);}cout << res << endl;return 0;
}

最长公共子序列

#include<iostream>
#include<algorithm>
#include<cstring>const int N = 1010;
char a[N], b[N];
int f[N][N];using namespace std;int main(){int n, m;cin >> n >> m;cin >> a+1 >> b+1;for (int i = 1; i <= n; i++ ) {for (int j = 1; j <= m; j++) {f[i][j] = max(f[i-1][j], f[i][j-1]);if (a[i] == b[j]) {f[i][j] = max(f[i][j], f[i-1][j-1] + 1);}}}cout << f[n][m] << endl;return 0;
}
http://www.lryc.cn/news/438602.html

相关文章:

  • 【机试准备】常用容器与函数
  • Base 社区见面会 | 新加坡站
  • 麒麟操作系统搭建Nacos集群
  • Imagination推出性能最高且具有高等级功能安全性的汽车GPU IP
  • 端口大全说明,HTTP,TCP,UDP常见端口对照表
  • dplyr、tidyverse和ggplot2初探
  • pandas:读取各类文件方法以及爬虫时json数据保存
  • 二、(JS)JS中常见的键盘事件
  • 【CSS】样式水平垂直居中
  • 深入理解数据分析的使用流程:从数据准备到洞察挖掘
  • CSS 响应式设计(补充)——WEB开发系列36
  • Qt常用控件——QDateTimeEdit
  • 什么是上拉,下拉?
  • 76-mysql的聚集索引和非聚集索引区别
  • 每日一题——第八十八题
  • 【创作活动】学习使用哪个编程工具让你的工作效率翻倍?
  • 基于STM32C8T6的CubeMX:HAL库点亮LED
  • 职业院校数据科学与大数据技术专业人工智能实训室建设方案
  • JavaScript网页设计案例分析
  • 2024.9.15周报
  • QT模型视图结构1
  • Ubuntu20+Noetic+cartographer_ros编译部署
  • linux-L3-linux 复制文件
  • Kotlin:1.9.0 的新特性
  • golang实现从服务器下载文件到本地指定目录
  • C++数据结构-树的概念及分类介绍(基础篇)
  • 职场 Death Note
  • Vue3.0组合式API:computed计算属性、watch监听器、watchEffect高级监听器
  • RAII 与 std::lock_guard 在 C++ 中的应用:自动化互斥锁管理与线程安全
  • 风格汇:奢华风格在UI设计中如何被定义的。