leetcode 189: Rotate Array
来源:互联网 发布:用excel给数据取对数 编辑:程序博客网 时间:2024/05/21 22:58
解题思路:
对于字符串s,设s由字符串s1和s2两部分组成。先把s1逆序,再把s2逆序, 最后把s逆序,数组s就向右旋转了s2.size()个元素
问题描述:
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.
AC解,C++代码,菜鸟一个,请大家多多指正
class Solution {public: void reverse(vector<int>& nums, int left, int right) { while (left < right) { int tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; left++; right--; } } void rotate(vector<int>& nums, int k) { int nums_length = nums.size(); if (nums_length < 2) { return ; } k = k % nums_length; if (k < 1) { return ; } reverse(nums, 0, nums_length - 1 - k); reverse(nums, nums_length - k, nums_length - 1); reverse(nums, 0, nums_length - 1); return ; }};
0 0
- [LeetCode][189][Rotate Array]
- Leetcode 189: Rotate Array
- leetCode #189 Rotate Array
- LeetCode #189 Rotate Array
- LeetCode(189) Rotate Array
- [LeetCode 189]Rotate Array
- LeetCode #189 Rotate Array
- LeetCode 189 Rotate Array
- leetcode-189 Rotate Array
- leetcode 189 Rotate Array
- leetcode-189 Rotate Array
- Leetcode[189]-Rotate Array
- leetcode[189]:Rotate Array
- leetcode 189: Rotate Array
- [Leetcode]#189 Rotate Array
- leetcode 189 Rotate Array
- 【LeetCode】189Rotate Array
- LeetCode 189 Rotate Array
- Win7(Windows 7)下用VS2013(Visual Studio 2013)编译crtmpserver
- 实现一些字符串操作标准库函数、解决一些字符串问题
- CodeForces 732B 之 Cormen — The Best Friend Of a Man
- Spring MVC 入门示例讲解
- PHP 简单的for循环
- leetcode 189: Rotate Array
- 异常摘要
- 安卓中获取手机的相关信息的工具类
- JavaScript中arguments[0]()表示的是什么?
- iOS10相册相机闪退bug
- 【PI应用】用Java查询雅虎天气并解析Json数据
- 收藏一个关于数据库存储BLOB类型数据的方法
- selenium_skeleton—— page-objects toolkit
- hibernateFilter 在web.xml中配置