Skip to content

Commit

Permalink
3
Browse files Browse the repository at this point in the history
  • Loading branch information
TH-coder committed Mar 18, 2019
1 parent 95a4f38 commit afc3583
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 3 - 旋转数组(189)/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="index.js"></script>
</body>
</html>
36 changes: 36 additions & 0 deletions 3 - 旋转数组(189)/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var rotate1 = function (nums, k) {
for (let i = 0; i < k; i++) {
const num = nums.pop();
nums.unshift(num)
}
};


var rotate2 = function (nums, k) {
const length = nums.length;
if (length === k || length === 1) {
return nums
} else {
if (length < k) {
k = k % length
}
nums.unshift(...nums.splice(-k, k));
}
};

var rotate3 = function (nums, k) {
let l = nums.length;
k %= l;
nums.unshift(...nums.splice(nums.length - k, k))
};

const arr1 = [1, 2, 3, 4, 5, 6, 7];
const arr2 = [1, 2, 3, 4, 5, 6, 7];
const arr3 = [1, 2, 3, 4, 5, 6, 7];
const k = 3;
rotate1(arr1,k)
rotate2(arr2,k);
rotate3(arr3,k);
console.log(arr1);
console.log(arr2);
console.log(arr3);

0 comments on commit afc3583

Please sign in to comment.