---恢复内容开始---

Description

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

问题描述:给定一个长度为n的数组和一个非负整数k,向右旋转k部。

思路,首先这里k是可以大于数组的长度的。我采用最暴力的解法,这种解法效率很低,提交之后只能到13%-85% 效率不固定

public void Rotate(int[] nums, int k) {
k = k % nums.Length;
int[] temp = new int[k];
if(k== nums.Length)
return;
int length = nums.Length;
for(int i = ; i < k; i++)
temp[i] = nums[length - k + i];
for(int i = length - k -; i >=; i--)
nums[i+k]=nums[i];
for(int i = ; i < k; i++)
nums[i]=temp[i];
}

解法二  采用反转的方式,先把所有的数组反转,然后反转前k个元素,再反转后n-k个元素

public class Solution {
public void Rotate(int[] nums, int k) {
k = k % nums.Length;
Reverse(nums, , nums.Length-);
Reverse(nums, , k-);
Reverse(nums, k, nums.Length-);
}
private void Reverse(int[] arr, int start, int end){
while(start < end){
int temp = arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
}
}

但是 实测发现,后一种方法效率还不如第一种方法。

LeetCode Array Easy 189. Rotate Array的更多相关文章

  1. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

  2. 【leetcode❤python】 189. Rotate Array

    #-*- coding: UTF-8 -*-#由于题目要求不返回任何值,修改原始列表,#因此不能直接将新生成的结果赋值给nums,这样只是将变量指向新的列表,原列表并没有修改.#需要将新生成的结果赋予 ...

  3. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  4. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

  5. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  6. 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  ...

  7. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  8. 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 ...

  9. 【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  ...

随机推荐

  1. SnowFlake --- 分布式id生成算法

    转载自:https://segmentfault.com/a/1190000011282426 概述 SnowFlake算法生成id的结果是一个64bit大小的整数,它的结构如下图: 1位,不用.二进 ...

  2. Java8 新增BASE64加解密API

    什么是Base64编码? Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法 基于64个字符A-Z,a-z,0-9,+,/ ...

  3. web前端架构师学习流程

    JavaScript 开发(高级) 系统知识 1.1ECMAScript标准的发展过程,ES6语言对JavaScript的改进: 1.2ES6中语法层面的新特性(let.const.参数扩展.模块化等 ...

  4. go语言从例子开始之Example25.通道方向

    当使用通道作为函数的参数时,你可以指定这个通道是不是只用来发送或者接收值.这个特性提升了程序的类型安全性. Example: package main import "fmt" / ...

  5. 2019牛客暑期多校训练营(第三场)I Median

    题意:给出n-2的中位数序列b,b[i]代表原序列中(a[i],a[i+1],a[i+2])的中位数,求a. 解法:比赛的时候没做出来,赛后看题解的.解法跟网上各位大佬一样:首先要证明其实原序列a中的 ...

  6. vue 如何读取编译携带的参数

    vue 环境有很多套,我们需要根据不同环境设置不同的一些参数,如何不装任何依赖的情况下获取参数 下面是我制作官网,需要根据开发还是生产环境配置不同CDN,用vue-cli2+webpack,配置是再: ...

  7. 力扣—— Swap Nodes in Pairs(两两交换链表中的节点) python实现

    题目描述: 中文: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4, ...

  8. 配置本机的yum源

    配置本机的yum源 环境:操作系统CentOS6.5 1.挂在安装光盘 [root@CentOS40 ~]# mkdir -p /mnt/cdrom[root@CentOS40 ~]# mount / ...

  9. 16.ThreadGroup线程组

    public class ThreadGroupDemo implements Runnable{ public static void main(String[] args){ //建立一个名为&q ...

  10. Kubernetes 健康检查的两种机制:Liveness 探测和 Readiness 探测

    Kubernetes 健康检查的两种机制:Liveness 探测和 Readiness 探测,并实践了健康检查在 Scale Up 和 Rolling Update 场景中的应用.kubelet使用启 ...