189. Rotate Array(两次反转)
2024-08-26 12:40:04
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.
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;
}
}
最新文章
- ASP.NET MVC随想录——漫谈OWIN
- 快速学习C语言四: 造轮子,ArrayList
- mysql技巧之select count的比较
- ado.net 修改,查询
- WebForm中<;%=%>;与<;%#%>;的区别?
- core java 5~6(OOP &; 高级语言特征)
- The xor-longest Path
- HDU-2176 取(m堆)石子游戏
- My way to Python - Day02
- c++打印环境变量
- spring mvc 下载文件链接
- WC2019 划水记
- Pytorch 入门之Siamese网络
- 思科恶意加密TLS流检测论文记录——由于样本不均衡,其实做得并不好,神马99.9的准确率都是浮云啊,之所以思科使用DNS和http一个重要假设是DGA和HTTP C&;C(正常http会有图片等)。一开始思科使用的逻辑回归,后面17年文章是随机森林。
- Android 控件: Webview 的一些知识点
- Eclipse 4.2 failed to start after TEE is installed
- svn新增文件时自动给文件设置强制只读属性needs-lock
- 分享八:特殊的mysql函数
- javascript中字符串拼接详解
- Java基础知识强化之集合框架笔记77:ConcurrentHashMap之 ConcurrentHashMap的基本操作
热门文章
- hdu 1756:Cupid&#39;s Arrow(计算几何,判断点在多边形内)
- 如何用ChemDraw选择结构
- MFC中给控件添加变量,DoDataExchange中
- Microsoft Word、Excel、PowerPoint转Pdf
- Windows游戏编程大师技巧之三角形填充
- 移动App如何实践单点登录
- 【BZOJ3232】圈地游戏 分数规划+最小割
- C++编译遇到参数错误(cannot convert parameter * from &#39;const char [**]&#39; to &#39;LPCWSTR&#39;)
- 安装TortoiseSVN客户端时遇到的异常
- Sharepoint ECMAScript