|
|
@ -24,6 +24,14 @@ comments: true
|
|
|
|
int[] nums = { 1, 3, 2, 5, 4 };
|
|
|
|
int[] nums = { 1, 3, 2, 5, 4 };
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Javascript"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript title="array.javascript"
|
|
|
|
|
|
|
|
/* 初始化数组 */
|
|
|
|
|
|
|
|
var arr = new Array(5).fill(0)
|
|
|
|
|
|
|
|
var nums = [1, 3, 2, 5, 4]
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
|
|
|
```cpp title="array.cpp"
|
|
|
@ -69,6 +77,19 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Javascript"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript title="array.javascript"
|
|
|
|
|
|
|
|
/* 随机返回一个数组元素 */
|
|
|
|
|
|
|
|
function randomAccess(nums){
|
|
|
|
|
|
|
|
// 在区间 [0, nums.length) 中随机抽取一个数字
|
|
|
|
|
|
|
|
const random_index = Math.floor(Math.random() * nums.length)
|
|
|
|
|
|
|
|
// 获取并返回随机元素
|
|
|
|
|
|
|
|
random_num = nums[random_index]
|
|
|
|
|
|
|
|
return random_num
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
|
|
|
```cpp title="array.cpp"
|
|
|
@ -114,6 +135,22 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Javascript"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript title="array.javascript"
|
|
|
|
|
|
|
|
/* 扩展数组长度 */
|
|
|
|
|
|
|
|
function extend(nums, enlarge){
|
|
|
|
|
|
|
|
// 初始化一个扩展长度后的数组
|
|
|
|
|
|
|
|
let res = new Array(nums.length + enlarge).fill(0)
|
|
|
|
|
|
|
|
// 将原数组中的所有元素复制到新数组
|
|
|
|
|
|
|
|
for(let i=0; i<nums.length;i++){
|
|
|
|
|
|
|
|
res[i] = nums[i]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回扩展后的新数组
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
|
|
|
```cpp title="array.cpp"
|
|
|
@ -178,6 +215,28 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Javascript"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript title="array.javascript"
|
|
|
|
|
|
|
|
/* 在数组的索引 index 处插入元素 num */
|
|
|
|
|
|
|
|
function insert(nums, num, index){
|
|
|
|
|
|
|
|
// 把索引 index 以及之后的所有元素向后移动一位
|
|
|
|
|
|
|
|
for (let i = nums.length - 1; i >= index; i--) {
|
|
|
|
|
|
|
|
nums[i] = nums[i - 1];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 将 num 赋给 index 处元素
|
|
|
|
|
|
|
|
nums[index] = num;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 删除索引 index 处元素 */
|
|
|
|
|
|
|
|
function remove(nums, index){
|
|
|
|
|
|
|
|
// 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
|
|
|
for (let i = index; i < nums.length - 1; i++) {
|
|
|
|
|
|
|
|
nums[i] = nums[i + 1]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
|
|
|
```cpp title="array.cpp"
|
|
|
@ -239,6 +298,23 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Javascript"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript title="array.javascript"
|
|
|
|
|
|
|
|
/* 遍历数组 */
|
|
|
|
|
|
|
|
function traverse(nums){
|
|
|
|
|
|
|
|
let count = 0
|
|
|
|
|
|
|
|
// 通过索引遍历数组
|
|
|
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 直接遍历数组
|
|
|
|
|
|
|
|
for(let num of nums){
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
|
|
|
```cpp title="array.cpp"
|
|
|
@ -281,6 +357,19 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Javascript"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript title="array.javascript"
|
|
|
|
|
|
|
|
/* 在数组中查找指定元素 */
|
|
|
|
|
|
|
|
function find(nums, target){
|
|
|
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
|
|
|
if (nums[i] == target)
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="array.cpp"
|
|
|
|
```cpp title="array.cpp"
|
|
|
|