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 ...
随机推荐
- 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
[015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...
- 【海盗派测试分析MFQ&PPDCS】读书笔记
使用脑图花了一张读书笔记,可能有点长
- 钩子编程(HOOK) 安装进程内键盘钩子 (1)
摘要:钩子能够监视系统或进程中的各种事件消息.截获发往目标窗体的消息并进行处理.这样,我们就能够在系统中安装自己定义的钩子,监视系统中特定事件的发生.完毕特定的功能,比方截获键盘.鼠标的输入.屏幕取词 ...
- UVA 11354 - Bond (最小生成树 + 树链剖分)
题目链接~~> 做题感悟:这题開始看到时感觉不是树不优点理,一想能够用 Kruskal 处理成树 ,然后就好攻克了. 解题思路: 先用 Kruskal 处理出最小生成树.然后用树链剖分 + 线段 ...
- linux命令的别名alias,unalias
1. 别名 linux别名alias的作用: 1. 简化特别长得命令和參数 2. 对一些命令添加默认选项.提高安全性. 2. alias使用 [www@work sh]$ alias lm='ls - ...
- Away3D引擎学习笔记(二)CameraController相机控制的应用
cameraController---相机控制器 这里针对Away3D里面封装的CameraController的区别和使用做些简单介绍.相机控制器的设计思路比较清晰,所以难点东西不多.使用方面附上源 ...
- 安装wamp后 异常Exception Exception in module wampmanager.exe at 000F15A0
系统环境:Windows 2008 R2 64bit 安装环境:wampserver2.4-x64 按照正常windows安装程序,完成WAMP Server程序安装,安装完成启动WAMP Serve ...
- Cassandra安装和初次使用
Cassandra安装和初次使用 卡珊德拉(Cassandra)又译卡桑德拉.卡珊卓,为希腊.罗马神话中特洛伊(Troy)的公主,阿波罗(Apollo)的祭司.因神蛇以舌为她洗耳或阿波罗的赐予而有预言 ...
- HTTP响应头缓存控制
在一般Web开发中,有时需要设置特殊的HTTP响应头阻止客户端(一般是浏览器)缓存(使用)该次请求的响应. 这时候大部分开发人员都是百度或谷歌几段代码复制粘贴即了事. 以下简述一下关于缓存控制的几种H ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office新的App模型
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps Office新的App模型 Office 2 ...