LintCode 39 恢复旋转排序数组
来源:互联网 发布:淘客营销软件 编辑:程序博客网 时间:2024/05/14 11:33
题目:searchMatrix
要求:
给定一个旋转排序数组,在原地恢复其排序。
什么是旋转数组?
比如,原始数组为[1,2,3,4], 则其旋转数组可以是[1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]
样例:
[4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]
算法要求:
使用O(1)的额外空间和O(n)时间复杂度
解题思路:
循环将头部放到后面,知道遇到比下一个值比当前值小的时候停止。
算法如下:
void recoverRotatedSortedArray(vector<int> &nums) { // write your code here int size = nums.size(); for (int i = 1; i < size; i++) { int temp = nums[0]; if (temp > nums[1]) { break; } nums.erase(nums.begin()); nums.push_back(temp); } int temp = nums[0]; nums.erase(nums.begin()); nums.push_back(temp); }
0 0
- LintCode 39 恢复旋转排序数组
- LintCode之39 恢复旋转排序数组
- Lintcode:恢复旋转排序数组
- Lintcode 39. 恢复旋转排序数组
- [LintCode]39.恢复旋转排序数组
- Lintcode 恢复旋转数组
- 【LintCode】Recover Rotated Sorted Array 恢复旋转排序数组
- 恢复旋转排序数组
- 恢复旋转排序数组
- 恢复旋转排序数组
- 恢复旋转排序数组
- 恢复旋转排序数组
- 恢复旋转排序数组
- LintCode-搜索旋转排序数组
- LintCode : 搜索旋转排序数组
- LintCode:搜索旋转排序数组
- LintCode:搜索旋转排序数组
- *[Lintcode]搜索旋转排序数组
- QT::QColor
- Encodeing(三)
- 使用spring遇到"BeanNotOfRequiredTypeException......but was actually of type [com.sun.proxy.$Proxy18]"异常
- When You And I Were Young, Seven7
- 合并两个排序的链表(Java实现)
- LintCode 39 恢复旋转排序数组
- 关于java多线程
- 桥接模式
- leetcode 39 Combination Sum (回溯法)
- Series 和 DataFrame 中的sort_index 方法
- Deep Learning学习笔记(二)——BP反向传播算法及其在CNN中的计算
- 凡是过去,皆为序章,
- 微信小程序组件
- node.js搭建代理服务器实现跨域