LeetCode(67)-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].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
思路:
- 题意:要求对给定长度的整形数组n进行平移,给定k,就平移3个位置(循环)
- 要求是在o(1)的空间,不能考虑数组的复制了,根据算法,这个平移转化为逆序。reverse(nums,0,n-k-1),reverse(nums,n-k,n-1),reverse(nums,0,n-1)等价,写一个reverse的函数。
代码:
public class Solution {
public void reverse(int[] nums,int start,int end){
while(start < end){
int tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
start++;
end--;
}
}
public void rotate(int[] nums, int k) {
if(nums.length == 0){
return;
}
int n = nums.length;
k = k%n;
reverse(nums,0,n-k-1);
reverse(nums,n-k,n-1);
reverse(nums,0,n-1);
}
}
LeetCode(67)-Rotate Array的更多相关文章
- 【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 189. 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 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- Java for LeetCode 189 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:Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- Java [Leetcode 189]Rotate Array
题目描述: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...
- C#解leetcode 189. 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 stl
题意:将数组旋转k次,如将数组[1,2,3,4,5]旋转1次得到[2,3,4,5,1],将数组[1,2,3,4,5]旋转2次得到[3,4,5,1,2]..... 本质是将数组分成两部分a1,a2,.. ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
随机推荐
- Dynamics CRM2013 流程拷贝
在CRM中工作流是一个非常不错的功能,在实际业务场景中能满足各种业务需求.在我们设置一个工作流的时候,同一个实体一个逻辑功能可能需要多个工作流来实现,而多个工作流的不同之处可能只是启动时间或者是步骤中 ...
- 【一天一道LeetCode】#263. Ugly Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- Latex 表格内文字过长自动换行
法一: [plain] view plaincopy \begin{tabular}{m{5cm}} 法二: [plain] view plaincopy \begin{tabular}{p{0.9\ ...
- CentOS上PHP完全卸载
想把PHP卸载干净,直接用yum的remove命令是不行的,需要查看有多少rpm包,然后按照依赖顺序逐一卸载. 1.首先查看机器上安装的所有php相关的rpm包 [root@localhost ngi ...
- WIP 投料报 Invalid Serial Number
1.接口表数据检查无误 2.同样数据界面能正常完成 界面做trace SQL ID: b2mw8gjyv7guh Plan Hash: 2015965341 DELETE FROM MTL_SERIA ...
- [C++学习历程]基础部分 C++中的类型和声明
前面搭起了C++的VS环境,可以在VS中编写C++代码了,也运行了最简单的一个程序Helloworld.那么我们该怎么才能写出功能强大的程序,怎样才能随心所欲的应用呢,那就需要重新回头来,从C++基础 ...
- UNIX环境高级编程——sigqueue、sigsuspend函数
一.sigqueue函数 功能:新的发送信号系统调用,主要是针对实时信号提出的支持信号带有参数,与函数sigaction()配合使用. int sigqueue(pid_t pid, int sig, ...
- Using mysqldump for Backups(备份还原数据库实例及参数详细说明)
官网mysqldump命令如下: Using mysqldump for Backups https://dev.mysql.com/doc/refman/5.7/en/using-mysqldump ...
- C++ Primer 有感(new和delete表达式)
定义变量时,必须指定其数据类型和名字.而动态创建对象时,只需指定其数据类型,而不必为该对象命名.取而代之的是,new表达式返回指向性创建的指针. 1.动态创建对象的默认初始化 对于类类型的对象,用该类 ...
- oozie note
http://blog.sina.com.cn/s/blog_62a9902f01011ccd.html 实例:http://www.infoq.com/cn/articles/oozieexampl ...