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 ...
随机推荐
- cocos2d-x 发布win32 release版本后找不到msvcr110.dll
解决方法: 安装Visual C++ Redistributable for Visual Studio 2012 //下载地址 http://www.microsoft.com/zh-CN/down ...
- BZOJ3675 Apio2014 序列分割 【斜率优化】
Description 小H最近迷上了一个分隔序列的游戏.在这个游戏里,小H需要将一个长度为n的非负整数序列分割成k+1个非空的子序列.为了得到k+1个子序列,小H需要重复k次以下的步骤: 1.小H首 ...
- [BZOJ5073][Lydsy1710月赛]小A的咒语
bzoj description 你有一个\(A\)串和\(B\)串,你需要判断是否可以在\(A\)串中拆出\(x\)个互不相交的子串,使它们按顺序拼在一起可以组成\(B\)串. \(|A|,|B|\ ...
- Tornado之自定义异步非阻塞的服务器和客户端
一.自定义的异步非阻塞的客户端 #!/usr/bin/env python # -*- coding: utf8 -*- # __Author: "Skiler Hao" # da ...
- Tree的两种存储形式
1.xml存储 2.链式结构存储 List<Node> nodes = new List<Node>() { , Name = "中国" }, , Name ...
- ldo的一些参数理解
psrr是衡量ldo抑制外来信号噪声的能力,而内部噪声是由基准电压和误差放大器引入的.通常器件手册说的输出噪声指的就是内部噪声. ldo的暂态响应也是一个重要指标
- apiman 安装&&使用
安装测试基于docker 1. docker image pull docker pull apiman/on-wildfly1 2. 启动 docker run -d -p 8081: ...
- python之面向对象(继承)
类的继承 python之面向对象(继承) 面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制.继承完全可以理解成类之间的类型和子类型关系. 需要注意的地方:继承语法 c ...
- ubuntu12.04LTS安装以及卸载 QT4.8.6和QT creator2.5.2
鉴于,下载QT5.5安装,编译总是有问题,可能是配置不正确. 于是按照论坛的一些资料,就换回QT4版本,具体实施步骤如下: 在qt官网http://download.qt.io/archive/ 下的 ...
- 数据库的备份与恢复(oracle 11g) (转)
一. 内容与步骤 (注意这里许多步骤需要同学们查资料,理解并消化后才能完成) 1.数据库创建 (1) 安装Oralce11g: (2) 创建至少两个以上用户: (3) 每个用户 ...