字符串,数组旋转问题-leetcode 189 Rotate Array
来源:互联网 发布:年度洗脑网络神曲 编辑:程序博客网 时间:2024/06/05 14:44
原题链接:Rotate Array
分析:这种问题属于反转问题,不管是字符串,还是数组都可以使用类似的方法解决;有很多方法解决这种问题,这里提供一种经典的三反转法,这种方法不管是时间复杂度还是空间复杂度都做到了最低。看代码:
class Solution {public: void reverse(vector<int>& nums,int from,int to){ while(from<to){ swap(nums[from],nums[to]); from++; to--; } } void rotate(vector<int>& nums, int k) { /* 使用反转三步法,先反转移动的部分,再反转不移动的部分,最后全部反转 Time Complexity:O(N) Space Complexity:O(1) */ int sz=nums.size(); int border=k%sz; if(sz<=1 || border==0)return; reverse(nums,0,sz-border-1); reverse(nums,sz-border,sz-1); reverse(nums,0,sz-1); }};
阅读全文
0 0
- 字符串,数组旋转问题-leetcode 189 Rotate Array
- LeetCode 189 Rotate Array(旋转数组)
- LeetCode 189 Rotate Array(旋转数组)
- leetcode:Rotate Array旋转数组
- 【LeetCode】Rotate Array 旋转数组
- Rotate Array 数组旋转 leetcode
- leetcode Rotate Array 旋转数组
- LeetCode Rotate Array(数组的旋转)
- LeetCode 189. Rotate Array(旋转数组)
- leetcode 189. Rotate Array-旋转数组
- leetcode 189. Rotate Array旋转数组
- Rotate Array数组旋转
- Rotate Array 数组的旋转
- [C++]Rotate Array 旋转数组
- Rotate Array 部分旋转数组
- 189. Rotate Array 旋转数组
- 189. Rotate Array 旋转数组
- 【LeetCode-面试算法经典-Java实现】【189-Rotate Array(旋转数组)】
- MySql创建用户并授权
- 安卓USB开发教程 <一> USB Host 与 Accessory
- SVG 形状
- JAVA_判断网络给定地址图片是否存在
- jstl中<c:forEach>的用法
- 字符串,数组旋转问题-leetcode 189 Rotate Array
- SVG 效果--轮廓和填充
- SVG 滤镜
- SVG 绘图
- 完美孤独-歌词
- 银联的跨行清算体系架构分析
- hibernate--面试题
- 通过二维码访问WEB程序
- 自适应设备分辨率