前两天在leetcode上刷题,无意中做了这么个小问题觉得值得反思一下。
题目很简单: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]
.
Note : Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
首先想到最简单的实现方法,循环拿到数组的最后一个元素,然后把所有其他元素后移一位,再把之前的最后一个元素放在第一位,这方法简单粗暴,但是资源浪费严重,两重循环一旦input元素很大,效率实在低下。还是贴上代码:
public class Solution { public void rotate(int[] nums, int k) { int len = nums.length; for(int i=0;i<k;i++){ int temp = nums[len-1]; for(int j=len-1;j>0;j--){ nums[j] = nums[j-1]; } nums[0] = temp; } } }
基本没啥技术含量,感觉就是面试的时候突然感觉智商没了的话,起码写点什么。。。
那么下一步想的是如何减少循环嵌套,只用一遍循环解决这个问题,其实很简单,稍微观察下就能知道所谓的右移无非就是把第 length(数组长度)-k(右移个数) 到最后一个元素拼到第一个元素之前。说白了就是从后往前数第K个元素拉到最前面。这样的话毫无疑问得再动用另一个数组作为缓存。代码如下(仍需优化)
public class Solution { public void rotate(int[] nums, int k) { int len = nums.length; if(len<2||len==k){ return; } if(k>len){ k=k%len; } int[] copy = new int[len]; for(int i=0;i<k;i++){ copy[i] = nums[len-k+i]; } for(int j=0;j<len-k;j++){ copy[k+j] = nums[j]; } for(int m = 0;m<len;m++){ nums[m] = copy[m]; } } }
肯定跑过,而且执行效率在Java里也算是靠前的。
目前还在想怎么把执行效率再提高一点,不过就方法来说,在leetcode有位同学的方法还是挺值得借鉴的,主要是我特别喜欢这种代写的特别清楚明白O(n) time and O(1)space,不过速度还是比我的差一点点~:
public class Solution { public void rotate(int[] nums, int k) { int end = nums.length; k=k%end;// to check for outofbounds when k >= nums.length rotate(nums,0,end-k-1);//reverse one half of the array rotate(nums,end-k,end-1);//reverse other half of the array rotate(nums,0,end-1);//reverse whole array } public void swap(int[] nums,int a,int b){ int temp = nums[a]; nums[a] = nums[b]; nums[b] = temp; } public void rotate(int[] nums,int start,int end){ for(int i = start;i<=(start+end)/2;i++){ swap(nums,i,(start+end)-i); } } }
虽然这是个小问题,但是我觉得时刻多一个问题多几分想法,还是很有必要的
相关推荐
学习Matrix的对图像的处理可分为四类基本变换: Translate平移变换 Rotate 旋转变换 Scale缩放变换 Skew 错切变换 最好的demo
$ npm install --save array-rotate 用法 var rotateArray = require ( 'array-rotate' ) ; var arr = rotateArray . createArray ( 1 , 2 , 3 , 4 , 5 , 6 ) ; var positionToRotate = 2 ; var newArr = ...
qt5工程,实现类似于图片浏览器功能,QGraphicsView(平移/缩放/旋转);参考某大神的做法;
对QCustomPlot中选中的某条线进行选中,然后能通过鼠标的移动让它进行平移;然后,可以根据图中的某个点进行旋转;
项目请参见:https://handsome-man.blog.csdn.net/article/details/116427984 图像平移是指将图像中所有的点都沿着水平或垂直方向移动一定的距离。 项目可直接运行~
Impact JS 数组实用程序插件 此插件使用不可枚举的实用程序方法扩展了本机 Array 对象 安装 作为子模块,从 git 命令行: git submodule add ...Array.rotate(n) 返回数组的副本,顺
本程序是用directx + c#编写的三维操作程序,但原理也是适合opengl的,程序是通过精确的数学计算得到所需要的数值
Matrix就是一个3X3的矩阵,对图片的处理可分为四个基础变换操作,Translate(平移变换)、Rotate(旋转变换)、Scale (缩放变换)、Skew(错切变换),如果大家对Matrix不太了解的话可以看看这篇文章(点击查看),作者对...
此apache mod_log_rotate 是win32下vc9版的apache 日志文件管理工具。 使用: 复制mod_log_rotate.so到 Apache2/modules 确保你的系统安装了 Visual C++ 2008 Redistributable (可从这里下载:) ...
rotate旋转
先说明一下为什么要将数组转换成Image类。我处理的图像是FITS ...下方的这幅图就是通过python下的Image库中的rotate函数实现的 接下来贴上代码。 import Image import numpy as np #生成一个数组,维度为100*100,灰
jquery.rotate.min.js 版本VERSION: 2.3
旋转图像的c 代码,正对bmp格式, 无插值,
根据从 3D 旋转、平移、缩放和 2D 倾斜构建一个 4x4 矩阵。 var compose = require ( 'css-mat4' ) var matrix = compose ( [ ] , { translate : [ 25 , 15 , 25 ] , rotate : [ 0 , Math . PI / 2 , - Math . ...
reverse函数用于翻转指定区间内的元素。在left_rotate函数中,我们先将k取模以保证k小于n,然后分别对前k个数、剩下的n-k个数和整个数组进行翻转即可完成循环左移。
Laravel开发-laravel-logs-rotate 使用压缩旋转文件日志
前端开源库-winston-daily-rotate-fileWinstonDaily Rotate文件,Winston每天记录到一个旋转文件的传输。
给定一个数组,将数组循环右移M位。 思路 rotate即可。注意m %= n。关于rotate函数的使用,详见cppreference。 代码 #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr);...
rot_trans_mol:旋转和平移一个分子。 This package provides multiple methods to rotate or translate (the later is ordinary). It can uniformly as well as randomly rotate a molecule in three-dimensional ...
Rotate image without degrading it