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. JS 导出Table为excel的三种可行方法

    [html] view plain copy<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...

  2. 把数据库里面的stu表中的数据,导出到excel中

    # 2.写代码实现,把我的数据库里面的stu表中的数据,导出到excel中 #编号 名字 性别 # 需求分析:# 1.连接好数据库,写好SQL,查到数据 [[1,'name1','男'],[1,'na ...

  3. centOS7安装RabbitMQ及python实例

    1.rabbitmq是有erlang开发的,所以首先要先安装erlang rpm -ivh erlang-18.1-1.el7.centos.x86_64.rpm rpm -ivh rabbitmq- ...

  4. firewalld实现网关功能

    用ip a查看自己的路由服务器接口,一个外网接口 wan ,还有内网接口: 我这里是一块网卡,配了一个虚拟ip,eth0: wan口   eth0:1 lan口 注意:要注意顺序,先将接口加到zone ...

  5. 制作rpm包报错问题

    在编译生成rpm包时,报错 ERROR 0001: file '***' contains a standard rpath '/usr/lib64'这是由于代码理由hardcode导致,解决问题有两 ...

  6. sublime livereload插件

    1.首先在chrome商店下载livereload 安装之后记得在 chrome 的 扩展程序 里面 勾上 允许访问文件地址 2.sublime text 3 中下载插件 Livereload 3.配 ...

  7. PHP curl post header

    第三方教程推荐:https://www.cnblogs.com/CHEUNGKAMING/p/5717429.html

  8. 一家O2O社区App公司的随笔

    近两年,O20是业内最火爆的关键词之一,去年拿到1亿天使投资的叮咚小区都引起了各方的关注. 一个月之前我入职了一家以做O2O社区产品的创业公司,此文会把这段时间和后边所见所想都写出来,总结创业公司和创 ...

  9. 《TomCat与Java Web开发技术详解》(第二版) 第五章节的学习总结 ---- Servlet的高级用法

    这一章节主要是介绍了Servlet技术的一些高级用法,如下是我自己的整理归纳 1.下载文件:即获取服务器文件,并把文件写入反馈给客户端 ServletContext.getResourceAsStre ...

  10. 不可忽略的apache 的 Keep Alive

    转载链接:http://hi.baidu.com/jx_iben/item/d5fe91feed74495ec9f337f1 在网页开发过程中,Keep-Alive是HTTP协议中非常重要的一个属性. ...