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].

Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

[show hint]

Related problem: Reverse Words in a String II

 class Solution {
public void rotate(int[] nums, int k) {
k%=nums.length;
if(k==0) return; reverce(nums,0,nums.length-1);
reverce(nums,0,k-1);
reverce(nums,k,nums.length-1);
}
private void reverce(int[] a,int i,int j){
for(int k = 0;k <= (j-i)/2;k++){
swap(a,i+k,j-k);
}
}
private void swap(int[] a,int i ,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

最新文章

  1. ASP.NET MVC随想录——漫谈OWIN
  2. 快速学习C语言四: 造轮子,ArrayList
  3. mysql技巧之select count的比较
  4. ado.net 修改,查询
  5. WebForm中&lt;%=%&gt;与&lt;%#%&gt;的区别?
  6. core java 5~6(OOP &amp; 高级语言特征)
  7. The xor-longest Path
  8. HDU-2176 取(m堆)石子游戏
  9. My way to Python - Day02
  10. c++打印环境变量
  11. spring mvc 下载文件链接
  12. WC2019 划水记
  13. Pytorch 入门之Siamese网络
  14. 思科恶意加密TLS流检测论文记录——由于样本不均衡,其实做得并不好,神马99.9的准确率都是浮云啊,之所以思科使用DNS和http一个重要假设是DGA和HTTP C&amp;C(正常http会有图片等)。一开始思科使用的逻辑回归,后面17年文章是随机森林。
  15. Android 控件: Webview 的一些知识点
  16. Eclipse 4.2 failed to start after TEE is installed
  17. svn新增文件时自动给文件设置强制只读属性needs-lock
  18. 分享八:特殊的mysql函数
  19. javascript中字符串拼接详解
  20. Java基础知识强化之集合框架笔记77:ConcurrentHashMap之 ConcurrentHashMap的基本操作

热门文章

  1. hdu 1756:Cupid&#39;s Arrow(计算几何,判断点在多边形内)
  2. 如何用ChemDraw选择结构
  3. MFC中给控件添加变量,DoDataExchange中
  4. Microsoft Word、Excel、PowerPoint转Pdf
  5. Windows游戏编程大师技巧之三角形填充
  6. 移动App如何实践单点登录
  7. 【BZOJ3232】圈地游戏 分数规划+最小割
  8. C++编译遇到参数错误(cannot convert parameter * from &#39;const char [**]&#39; to &#39;LPCWSTR&#39;)
  9. 安装TortoiseSVN客户端时遇到的异常
  10. Sharepoint ECMAScript