我正在尝试在编码中解决这个问题。我的解决方案通过了所有测试,除了一个带有空数组和旋转 1 作为参数的测试。我不知何故迷失了如何解决这个问题。有人可以把我推向正确的方向吗?除了为空数组检查编写特定的 if
子句之外。我想不出更优雅的解决方案。
function solution(A, K)
{
for (i=0; i<K; i++)
{
A.unshift(A.pop());
}
return A;
}
一种解决方案是在循环conditions
部分检查A.length
。
function solution(A, K)
{
for (i = 0; i < K && A.length; i++)
{
A.unshift(A.pop());
}
return A;
}
console.log(solution([3, 8, 9, 7, 6], 3));
console.log(solution([], 5));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
但是,您可以使用 Array.slice() 执行另一种方法:
function solution(A, K)
{
let i = A.length - (K % (A.length || 1));
return [...A.slice(i), ...A.slice(0, i)];
}
console.log(solution([3, 8, 9, 7, 6], 3));
console.log(solution([], 0));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
试试这个:
let solution = (A, K) => {
for (i=0; i<K; i++){
A.unshift(A.pop());
}
return A[0] != undefined ? A : [];
}
解决此问题的另一种方法是通过递归。在这种情况下,您可以使用 if
语句,但是,这是终止递归调用的基本情况。这里的基本情况是,如果K === 0
意味着如果我们不再需要旋转数组,则返回数组。它还有另一部分,如果!A.length
,我们返回数组。这意味着如果数组为空,则返回当前数组。
function solution(A, K) {
if(K === 0 || !A.length) return A;
return solution([A.pop(), ...A], K-1);
}
console.log(solution([1, 2, 3, 4], 2)); // [3, 4, 1, 2]
console.log(solution([], 10)); // []
如果您愿意,可以将以上内容重写为单行:
const f = (A, K) => K === 0 || !A.length ? A : f([A.pop(), ...A], K-1);
您可以可视化上面的函数执行以下操作(让我们solution
为我们的示例f
函数):
f([1, 2, 3, 4], 2) = f([4] + [1, 2, 3], 1) = f([4, 1, 2, 3], 1)
f([4, 1, 2, 3], 1) = f([3, 4, 1, 2], 0) --> [3, 4, 1, 2] // we return the array instead of another function call as K === 0 (our base case)
或者当数组为空时:
f([], 10) = [] // instantly return our array as it is empty. The clause !A.length is true (also part of our base case) which means we return A (our array) instantly.
另一种全面获得 100% 的替代方案:
function solution(A, K) {
const k = K % A.length; // in case the rotation is bigger than the length of the array
if (K === 0 || k === 0) {
return A;
}
const head = A.slice(0, A.length - k);
const tail = A.slice(A.length - k, A.length);
return [...tail, ...head];
}
刚得到 100%。
function cyclicRotation(A, K){
K = K % A.length
let sliceA = A.slice(A.length-K, A.length)
let sliceB = A.slice(0, A.length-K)
return sliceA.concat(sliceB)
}
我的 100% JavaScript 解决方案,通过事先创建 A 的副本来避免更改 A 参数的值:
function solution(A, K) {
const rotatedA = [...A];
for (let i = 0; i < K; i++) {
rotatedA.length > 0 && rotatedA.unshift(rotatedA.pop());
}
return rotatedA;
}
得到 100%,请给我你的意见
function solution(A, K) {
let returnedArray = [...A];
if((A.length === K || A.length === 0 || isAllValueTheSame(A))){
return A
}
for(let i = 0;i<K;i++){
returnedArray = rotate(returnedArray)
}
return returnedArray
}
function isAllValueTheSame (A){
const firstValue = A[0]
return A.every((value) => value === firstValue)
}
function rotate (A){
let tempArray = [...A];
tempArray[0] = A[A.length-1] // the last goes first
for(let i = 0 ; i < A.length -1 ;i++){
tempArray[i+1] = A[i]
}
return tempArray
}