【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 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.
Related problem: Reverse Words in a String II
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
程式碼
public class Solution {
public void rotate(int[] nums, int k) {
int len=nums.length;
if(k==0 || k==len) return;
if(k>len) k=k-len;
int[] result=new int[len];
int result_index=0;
int j=0;
for(int i=k;i>=1;i--){
result[result_index]=nums[len-i];
result_index++;
}
while(result_index<len){
result[result_index]=nums[j];
result_index++;
j++;
}
for(int m=0;m<len;m++){
nums[m]=result[m];
}
}
}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- Rotate Array@LeetCodeLeetCode
- Mysql從零單排-1MySql
- [LeetCode] Rotate StringLeetCode
- 從零單排學Redis【黃金】Redis
- 從零單排學Redis【白銀】Redis
- 從零單排學Redis【鉑金一】Redis
- 從零單排學Redis【鉑金二】Redis
- SpringBoot從零單排 ------初級入門篇Spring Boot
- [LeetCode] 61. Rotate ListLeetCode
- Leetcode 61. Rotate ListLeetCode
- 【3y】從零單排學Redis【青銅】Redis
- 「從零單排canal 03」 canal原始碼分析大綱原始碼
- 「從零單排canal 05」 server模組原始碼解析Server原始碼
- 「從零單排canal 07」 parser模組原始碼解析原始碼
- 從零單排,使用 Netty 構建 IM 聊天室~Netty
- 「從零單排canal 06」 instance模組原始碼解析原始碼
- Laravel 從零單排系列教程 01 :Homestead 環境搭建Laravel
- LeetCode C++ 1464. Maximum Product of Two Elements in an Array【Array/Sort】簡單LeetCodeC++
- 三分鐘從零單排js靜態檢查JS
- Spring AOP從零單排-織入時期原始碼分析Spring原始碼
- Leetcode Sort ArrayLeetCode
- 圖解:什麼是旋轉陣列(Rotate Array)?圖解陣列
- 「從零單排canal 04」 啟動模組deployer原始碼解析原始碼
- 「從零單排HBase 10」HBase叢集多租戶實踐
- 從零開始單排學設計模式「策略模式」黑鐵 II設計模式
- 從零開始單排學設計模式「UML類圖」定級賽設計模式
- 最新【從零單排】系列流出,教你如何實現字典儲存結構
- 從零開始單排學設計模式「裝飾模式」黑鐵 I設計模式
- 從零開始實現簡單 RPC 框架 3:配置匯流排 URLRPC框架
- LeetCode Kth Largest Element in an ArrayLeetCode
- LeetCode Patching Array All In OneLeetCode
- leetcode 283. 移動零(簡單)LeetCode
- 從零開始單排學設計模式「簡單工廠設計模式」黑鐵 III設計模式
- 從零單排Java 8(3) —— List結合Lambdas對排序的高階用法Java排序
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- LeetCode之Squares of a Sorted Array(Kotlin)LeetCodeKotlin
- LeetCode之Sort Array By Parity(Kotlin)LeetCodeKotlin
- Leetcode 88. Merge Sorted ArrayLeetCode
- [LeetCode] 3152. Special Array IILeetCode