|
|
|
@ -58,8 +58,8 @@ comments: true
|
|
|
|
|
|
|
|
|
|
```typescript title="array.ts"
|
|
|
|
|
/* 初始化数组 */
|
|
|
|
|
let arr: number[] = new Array(5).fill(0)
|
|
|
|
|
let nums: number[] = [1, 3, 2, 5, 4]
|
|
|
|
|
let arr: number[] = new Array(5).fill(0);
|
|
|
|
|
let nums: number[] = [1, 3, 2, 5, 4];
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
@ -138,11 +138,11 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 随机返回一个数组元素 */
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
@ -153,10 +153,10 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
/* 随机返回一个数组元素 */
|
|
|
|
|
function randomAccess(nums: number[]): number {
|
|
|
|
|
// 在区间 [0, nums.length) 中随机抽取一个数字
|
|
|
|
|
const random_index = Math.floor(Math.random() * nums.length)
|
|
|
|
|
const random_index = Math.floor(Math.random() * nums.length);
|
|
|
|
|
// 获取并返回随机元素
|
|
|
|
|
const random_num = nums[random_index]
|
|
|
|
|
return random_num
|
|
|
|
|
const random_num = nums[random_index];
|
|
|
|
|
return random_num;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
@ -234,11 +234,11 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 扩展数组长度 */
|
|
|
|
|
function extend(nums, enlarge){
|
|
|
|
|
function extend(nums, enlarge) {
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
|
let res = new Array(nums.length + enlarge).fill(0);
|
|
|
|
|
const res = new Array(nums.length + enlarge).fill(0);
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
for(let i=0; i<nums.length;i++){
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
res[i] = nums[i];
|
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
@ -252,13 +252,13 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
/* 扩展数组长度 */
|
|
|
|
|
function extend(nums: number[], enlarge: number): number[] {
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
|
const res = new Array(nums.length + enlarge).fill(0)
|
|
|
|
|
const res = new Array(nums.length + enlarge).fill(0);
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
for (let i = 0; i < nums.length; i++){
|
|
|
|
|
res[i] = nums[i]
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
res[i] = nums[i];
|
|
|
|
|
}
|
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
|
return res
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
@ -356,7 +356,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 在数组的索引 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];
|
|
|
|
@ -366,7 +366,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除索引 index 处元素 */
|
|
|
|
|
function remove(nums, index){
|
|
|
|
|
function remove(nums, index) {
|
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for (let i = index; i < nums.length - 1; i++) {
|
|
|
|
|
nums[i] = nums[i + 1];
|
|
|
|
@ -381,17 +381,17 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
function insert(nums: number[], num: number, index: number): void {
|
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
|
|
|
|
for (let i = nums.length - 1; i >= index; i--) {
|
|
|
|
|
nums[i] = nums[i - 1]
|
|
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
|
}
|
|
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num
|
|
|
|
|
nums[index] = num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 删除索引 index 处元素 */
|
|
|
|
|
function remove(nums: number[], index: number): void {
|
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for (let i = index; i < nums.length - 1; i++) {
|
|
|
|
|
nums[i] = nums[i + 1]
|
|
|
|
|
nums[i] = nums[i + 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
@ -466,14 +466,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 遍历数组 */
|
|
|
|
|
function traverse(nums){
|
|
|
|
|
function traverse(nums) {
|
|
|
|
|
let count = 0;
|
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
// 直接遍历数组
|
|
|
|
|
for(let num of nums){
|
|
|
|
|
for (let num of nums) {
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -484,14 +484,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
```typescript title="array.ts"
|
|
|
|
|
/* 遍历数组 */
|
|
|
|
|
function traverse(nums: number[]): void {
|
|
|
|
|
let count = 0
|
|
|
|
|
let count = 0;
|
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
count++
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
// 直接遍历数组
|
|
|
|
|
for(let num of nums){
|
|
|
|
|
count += 1
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
@ -557,10 +557,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
|
|
|
|
|
```javascript title="array.js"
|
|
|
|
|
/* 在数组中查找指定元素 */
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
@ -573,10 +572,10 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
|
function find(nums: number[], target: number): number {
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
if (nums[i] === target) {
|
|
|
|
|
return i
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|