1 Star 9 Fork 2

Mancuoj/408-ds

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
images
09.cpp
10.cpp
11.cpp
12.cpp
13.cpp
14.cpp
15.cpp
16.cpp
17.cpp
18.cpp
19.cpp
20.cpp
21.cpp
22.cpp
CMakeLists.txt
README.md
ds.h
test_ds.cpp
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
10.cpp 958 Bytes
一键复制 编辑 原始数据 按行查看 历史
Mancuoj 提交于 3年前 . feat: 10
#include "ds.h"
/**
* 打印数组,返回字符串
*/
std::string print_array(int arr[], int n) {
std::stringstream ss;
for (int i = 0; i < n; i++) {
ss << arr[i];
if (i != n - 1) {
ss << " ";
}
}
return ss.str();
}
/**
* 暴力解:创建一个tmp数组存储[p, n-1]和[0, p-1]
*/
void reverse_bf(int R[], int p, int n) {
int tmp[n], k = 0;
for (int i = p; i < n; i++) {
tmp[k++] = R[i];
}
for (int i = 0; i < p; i++) {
tmp[k++] = R[i];
}
for (int i = 0; i < n; i++) {
R[i] = tmp[i];
}
}
/**
* 最优解:整体逆置,然后部分逆置
*/
void reverse(int R[], int l, int r) {
for (int i = 0; i < (r - l + 1) / 2; i++) {
int tmp = R[l + i];
R[l + i] = R[r - i];
R[r - i] = tmp;
}
}
void reverse_all(int R[], int p, int n) {
reverse(R, 0, p - 1);
reverse(R, p, n - 1);
reverse(R, 0, n - 1);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/mancuojie/408-ds.git
git@gitee.com:mancuojie/408-ds.git
mancuojie
408-ds
408-ds
main

搜索帮助