项目github地址:bitcarmanlee easy-algorithm-interview-and-practice
欢迎大家star,留言,一起学习进步

Problem

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]. How many different ways do you know to solve this problem?

这道题是旋转数组。大致思路如下:
1.复制数组,最简单的方式。
2.采用类似冒泡排序的方式,每次将最右边一个移到最前面。
3.采用旋转逆序的方式。

talk is cheap,show me the code!

public class RotateArray {public static void printArray(int[] array) {for(int i=0; i<array.length; i++) {System.out.print(array[i] + " ");}}//方法一:复制数组,时间复杂度与空间复杂度均为O(n)public static void rotate(int[] nums, int k) {if(k > nums.length) {k%= nums.length;}int[] result = new int[nums.length];for(int i=0; i<k; i++) {result[i] = nums[nums.length-k+i];}int j = 0;for(int i=k; i<nums.length; i++) {result[i] = nums[j];j++;}System.arraycopy(result,0,nums,0,nums.length);}//方法二:类似于冒泡排序,时间复杂度O(nk),空间复杂度O(1)public static void bubble_rotate(int[] nums, int k) {if(nums == null || k < 0) {throw new IllegalArgumentException("Illegal argument!");}for(int i=0; i<k; i++) {for(int j=nums.length-1; j>0; j--) {int tmp = nums[j];nums[j] = nums[j-1];nums[j-1] = tmp;}}}//方法三:逆序的方式,时间复杂度O(n),空间复杂度O(1)public static void reverse_rotate(int[] nums, int k) {if(nums == null || k < 0) {throw new IllegalArgumentException("Illegal argument!");}reverse_array(nums,0,k-1);reverse_array(nums,k,nums.length-1);reverse_array(nums,0,nums.length-1);}public static void reverse_array(int[] array, int left, int right) {if (array == null || array.length == 0 || left < 0 || right < 0) {throw new IllegalArgumentException("Illegal argument!");}while(left < right) {int tmp = array[left];array[left] = array[right];array[right] = tmp;left++;right--;}}public static void main(String[] args) {int[] nums = {1,2,3,4,5,6,7};reverse_rotate(nums,3);printArray(nums);}
}

Rotate Array相关推荐

  1. leetcode-189. Rotate Array

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

  2. [勇者闯LeetCode] 189. Rotate Array

    [勇者闯LeetCode] 189. Rotate Array Description Rotate an array of n elements to the right by k steps. F ...

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

  4. 【LeetCode从零单排】No189 .Rotate Array

    题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  5. Leet Code OJ 189. Rotate Array [Difficulty: Easy]

    题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...

  6. HappyLeetcode50: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之Rotate Array

    1.题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the a ...

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

  9. C#LeetCode刷题之#189-旋转数组(Rotate Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3700 访问. 给定一个数组,将数组中的元素向右移动 k 个位置, ...

  10. leetcode python3 简单题189. Rotate Array

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百八十九题 (1)题目 英文: Given an array, rotate t ...

最新文章

  1. C语言第二次博客作业---分支结构
  2. 计算机书籍-Apress机器学习和预测分析
  3. PyTorch-运算加速
  4. Oracle 20c 新特性:缺省的只读 Oracle HOME 支持
  5. 关于IT测试中的一些问题。
  6. python 例子 银行_Python3 适合初学者学习的银行账户登录系统实例
  7. 湖工微型计算机及原理题目,2017年湖北工业大学电气与电子工程学院942微机原理与应用考研导师圈点必考题汇编...
  8. 语言(文化)代码与国家地区对照表,各国手机号正则
  9. 这个与流程管理相结合的低代码平台,你一定不能错过
  10. HTTP 405错误解决方法
  11. 安全事故 没有“高级失误”
  12. html doc,HTML咸蛋超人版.doc
  13. 树莓派各类显示屏体验
  14. 如何在AD中批量创建域用户
  15. GitHub学习总结
  16. JPA Spring Data JPA详解
  17. JavaWeb基础5——HTTP,TomcatServlet
  18. 系统移植-(二)u-boot:启动过程做的工作(汇编阶段、C阶段)
  19. Android开发 实现跑马灯效果滚动字幕
  20. java与seo_java实现seo优化 提高运行效率

热门文章

  1. 阿里云破世界记录,王坚说新登月计划需十年,我看不用!
  2. UML中依赖(Dependency)和关联(Association)之间的区别
  3. AES算法,DES算法,RSA算法JAVA实现
  4. Mysql 基础知识
  5. SD-WAN+物联网:让城市更智慧
  6. Android游戏开发的开源框架
  7. restful接口的设计规范
  8. mysql xa事务简单实现
  9. redis持久化方法
  10. Logstash 初探