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 ...
随机推荐
- 谈一谈Http Request 与Http Response
1.什么是HTTPRequest与HTTP Response? 我们平时打开浏览器,输入网址,点击Enter按键,然后我们想要的网页就呈现在我们的眼前,可是这个过程是怎么实现的呢? 简单来说是这样的: ...
- PJISP 修改 消息头Fromto字段
项目需求,需要修改sip信令消息头中Fromto字段,完成此功能需要修改sip库(PJSIP)源码,具体如下: PJSIP 消息头 Formto 字段默认的格式是sip:平台@平台IP地址,例如si ...
- 利用 apache bench 模拟并发请求
示意代码如下 ab -n 1000 -c 10 http://127.0.0.1/ -n 指的是总的请求,默认值是 1 -c 指的是并发数,默认值是 1 -t 指的是测试的总时间,测试所进行的最大秒数 ...
- uboot和内核分区的改动
随着内核的更新,内核越来越大,uboot给nand的kernel分区默认是2M的 device nand0 <nandflash0>, # parts = 4 #: name ...
- HBase概念及表格设计
HBase概念及表格设计 1. 概述(扯淡~) HBase是一帮家伙看了Google发布的一片名为“BigTable”的论文以后,犹如醍醐灌顶,进而“山寨”出来的一套系统. 由此可见: 1. 几乎所有 ...
- 如何搭建maven项目和搭建ssm框架
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- 关于reset.css的那些事
分析过程:body: 发现在没有填写任何内容的情况下是没有margin 的 只有在加入了文字或者div中以后才有了margin(废话)那么 我想问一下 body的padding 默认的有吗??在哪里 ...
- 让WebRTC支持H264编解码
近期实验了下怎样让WebRTC支持H264编码.记录下,供有须要的人參考. 说明一下,我是在 Ubuntu Server 14.04 下编译的 WebRTC ,使用 native(C++) api 开 ...
- [Android Studio 权威教程]AS 中配置强大的版本号管理系统(Git、SVN、等)
在Eclipse中加入Git等版本号管理工具须要自己加入插件.并且个人认为不咋好用,在AS中已经给我们集成好了,我们仅仅须要配置一下就OK了.今天就和大家聊聊怎么配置以及使用的要点. 1. 安装Git ...
- Myeclipse 选中高亮
打开显示功能 选择Windows->Preferences->Java-> Editor-> Mark Occurrences ,勾选选项.这时,当你单击一个元素的时候,代码中 ...