LeetCode & Q189-Rotate Array-Easy
Array
Description:
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].
刷题还是习惯早上刷,现在脑子不灵光了,哎...
这题我想的很复杂,可以说和算法不沾边,看了Discuss照着写了一遍。
public class Solution {
public void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length-1);
reverse(nums, 0, k-1);
reverse(nums, k, nums.length-1);
}
public void reverse(int[]nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
}
LeetCode & Q189-Rotate Array-Easy的更多相关文章
- 【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 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 ...
- 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(67)-Rotate Array
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
- 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 665. Non-decreasing Array(Easy)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
随机推荐
- Eclipse常用不常用快捷键
逼格高且常用的7个快捷键:Ctrl+O:列出该类下的所有方法 Ctrl+E:列出打开的所有类 Shift+Enter:在当前行下一行创建空白行(Ctrl+Shift+Enter:在当前行上一行 ...
- Java求最大公约数和最小公倍数
最大公约数(Greatest Common Divisor(GCD)) 基本概念 最大公因数,也称最大公约数.最大公因子,指两个或多个整数共有约数中最大的一个.a,b的最大公约数记为(a,b),同样的 ...
- 开发中使用mongoTemplate进行Aggregation聚合查询
笔记:使用mongo聚合查询(一开始根本没接触过mongo,一点一点慢慢的查资料完成了工作需求) 需求:在订单表中,根据buyerNick分组,统计每个buyerNick的电话.地址.支付总金额以及总 ...
- Objective-C 中的 BOOL
之前开发了一个针对单个数据模型,自动建表.增删改查等操作的 ORM 库,后边在 iPhone 5c 上使用时,出现了 crash 的情况. 该项目在 Github 开源,项目地址为SXJDatabas ...
- ListIterator
1,ListIterator与Iterator Iterator的功能:next(),hasNext(),remove() 功能太少,因此出现了ListIterator,他的功能要比Iterator多 ...
- Day3--------------目录文件的浏览、管理及维护
1.pwd命令功能 2.cd命令功能 cd.. 返回上一级 cd~ cd- 返回上一次的目录 cd 3.ls命令功能 ls -a ls -all ls -l 4.cp命令功能 cp -i 覆 ...
- ASCII十进制转字符串的方法
/// <summary> /// ASCII转字符串 /// </summary> /// <param name="asciiCode">A ...
- java中有关流操作的类和接口
一.java操作l流有关的类和接口 1.File 文件类 2.RandomAccessFile 随机存储文件类 3.InputStream 字节输入流 4.OutputStream 字节输出流 5.R ...
- linux设置代理修改接口数据
其实很简单,希望看到的人可以一次搞定,所以我把所有步骤写一块儿了. 1.首先在自己能上网的机器上安装fiddler,程序自己百度搜就可以,百度软件中心的程序就行. 2.安装fiddler后,管理员权限 ...
- Java创建线程的三种方式
一.继承Thread类创建线程类 (1)定义Thread类的子类,并重写该类的run方法,该run方法的方法体就代表了线程要完成的任务.因此把run()方法称为执行体. (2)创建Thread子类的实 ...