http://www.geeksforgeeks.org/array-rotation/

O(n), O(1)

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; int gcd(int a, int b) {
return b == ? a : gcd(b, a % b);
} void leftrotate(int arr[], int d, int n) {
for (int i = ; i < gcd(d, n); i++) {
int tmp = arr[i];
int j = i;
while () {
int k = j + d;
if (k >= n) k -= n;
if (k == i) break;
arr[j] = arr[k];
j = k;
}
arr[j] = tmp;
}
} int main() {
int arr[] = {, , , , , , };
leftrotate(arr, , );
for (int i = ; i < ; i++) cout << arr[i] << " ";
return ;
}

O(n), O(1)

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; void reverse(int arr[], int a, int b) {
for (int i = ; i < (b-a)/; i++) swap(arr[a+i], arr[b--i]);
} void leftrotate(int arr[], int d, int n) {
reverse(arr, , d);
reverse(arr, d, n);
reverse(arr, , n);
} int main() {
int arr[] = {, , , , , , };
leftrotate(arr, , );
for (int i = ; i < ; i++) cout << arr[i] << " ";
return ;
}

Data Structure Array: Program for array rotation的更多相关文章

  1. [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  2. [Algorithm] Trie data structure

    For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...

  3. 面试总结之数据结构(Data Structure)

    常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...

  4. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  5. [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  6. UVa 11995:I Can Guess the Data Structure!(数据结构练习)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  7. [UVA] 11995 - I Can Guess the Data Structure! [STL应用]

    11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Str ...

  8. sklearn中报错ValueError: Expected 2D array, got 1D array instead:

    from sklearn.linear_model import LinearRegression lr = LinearRegression() print(tr_x.shape,tr_y.shap ...

  9. 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:

    决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...

随机推荐

  1. ASP.NET综合管理ERP系统100%源代码+所有开发文档

    该系统开发环境为:VS2010,数据库採用SQL Server,框架为ASP.NET. 源代码包含所有文档说明,代码简单易懂,凝视完整. 提示:假设没有安装水晶报表系统执行会报错,报表安装程序已经打包 ...

  2. 深入探析 Rational AppScan Standard Edition 新特性之 Glass Box 扫描

    众所周知,Web 应用安全测试通常有黑盒安全测试和白盒安全测试两种方法.这两种方法孰优孰劣一直众议纷纷.广为公认的是,这两种测试方法有着良好地互补性,两种测试方法的结合是未来安全测试技术的发展趋势.G ...

  3. 改进Spring中的分页技术

    Spring中有一个PagedListHolder,能够实现分页. 但此类有几个缺点: 1. 使用此类的代码比較繁琐 2. 此类存放的数据源是全部的记录集,即对于记录数为1000条的数据,即使我们仅仅 ...

  4. mac虚拟机搭建自动化环境-wda和python wda client

    尽量升级Xcode到最新版,保持iPhone的版本大于9.3 1.安装webDriverAgent到ios真机 从github上下载代码:git clone https://github.com/fa ...

  5. wps文档怎样去除广告

    安装完 WPS 之后右键—属性—打开文件夹位置(如图) 接下来进入 10.1.0.6929 文件夹内,再次进入 office6 文件夹内,找到 wpscenter 应用程序,将其删除.此应用包含定时弹 ...

  6. FFmpeg X264 H264编码指南[译]

    本文目标:如何创建一个高质量的H.264视频 x264 是一个 H.264 编码器. 通常有2种码率控制(rate control)模式:Constant Rate Factor (CRF) or T ...

  7. GreenDao学习

    官方文档地址:http://greenrobot.org/greendao/documentation//introduction/ 英语不好的可以使用谷歌翻译,很轻松的看懂文档,不需要FQ. 看不懂 ...

  8. Shell Error: -bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory (转)

    错误原因可能有以下几种: 1.在WIN底下用文本编辑工具修改过参数变量,在保存的时候没注意编码格式造成的, 2.也有可能是在VIM里修改,第一行末尾按到ctrl_v 查看文件是DOS格式.UNIX格式 ...

  9. selenium实现在新窗口打开链接

    问题:页面代码中不存在target="_blank",怎么实现点击一个按钮,在新窗口中打开? WebElement link = element.findElement(By.ta ...

  10. 图像处理之基础---2个YUV视频 拼接技术

    /************************************************* * 主要功能:两路 YUV4:2:0拼接一路左右半宽格式YUV视频 参考资料:http://www ...