首页 > 其他 > 详细

LeetCode - Refresh - Rotate Array

时间:2015-03-23 11:00:41      阅读:251      评论:0      收藏:0      [点我收藏+]

Note:

1. K might be very very large. So remember to module it with n.

2. two range reverse (0, k-1), (k, n-1).

 

 1 class Solution {
 2 public:
 3     void reverse(int nums[], int start, int end) {
 4         while (start < end) {
 5             [](int &a, int &b) {int t = a; a = b; b = t;}(nums[start++], nums[end--]);
 6         }
 7     }
 8     void rotate(int nums[], int n, int k) {
 9         k %= n;
10         reverse(nums, 0, n-1);
11         reverse(nums, 0, k-1);
12         reverse(nums, k, n-1);
13     }
14 };

 

LeetCode - Refresh - Rotate Array

原文:http://www.cnblogs.com/shuashuashua/p/4359109.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!