-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 77 题:旋转数组算法题 #126
Comments
var arr = [-1, -100, 3, 99];
var k = 5;
for (let i = 0; i < k; i++) {
let val = arr.pop();
arr.unshift(val);
}
console.log(arr); |
var rotate = function(nums, k) {
for(var i = 0;i<k;i++){
nums.unshift(nums.pop())
}
return nums;
} |
/* 第 77 题:旋转数组算法题
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
输入: [1, 2, 3, 4, 5, 6, 7] 和 k = 3
输出: [5, 6, 7, 1, 2, 3, 4]
解释:
向右旋转 1 步: [7, 1, 2, 3, 4, 5, 6]
向右旋转 2 步: [6, 7, 1, 2, 3, 4, 5]
向右旋转 3 步: [5, 6, 7, 1, 2, 3, 4] */
const rotateAry = (ary, k) => {
let len = ary.length
let rotateNum = k % len
if (rotateNum === 0) {
return ary
}
let tmpAry = new Array(len)
ary.forEach((el, index) => {
if ((index + rotateNum) >= len) {
tmpAry[index + rotateNum - len] = el
} else {
tmpAry[index + rotateNum] = el
}
})
return tmpAry
}
console.log(rotateAry([1, 2, 3, 4, 5, 6, 7], 3)) 这么一对比我的写法好low啊= = |
问题转化为:数组的末尾k个元素移动到数组前面
test: |
找到一个合适的位置剪掉,unshift到原来的数组, 题目的意思应该是要改变原来的数组,所以直接对原数组开刀。 Array.prototype.rotate = function rotateArr(k = 0) {
const len = this.length;
if(!len) {
return this;
}
k = k % len;
if(k <= 0) {
return this;
}
const cut = this.splice(len - k);
return this.unshift(...cut);
}
let a = [1, 2, 3, 4, 5, 6];
a.rotate(3); |
拷贝下数组, 防止修改入参 function rotateArr(arr, k) {
const _tmp = JSON.parse(JSON.stringify(arr));
return [..._tmp.splice(k + 1), ..._tmp];
} |
function rotateStep(arr: any[]) {
arr.unshift(arr.pop());
}
function rotateTimes(arr: any[], k: number) {
for(let i=0; i<k; i++) {
rotateStep(arr);
}
}
const arr = [1, 2, 3, 4, 5, 6, 7, 8];
rotateTimes(arr, 1);
console.log(`arr ${JSON.stringify(arr)}`); |
var a = [1,2,3,4,5,6,7]; |
function f(arr,k){arr.unshift(...arr.splice(-k)); console.log(arr) } |
function rotate(arr, k) {
if (!Array.isArray(arr) || !arr.length) {
console.error('参数 arr 必须为长度大于 0 的数组');
return [];
}
if (!Number.isInteger(k) || k <= 0) {
console.error('参数 k 必须为非负整数');
return [];
}
const _tmp = JSON.parse(JSON.stringify(arr));
return [..._tmp.splice(-k % _tmp.length), ..._tmp];
}
// test
const arr = [1, 2, 3, 4, 5, 6, 7];
console.log(rotate(arr, 0)); // [1, 2, 3, 4, 5, 6, 7]
console.log(arr); // [1, 2, 3, 4, 5, 6, 7], 纯函数,不改变原数组
console.log(rotate(arr, 'aaa')); // []
console.log(rotate(arr, 2)); // [ 6, 7, 1, 2, 3, 4, 5 ] |
|
let rotArr = (arr,k) => { |
leetcode 第77题
|
let arr = [1, 2, 3, 4, 5, 6, 7];
let rotate = (arr,k)=>[...arr.slice(-k),...arr.slice(0,-k)] |
这操作。。。 |
|
function rotateArray(array, step) {
// 旋转长度超过数组长度,只需要旋转余数就行了
step = step % array.length;
// splice 方法同时会将原数组截断
var rotateArray = array.splice(array.length - step);
// 重新拼接
return rotateArray.concat(array);
}
rotateArray([1, 2, 3, 4, 5], 6); |
编程式实现,见笑了 function rotateArr(arr, k) {
let len = arr.length;
let result = [];
k = k > len ? k % len : k;
for (let i = len - 1; i >= 0; i--) {
if (len - i <= k) {
result.unshift(arr[i]);
} else {
result[i + k] = arr[i];
}
}
return result;
} |
当 k = 0 或 k = arr.length 时,返回结果有误: |
function rotateArr(arr,k){ |
/**
* solution1
* 最短路径逐个转移
*/
var rotate = function (nums, k) {
const len = nums.length;
let c = k % len;
if (c > len / 2) {
c = len - c;
while (c > 0) {
nums.push(nums.shift());
c--;
}
}
else {
while (c > 0) {
nums.unshift(nums.pop());
c--;
}
}
};
/**
* solution2
* 妙用API一次性转移
*/
var rotate = function (nums, k) {
nums.unshift(...nums.splice(- (k % nums.length)));
};
/**
* solution3
* 三次翻转
*/
var rotate = function (nums, k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
};
function reverse(nums, start, end) {
while (start < end) {
let temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
} |
@zpzxgcr 仔细看题目的example老哥 输入: [1, 2, 3, 4, 5, 6, 7] 和 k = 3
输出: [5, 6, 7, 1, 2, 3, 4]
解释:
向右旋转 1 步: [7, 1, 2, 3, 4, 5, 6]
向右旋转 2 步: [6, 7, 1, 2, 3, 4, 5]
向右旋转 3 步: [5, 6, 7, 1, 2, 3, 4] |
@zxcweb k = 10000000000 |
@ChasLui const _tmp = [...arr]; |
@kungithub f([1,2],13);
> [1, 2] |
function rotateArr(arr, k) {
return [...arr.splice(-(k % arr.length)), ...arr];
} |
@kingstone3 yes |
|
const arr = [1, 2, 3, 4, 5, 6, 7] function change(arr, k) { |
|
|
let a =[1,2,3,4,5,6,7],k=3; |
|
|
|
function rotateArr(arr, offset) {
for (let i = 0; i < offset; i++) {
arr.unshift(arr.pop());
}
return arr;
}
function rotateArr2(arr, offset) {
return [...arr.slice(arr.length - offset), ...arr.slice(0, arr.length - offset)]
}
console.log(rotateArr([1, 2, 3, 4, 5, 6, 7], 3));
console.log(rotateArr2([1, 2, 3, 4, 5, 6, 7], 4)); |
const splitIndex=arr.length-step;
return arr.slice(splitIndex).concat(arr.slice(0,splitIndex))
}
console.log(rota( [1, 2, 3, 4, 5, 6, 7],3)) |
主要就是一个拼接把。。。 |
function reversal(arr,k){ |
function re(arr, k) {
} |
function rotateArray(arr = [], key = 1) {
const copy = [...arr]
const len = arr.length
const j = key % len
if(j === 0) return arr
if (key <= len) {
for (let i = 0; i < len; i++) {
if (i + key < len) {
arr[i + key] = copy[i]
} else {
arr[i + key - len] = copy[i]
}
}
}else {
rotateArray(arr, j)
}
return arr
} 来此一游。 |
|
const rotate = (arr, = [] step = 0) => arr.splice(-step).concat(arr)
console.log(rotate([1,2,3,4,5,6,7], 4))
// result: [4, 5, 6, 7, 1, 2, 3] |
//nm,我好菜啊,写了这么多,,,
function rotateArr(arr, len) {
if (len < 0) return '参数不能为负数';
let newArr = [...arr],
temp = [];
while (len > 0) {
len--;
for (let i = 0; i < arr.length; i++) {
if (i == arr.length-1) break;
temp.push(newArr[i + 1]);
newArr[i + 1] = i == 0 ? newArr[i] : temp.shift();
}
newArr[0] = temp.pop();
}
return newArr;
} |
使用一个快慢指针,找到需要旋转得部分,然后进行拼接。 |
function roteArray(arr = [], number) {
return arr.slice(-number).concat(arr.slice(0, - number))
}
function roteArray2(arr = [], number) {
return arr.splice(-number).concat(arr)
}
console.log(roteArray([1, 2, 3, 4, 5, 6, 7], 2));
console.log(roteArray2([1, 2, 3, 4, 5, 6, 7], 2)); |
有些拉垮了我
|
let k = 3
let ary = [1, 2, 3, 4, 5, 6, 7]
function rightMove(ary,k) {
const len = ary.length
if(len) {
const res = []
for(let i = 0;i < len;i++) {
if(i + k > len -1) {
res[(i + k) % len] = ary[i]
} else {
res[i + k] = ary[i]
}
}
return res
}
}
console.log(rightMove(ary,k)); |
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let rotateArr = (arr = Array, k) => [
...arr.slice(k % arr.length),
...arr.slice(0, k),
];
console.log(rotateArr(arr, 3)); |
|
|
|
const rotateList = (itemList, offset) => {
const len = itemList.length;
const realOffset = offset % len;
return [...itemList.slice(len-realOffset), ...itemList.slice(0, len-realOffset)];
}
const res = rotateList([1, 2, 3, 4, 5, 6, 7], 3);
console.log(res)
const res1 = rotateList([-1, -100, 3, 99], 2);
console.log(res1); |
1 similar comment
const rotateList = (itemList, offset) => {
const len = itemList.length;
const realOffset = offset % len;
return [...itemList.slice(len-realOffset), ...itemList.slice(0, len-realOffset)];
}
const res = rotateList([1, 2, 3, 4, 5, 6, 7], 3);
console.log(res)
const res1 = rotateList([-1, -100, 3, 99], 2);
console.log(res1); |
|
/* 第 77 题:旋转数组算法题 |
因为步数有可能大于数组长度,所以要先取余
The text was updated successfully, but these errors were encountered: