mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 11:56:30 +08:00
build
This commit is contained in:
parent
d5a58e0deb
commit
c7794e1a84
5 changed files with 20 additions and 20 deletions
|
@ -239,7 +239,7 @@ comments: true
|
|||
|
||||
```dart title="array.dart"
|
||||
/* 随机访问元素 */
|
||||
int randomAccess(List nums) {
|
||||
int randomAccess(List<int> nums) {
|
||||
// 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
int randomIndex = Random().nextInt(nums.length);
|
||||
// 获取并返回随机元素
|
||||
|
@ -411,7 +411,7 @@ comments: true
|
|||
|
||||
```dart title="array.dart"
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
void insert(List nums, int num, int index) {
|
||||
void insert(List<int> nums, int num, int index) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
for (var i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
|
@ -573,7 +573,7 @@ comments: true
|
|||
|
||||
```dart title="array.dart"
|
||||
/* 删除索引 index 处元素 */
|
||||
void remove(List nums, int index) {
|
||||
void remove(List<int> nums, int index) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for (var i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
|
@ -772,19 +772,19 @@ comments: true
|
|||
|
||||
```dart title="array.dart"
|
||||
/* 遍历数组元素 */
|
||||
void traverse(List nums) {
|
||||
var count = 0;
|
||||
void traverse(List<int> nums) {
|
||||
int count = 0;
|
||||
// 通过索引遍历数组
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// 直接遍历数组元素
|
||||
for (var num in nums) {
|
||||
count += nums[i];
|
||||
for (int num in nums) {
|
||||
count += num;
|
||||
}
|
||||
// 通过 forEach 方法遍历数组
|
||||
nums.forEach((element) {
|
||||
count += element;
|
||||
nums.forEach((num) {
|
||||
count += num;
|
||||
});
|
||||
}
|
||||
```
|
||||
|
@ -954,7 +954,7 @@ comments: true
|
|||
|
||||
```dart title="array.dart"
|
||||
/* 在数组中查找指定元素 */
|
||||
int find(List nums, int target) {
|
||||
int find(List<int> nums, int target) {
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == target) return i;
|
||||
}
|
||||
|
@ -1143,7 +1143,7 @@ comments: true
|
|||
|
||||
```dart title="array.dart"
|
||||
/* 扩展数组长度 */
|
||||
List extend(List nums, int enlarge) {
|
||||
List<int> extend(List<int> nums, int enlarge) {
|
||||
// 初始化一个扩展长度后的数组
|
||||
List<int> res = List.filled(nums.length + enlarge, 0);
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
|
|
|
@ -158,7 +158,7 @@ comments: true
|
|||
|
||||
/* 构造函数 */
|
||||
ListNode *newListNode(int val) {
|
||||
ListNode *node, *next;
|
||||
ListNode *node;
|
||||
node = (ListNode *) malloc(sizeof(ListNode));
|
||||
node->val = val;
|
||||
node->next = NULL;
|
||||
|
@ -1285,7 +1285,7 @@ comments: true
|
|||
|
||||
/* 构造函数 */
|
||||
ListNode *newListNode(int val) {
|
||||
ListNode *node, *next;
|
||||
ListNode *node;
|
||||
node = (ListNode *) malloc(sizeof(ListNode));
|
||||
node->val = val;
|
||||
node->next = NULL;
|
||||
|
|
|
@ -1817,13 +1817,13 @@ comments: true
|
|||
/* 访问元素 */
|
||||
pub fn get(&self, index: usize) -> i32 {
|
||||
// 索引如果越界则抛出异常,下同
|
||||
if index < 0 || index >= self.size {panic!("索引越界")};
|
||||
if index >= self.size {panic!("索引越界")};
|
||||
return self.arr[index];
|
||||
}
|
||||
|
||||
/* 更新元素 */
|
||||
pub fn set(&mut self, index: usize, num: i32) {
|
||||
if index < 0 || index >= self.size {panic!("索引越界")};
|
||||
if index >= self.size {panic!("索引越界")};
|
||||
self.arr[index] = num;
|
||||
}
|
||||
|
||||
|
@ -1840,7 +1840,7 @@ comments: true
|
|||
|
||||
/* 中间插入元素 */
|
||||
pub fn insert(&mut self, index: usize, num: i32) {
|
||||
if index < 0 || index >= self.size() {panic!("索引越界")};
|
||||
if index >= self.size() {panic!("索引越界")};
|
||||
// 元素数量超出容量时,触发扩容机制
|
||||
if self.size == self.capacity() {
|
||||
self.extend_capacity();
|
||||
|
@ -1856,7 +1856,7 @@ comments: true
|
|||
|
||||
/* 删除元素 */
|
||||
pub fn remove(&mut self, index: usize) -> i32 {
|
||||
if index < 0 || index >= self.size() {panic!("索引越界")};
|
||||
if index >= self.size() {panic!("索引越界")};
|
||||
let num = self.arr[index];
|
||||
// 将索引 index 之后的元素都向前移动一位
|
||||
for j in (index..self.size - 1) {
|
||||
|
|
|
@ -288,7 +288,7 @@ comments: true
|
|||
stack.push(4);
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
let top = stack[stack.len() - 1];
|
||||
let top = stack.last().unwrap();
|
||||
|
||||
/* 元素出栈 */
|
||||
let pop = stack.pop().unwrap();
|
||||
|
|
|
@ -732,7 +732,7 @@ comments: true
|
|||
|
||||
/* 获取索引为 i 节点的父节点的索引 */
|
||||
parent(i) {
|
||||
return (i - 1) / 2;
|
||||
return Math.floor((i - 1) / 2); // 向下取整
|
||||
}
|
||||
|
||||
/* 层序遍历 */
|
||||
|
@ -818,7 +818,7 @@ comments: true
|
|||
|
||||
/* 获取索引为 i 节点的父节点的索引 */
|
||||
parent(i: number): number {
|
||||
return (i - 1) / 2;
|
||||
return Math.floor((i - 1) / 2); // 向下取整
|
||||
}
|
||||
|
||||
/* 层序遍历 */
|
||||
|
|
Loading…
Reference in a new issue