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

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. 领域驱动设计(DDD:Domain-Driven Design) 转摘自:http://www.jdon.com/ddd.html

    Eric Evans的“Domain-Driven Design领域驱动设计”简称DDD,Evans DDD是一套综合软件系统分析和设计的面向对象建模方法,本站Jdon.com是国内公开最早讨论DDD ...

  2. RecyclerView跳转到指定位置的三种方式

    自从android5.0推出RecyclerView以后,RecyclerView越来越受广大程序员的热爱了!大家都知道RecyclerView的出现目的是为了替代listview和ScrollVie ...

  3. Laravel 事务中使用悲观锁

    laravel 提供了方便快捷的数据库事务使用方式,在使用中遇到过几个容易混淆和被误导的地方,这里做个记录,希望哪里写的不对的地方各位大神指点一下 laravel 事务分为手动方式和自动方式. 但如果 ...

  4. java解决高并发问题

    对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了.而并发问题是绝大部分的程序员头疼的问题, 但话又说回来了,既然逃避不掉,那我们就坦然面对吧~今天就让我们一起来研 ...

  5. 2PC和3PC

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484077.html 2PC 是计算机网络尤其是在数据库领域内,为了使基于分布式系统架构下的所有节点 ...

  6. linux基础(六)

    今天我们来看一下Samba服务和nginx服务. Samba服务 1.samba的功能 samba是一个网络服务器,用于Linux和Windows之间共享文件. 2.samba服务的启动.停止.重启  ...

  7. Comet OJ 三元组 推导+两个指针+分类讨论

    题目:https://www.cometoj.com/contest/59/problem/F?problem_id=2681 题意:给你n个三元组 ai,bi,ci,如果某一对三元组满足    2* ...

  8. vue2 — vuex状态管理

    一.为什么使用vuex : https://www.cnblogs.com/goloving/p/9080005.html vuex的功能 和 localstorage 的作用是一样,把数据在一个所有 ...

  9. MySQL忘记密码无法登录的处理办法

    MySQL安装在CentOS服务器上. 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库.因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护 ...

  10. CSS 设置鼠标显示形状

    CSS 设置鼠标显示形状 <style type="text/css"><!-- span {display:block;line-height:30px;mar ...