Data Structure Array: Program for array rotation
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的更多相关文章
- [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 ...
- [Algorithm] Trie data structure
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...
- 面试总结之数据结构(Data Structure)
常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- [LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- 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 ...
- [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 ...
- sklearn中报错ValueError: Expected 2D array, got 1D array instead:
from sklearn.linear_model import LinearRegression lr = LinearRegression() print(tr_x.shape,tr_y.shap ...
- 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:
决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...
随机推荐
- 浅谈struts2标签中的2个非经常常使用的标签的使用方法(radio和select)
1.如图所看到的我们须要在前台的页面通过radio和select将相应的数据库中的数据显示到选项其中,这也是我们做项目中常常须要做的,动态的显示,而不是静态的显示. 首先我们须要在页面中导入strut ...
- 去除inline-block元素间间距的N种方法-zhangxinxu
张鑫旭原文:点这里进入原文 另外附上大漠老师的如何解决inline-block元素的空白间距地址!!! 去除inline-block元素间间距的N种方法: 一.现象描述 真正意义上的inline-bl ...
- Android下ListView的分页(9.6)
1 http://www.cnblogs.com/noTice520/archive/2012/02/10/2345057.html 2 http://www.92coding.com/blog/in ...
- C# readonly和const的不同以及它的具体用法
在C#中,我们用const来定义常量.常量就是我定义一个变量,这个变量的值在整个软件的生命周期都不变.比如我想求一个圆形的面积,我就可以把π定义成一个常量,因为我事先知道圆周率是就是3.1415926 ...
- CentOS修改IP地址及关闭/打开防火墙
1.CentOS修改IP地址: # ifconfig eth0 192.168.1.80 这样就把IP地址修改为192.168.1.80(如果发现上不了网 了,那么你可能需要把网关和DNS也改一下,后 ...
- Git--代码托管/协同开发
Git--代码托管 我爱写代码,公司写,家里写,如果每天来回带一个U盘拷贝着实麻烦,Git有没有类似于云盘似得东西可以进行数据同步呢?答案肯定是有. GitHub,一个基于Git实现的代码托管的平台, ...
- watch 命令
watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...
- rm 命令简要
rm 单独使用只能删除文件不能删除文件夹 rm -r 可以删除文件夹和文件 1.rm test.txt 删除文件 2.rm -r test.txt 每次删除的时候都询问是 ...
- delphi 无边框窗体常见问题
实现无边框窗体很简单,直接将窗体的BorderStyle属性设置为bsNone即可.但是这样会引起2个问题: 1.在xp系统下,任务栏鼠标右键点击无法弹出菜单 解决办法:在FormShow中加入这个过 ...
- 一定要搜藏的20个非常有用的PHP类库
一定要搜藏的20个非常有用的PHP类库 本文提供了20个非常有用的PHP类库的名称和下载地址.这20个PHP类库包含了图标库,RSS解析,缩略图生成,支付,OpenID,数据库抽象,PDF生成器等一系 ...