Merge branch 'dev' of github.com:justin-tse/hello-algo into justin-tse-dev

This commit is contained in:
Yudong Jin
2022-12-13 00:06:18 +08:00
10 changed files with 214 additions and 212 deletions

View File

@@ -5,30 +5,30 @@
*/
/* 随机访问元素 */
function randomAccess(nums){
function randomAccess(nums) {
// 在区间 [0, nums.length) 中随机抽取一个数字
const random_index = Math.floor(Math.random() * nums.length);
// 获取并返回随机元素
random_num = nums[random_index];
const random_num = nums[random_index];
return random_num;
}
/* 扩展数组长度 */
// 请注意,Python 的 list 是动态数组,可以直接扩展
// 为了方便学习,本函数将 list 看作是长度不可变的数组
function extend(nums, enlarge){
// 初始化一个扩展长度后的数组
let res = new Array(nums.length + enlarge).fill(0);
// 将原数组中的所有元素复制到新数组
for(let i=0; i<nums.length;i++){
// 请注意,JavaScript 的 Array 是动态数组,可以直接扩展
// 为了方便学习,本函数将 Array 看作是长度不可变的数组
function extend(nums, enlarge) {
// 初始化一个扩展长度后的数组
const res = new Array(nums.length + enlarge).fill(0);
// 将原数组中的所有元素复制到新数组
for (let i = 0; i < nums.length; i++) {
res[i] = nums[i];
}
}
// 返回扩展后的新数组
return res;
}
/* 在数组的索引 index 处插入元素 num */
function insert(nums, num, index){
function insert(nums, num, index) {
// 把索引 index 以及之后的所有元素向后移动一位
for (let i = nums.length - 1; i >= index; i--) {
nums[i] = nums[i - 1];
@@ -38,41 +38,39 @@ function insert(nums, num, index){
}
/* 删除索引 index 处元素 */
function remove(nums, index){
function remove(nums, index) {
// 把索引 index 之后的所有元素向前移动一位
for (let i = index; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
}
}
/* 遍历数组 */
function traverse(nums){
function traverse(nums) {
let count = 0;
// 通过索引遍历数组
for (let i = 0; i < nums.length; i++) {
count++;
count++;
}
// 直接遍历数组
for(let num of nums){
count += 1;
}
for (let num of nums) {
count += 1;
}
}
/* 在数组中查找指定元素 */
function find(nums, target){
function find(nums, target) {
for (let i = 0; i < nums.length; i++) {
if (nums[i] === target)
return i;
if (nums[i] == target) return i;
}
return -1;
}
/* Driver Codes*/
/* 初始化数组 */
var arr = new Array(5).fill(0);
const arr = new Array(5).fill(0);
console.log("数组 arr =", arr);
var nums = [1, 3, 2, 5, 4];
const nums = [1, 3, 2, 5, 4];
console.log("数组 nums =", nums);
/* 随机访问 */
@@ -95,5 +93,5 @@ console.log("删除索引 2 处的元素,得到 nums =", nums);
traverse(nums);
/* 查找元素 */
var index = find(nums, 3);
const index = find(nums, 3);
console.log("在 nums 中查找元素 3 ,得到索引 =", index);