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 [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;
}
}
189. Rotate Array(两次反转)的更多相关文章
- 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 OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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,.. ...
- 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 (旋转数组)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- 【easy】189. Rotate Array
题目标签:Array 题目给了我们一个数组 和 k. 让我们 旋转数组 k 次. 方法一: 这里有一个很巧妙的方法: 利用数组的length - k 把数组 分为两半: reverse 左边和右边的数 ...
- LeetCode Array Easy 189. Rotate Array
---恢复内容开始--- Description Given an array, rotate the array to the right by k steps, where k is non-ne ...
随机推荐
- Python爬虫(七)
源码: import requests import re from my_mysql import MysqlConnect # 获取详情页链接和电影名称 def get_urls(page): u ...
- THINKPHP5操作数据库代码示例
数据库表结构 #表结构 CREATE TABLE `qrcode_file` ( `id` ) NOT NULL AUTO_INCREMENT, `active` ) ' COMMENT '是否有效' ...
- laravel 调试模式及日志配置
1)调试模式和日志的配置都在 config/app.php 配置文件中 2)打开调试模式 'debug' => env('APP_DEBUG', true) 3)laravel的日志默认已经打开 ...
- 解决项目导入dubbo依赖项目报红叉问题
1.maven+ssm项目导入dubbo依赖 项目报错如下 2.出错原因在于dubbo依赖低版本的spring和低版本netty,准备通过maven的依赖管理将依赖传递过来的低版本的spring和ne ...
- 《C++ Primer Plus》学习笔记——C++程序创建到运行的整个过程
当你编写了一个C++,如何让它运行起来呢?具体的步骤取决于计算机环境和使用的C++编译器,但大体如下: 1.使用文本编辑器编写程序,并将其保存到文件中,这个文件就是程序的源代码. 2.编译源代码.这意 ...
- poj_3415 后缀数组+单调栈
题目大意 定义字符串T的子串T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. 给定两个字符串A和B,定义集合S为S = {(i, j, k) | k≥K, A(i, k) ...
- iPhone 6 Screens Demystified
http://www.paintcodeapp.com/news/iphone-6-screens-demystified
- 1358 棋盘游戏[状压dp]
1358 棋盘游戏 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 这个游戏在一个有10*10 ...
- 【BZOJ1441】Min 拓展裴蜀定理
[BZOJ1441]Min Description 给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小 Input 第一行给出数 ...
- 1044: Access denied for user 'hehe'@'localhost' to database 'imooc'
当我使用 mysql授予用户时, GRANT ALL PRIVILEGES ON *.* TO hehe IDENTIFIED BY 'some' WITH GRANT OPTION; 出现:1044 ...