LeetCode 旋转数组算法题解 All In One
LeetCode 旋转数组算法题解 All In One
js / ts 实现螺旋数组
旋转原理 图解
// 交换
function swapArray(i, j, nums) {
while(i < j) {
[
nums[i],
nums[j]
] = [
nums[j],
nums[i]
];
// console.log(`nums[${i}] =`, nums);
i++;
j--;
}
}
// 旋转
function rotateArray(k, nums) {
const len = nums.length;
// 1. 交换 (len - 1) / 2 次
swapArray(0, len - 1, nums);
// 2. 交换 (k - 1) / 2 次
swapArray(0, k - 1, nums);
// 3. 交换 Math.ceil((len - 1 - k) / 2) 次
swapArray(k, len - 1, nums);
}
189. Rotate Array
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-08-10
* @modified
*
* @description 89. Rotate Array
* @description 189. 旋转数组
* @difficulty Medium
* @ime_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://leetcode.com/problems/rotate-array/
* @link https://leetcode-cn.com/problems/rotate-array/
* @solutions
*
* @best_solutions
*
*/
export {};
const log = console.log;
/**
Do not return anything, modify nums in-place instead.
*/
// solution 2: in-place 就地交换算法 ✅
function reverse(i: number, j: number, nums: number[]): void {
while(i < j){
[
nums[i],
nums[j]
] = [
nums[j],
nums[i]
];
// 两两互换
i++;
j--;
}
};
function rotate(nums: number[], k: number): void {
k %= nums.length;
// 如果 k 大于 nums.length 则完成一个循环,这意味着它将保持不变,我们必须进行剩余移位
// reverse all
reverse(0, nums.length-1, nums);
// reverse first part
reverse(0, k - 1, nums)
// reverse second part
reverse(k, nums.length-1, nums);
};
// type alias
type ObjectType = {
// input: any[];
input: [number[], number];
result: number[];
desc: string;
}
// 1. TypeScript & define Object Array Interface methods ✅ extends Array<ObjectType>
// interface TestCaseInterface extends Array<ObjectType> {
// //
// }
// 2. TypeScript & define Object Array Interface methods ✅ [index: number]: ObjectType;
interface TestCaseInterface extends Array<any> {
[index: number]: ObjectType;
}
// 测试用例 test cases
const testCases: TestCaseInterface = [
{
input: [
[1,2,3,4,5,6,7],
3,
],
result: [5,6,7,1,2,3,4],
desc: 'value equal to [5,6,7,1,2,3,4]',
},
{
input: [
[-1,-100,3,99],
2,
],
result: [3,99,-1,-100],
desc: 'value equal to [3,99,-1,-100]',
},
];
for (const [i, testCase] of testCases.entries()) {
const [first, second] = testCase.input;
rotate(first, second);
log(`test case i result: \n`, first.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, testCase.result);
}
// solution 1:暴力破解:❌ Time Limit Exceeded
// function rotate(nums: number[], k: number): void {
// if(k === 0) {
// // return;
// } else {
// const len = nums.length;
// while(k > 0) {
// // deep clone
// let temp = [];
// for(let i= 0; i< len; i++) {
// // 破解对象引用 bug, 值引用
// temp[i] = nums[i];
// }
// let end = temp[len - 1];
// // 循环队列
// for(let i = 1; i< len; i++) {
// // 交换swap
// nums[i] = temp[i - 1];
// }
// nums[0] = end;
// // console.log(`nums =`, nums);
// k -= 1;
// }
// }
// // no return, in-place 就地交换
// };
https://leetcode.com/problems/rotate-array/
https://leetcode.cn/problems/rotate-array/
leetcode 题解 / LeetCode Solutions
https://www.youtube.com/results?search_query=+Leetcode+189
https://www.youtube.com/playlist?list=PLamwFu9yMruCBtS2tHUD77oI_Wsce-syE
https://www.youtube.com/channel/UCftIXZeipv4MTVwmfFphtYw/videos
类似问题
in-place 就地交换算法
LeetCode 796. Rotate String
https://leetcode.com/problems/rotate-string/
LeetCode 33. Search in Rotated Sorted Array
https://leetcode.com/problems/search-in-rotated-sorted-array/
https://www.youtube.com/watch?v=k_LJpNTEy5o
https://www.youtube.com/watch?v=gsOimOfyv7g
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16578483.html
未经授权禁止转载,违者必究!