Rotate Array
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7]
is rotated to [5,6,7,1,2,3,4]
.
class Solution {
public:
void rotate(int nums[], int n, int k) {
k = k%n;
int* copy = (int*)malloc(sizeof(int)*n);
for(int i=;i<k;++i)
copy[i] = nums[n-k+i];
for(int i=k;i<n;++i)
copy[i] = nums[i-k];
for(int i=;i<n;++i)
nums[i] = copy[i];
free(copy);
}
};
Rotate Array的更多相关文章
- 回文数组(Rotate Array (JS))
旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...
- 理解JavaScript中的参数传递 - leetcode189. Rotate Array
1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...
- LeetCode189——Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- Leetcode-189 Rotate Array
#189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- LeetCode: Reverse Words in a String && Rotate Array
Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- LeetCode Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
随机推荐
- sencha gridpanel改变单元格颜色
标题列包含 审核通过则绿色,包含拒绝为红色: { xtype: 'gridcolumn', renderer: function(value, metaData, record, rowIndex, ...
- WPF画图简便技巧
下面这个就是:起点在(0,0)开始的正方形.(用绿色填充).此方法比一条一条的写方便多了! <Path Fill="LawnGreen" Data="M 0 0 L ...
- gene框架文档 - 路由类 gene_router
路由类 Gene\Router 介绍 Gene\Router 是gene框架的核心类之一,本框架区别于其他常见框架的最大地方就是独特.强大.简单的路由定义等.路由强大灵活,支持回调.类方法:支持res ...
- php5.6-Apache2.4-mysql5.6环境配置(win7_64位)
----------------------------------------------------- ★软件工具:(下载时注意下载相应版本,不同版本安装细节可能会有差异!!) 1>http ...
- Hibernate+maven+mysql
最近在研究hibernate,想建立一个简单的Hibernate+maven+mysql工程,网上找了一大堆的示例,要么看不懂结构,要么就是缺少必要文件.总之都没有成功,结果无意在一个外文网上找了一个 ...
- mysql启用慢日志查询
查询超时时间:long_query_time 启动慢查日志:log_slow_queries={YES|NO} 启动慢查日志 : slow_query_log ...
- 通过OpenSSL来生成二进制格式证书文件(pfx和cer)
1.生成RSA字符串私钥 genrsa -out private-rsa.key 2.由1中私钥导出*.cer二进制公钥文件 req -new -x509 -key private-rsa.key - ...
- [Tool] Windows 8.1安装SQL Server
[Tool] Windows 8.1安装SQL Server 问题情景 因为工作的关系,需要在Windows 8.1.64Bit设备上安装SQL Server 2012.本来以为是个只要按下一步就可以 ...
- javascript --- Ajax基础
神马是Ajax? Ajax即‘Asynchronous javascript and XML’(异步javascript和XML),也就是所谓的无刷新页面读取技术. http请求 首先要了解http请 ...
- location对象及history对象
history对象 location 是最有用的BOM对象之一,它提供了与当前窗口中加载的文档有关的信息,还提供了一些导航功能.事实上,location 对象是很特别的一个对象,因为它既是windo ...