leetcode189
public class Solution {
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--;
}
} public void Rotate(int[] nums, int k)
{
k %= nums.Length;
reverse(nums, , nums.Length - );
reverse(nums, , k - );
reverse(nums, k, nums.Length - );
}
}
https://leetcode.com/problems/rotate-array/#/description
补充一个python的实现:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
part1 = nums[n-k:]
part2 = nums[:n-k]
nums.clear()
nums.extend(part1)
nums.extend(part2)
leetcode189的更多相关文章
- 倒转数组 Leetcode189
倒转数组 Leetcode189 记录调整数组顺序而不需要另加内存的一种方法: 题目 189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [ ...
- 理解JavaScript中的参数传递 - leetcode189. Rotate Array
1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...
- LeetCode189——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
#189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 ...
- [Swift]LeetCode189. 旋转数组 | Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- LeetCode189:Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- leetcode189. 旋转数组
方法 4:使用反转算法 这个方法基于这个事实:当我们旋转数组 k 次, k\%nk%n 个尾部元素会被移动到头部,剩下的元素会被向后移动. 在这个方法中,我们首先将所有元素反转.然后反转前 k 个元素 ...
- 面试题(9)之 leetcode-189
题目描述 解法一: /** * @param {number[]} nums * @param {number} k * @return {void} Do not return anything, ...
- 2017-3-6 leetcode 118 169 189
今天什么都没发生 ================================================= leetcode118 https://leetcode.com/problems ...
随机推荐
- js之瀑布流的实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Git详解之八 Git与其他系统
以下内容转载自:http://www.open-open.com/lib/view/open1328070454218.html Git 与其他系统 世界不是完美的.大多数时候,将所有接触到的项目全部 ...
- MySQL 5.7笔记
1. 初始化 重命名my-default.ini为my.ini 添加character_set_server=utf8 运行mysqld --initialize 2. 重置密码 服务器运行: mys ...
- Redis学习总结之二——Redis配置文件(Windows版)
# Redis configuration file example # Note on units: when memory size is needed, it is possible to sp ...
- matlab GUI重新命名
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309789 To change the name you should o ...
- SpringBoot 项目修改html后不需要重新启动(热部署)
基于IDEA配置: 一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <arti ...
- Robotframework第1课--安装RF
大家好,我是孟船长,现从事自动化测试的工作,工作用的工具就是Robotframework,现在把这“几年”的所得分享出来,希望新进入这行的朋友能够少吃点“新人苦”,能够早点入手robot framew ...
- 关于1024:堆栈下溢的错误(1024. Stack Underflow Occurred)
http://blog.163.com/sylar_lin/blog/static/192332093201111242412487/ 今天碰到个很奇怪的问题,注释掉下面的trace,realse版本 ...
- 2018-10-09 可用的前端 CDN
2018-10-09 可用的前端 CDN 360 前端静态资源库 https://cdn.baomitu.com/ 新浪的前端 CDN http://lib.sinaapp.com/ 又拍云的前端库慢 ...
- Linux之 iostat 解读磁盘io
1.iostat[oracle@orastb log]$ iostatLinux 3.10.0-327.el7.x86_64 (orastb.bonc.com.cn) 09/07/2017 _x86_ ...