国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

189. Rotate Array

2019-11-08 02:37:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題目

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.

[show hint]

Related problem: Reverse Words in a String II

Credits: Special thanks to @Freezen for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.


思路

思路:《程序員編程藝術(shù)第一~二十七章集錦與總結(jié)(教你如何編程)(by_July)定稿版》原題 計(jì)算公式:(X^tY^t)^t = YX;


代碼

class Solution {public: void rotate(vector<int>& nums, int k) { int length = nums.size(); k = k%length; reversalNum(nums,0,length-k-1); reversalNum(nums,length-k,length-1); reversalNum(nums,0,length-1); } void reversalNum(vector<int>& nums,int begin,int end) { int temp; while(begin < end) { temp = nums[begin]; nums[begin] = nums[end]; nums[end] = temp; begin++; end--; } }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 台南市| 屏东县| 闽清县| 金堂县| 忻城县| 阜宁县| 江北区| 盐山县| 万荣县| 青冈县| 青岛市| 庆元县| 通化市| 乌拉特中旗| 霍城县| 剑阁县| 昌都县| 陵川县| 洛宁县| 祁门县| 双柏县| 任丘市| 古田县| 台山市| 陇川县| 买车| 临猗县| 阿克| 伊吾县| 洛扎县| 玉山县| 彰武县| 土默特右旗| 涪陵区| 通许县| 盐亭县| 吴旗县| 渝中区| 万载县| 北安市| 德清县|