chapter_array_and_linkedlist/array/ #84
Replies: 107 comments 161 replies
-
|
缓存局部性:简单的说是和命中率有关系,分时间局部性和空间局部性 |
Beta Was this translation helpful? Give feedback.
-
|
很不错,数组的插入和删除理解的重点是在索引 |
Beta Was this translation helpful? Give feedback.
-
|
删除索引 index 元素时,可在代码中,循环结束后,将 nums 最后位置元素重置为 0,nums[-1] = 0, 使之与图片中的末尾元素无意义概念一致。 另外,插入元素时,Python 代码中,索引是否正确? 我的理解: for i in range(len(nums) - 1, index +1, -1). |
Beta Was this translation helpful? Give feedback.
-
|
已fork,正着手编写数组的Go示例 |
Beta Was this translation helpful? Give feedback.
-
|
数组中首个元素对应数组的内存地址偏移量是0,所以数组索引从0开始算起,学到了,感谢!!! |
Beta Was this translation helpful? Give feedback.
-
|
/* 初始化数组 */ |
Beta Was this translation helpful? Give feedback.
-
|
数组遍历 C++ 代码示例,只是 count++,没有遍历数组,是不是本来要写成 count += nums[i]? |
Beta Was this translation helpful? Give feedback.
-
|
代码示例 应该加一下语言的语法糖写法? 比如 rust 很多情况是不用 for的 根据情况编译后的效率也比for循环高效 虽然有些矛盾。 但是我觉得可以列出两种实现方式, 一个是for 理解原理另一个是如何更简便的写法。 |
Beta Was this translation helpful? Give feedback.
-
|
这个网站怎么没有记录功能,是因为没有后端用户服务器吗,, |
Beta Was this translation helpful? Give feedback.
-
|
作者您好,为什么在这里会强调“相同类型的元素”,而在下一章链表中却没有强调同类型呢? |
Beta Was this translation helpful? Give feedback.
-
|
之前都是在用,没有看到这么底层 |
Beta Was this translation helpful? Give feedback.
-
|
“数组有多种初始化写法。根据实际需要,选代码最短的那一种就好。” k神,个人感觉这句话有点奇怪, |
Beta Was this translation helpful? Give feedback.
-
|
请问计算内存地址那里为什么元素长度为4呢?我看数组长度为5,为什么不是5呢 |
Beta Was this translation helpful? Give feedback.
-
|
您好还是不太理解c++数组删除那块,我知道有效长度-1之后最末尾的元素可以忽略掉(无意义),可是这在源代码中没有体现,请问这该如何实现呀?第二个问题是在插入的时候不应该先判断是否满数组再扩容吗?感谢解答 |
Beta Was this translation helpful? Give feedback.
-
|
Hello! K大,想问下您的图都是怎么画的啊,感觉也太整齐美观了,想学习一下 |
Beta Was this translation helpful? Give feedback.
-
|
从地址计算公式的角度看,索引本质上是内存地址的偏移量。突然就醍醐灌顶了。 |
Beta Was this translation helpful? Give feedback.
-
|
作者大大您好,我是一个初学者,所以不太清楚c++的访问元素以及后续的代码中为什么要在括号里加入int *nums这一部分,我查到了这个是指针的意思,它的具体作用是什么嘞 |
Beta Was this translation helpful? Give feedback.
-
|
原来如此!非常感谢作者大大
发自我的iPhone
…------------------ Original ------------------
From: Aldebaran-ls ***@***.***>
Date: Fri,Nov 22,2024 0:12 AM
To: krahets/hello-algo ***@***.***>
Cc: TOKKOW ***@***.***>, Comment ***@***.***>
Subject: Re: [krahets/hello-algo] chapter_array_and_linkedlist/array/(Discussion #84)
|
Beta Was this translation helpful? Give feedback.
-
|
准备去集中刷一下数组的题,巩固一下所学哒。加油! |
Beta Was this translation helpful? Give feedback.
-
|
day3,争取一天一章 |
Beta Was this translation helpful? Give feedback.
-
|
数组的长度不可变,结构紧凑,所以适用于查找搜索而不适用于元素的删增 |
Beta Was this translation helpful? Give feedback.
-
|
求教,这个在线看怎么找到自己的评论啊,发出去 的找不到 |
Beta Was this translation helpful? Give feedback.
-
|
这个里面的拓展数组容量中c语言的写法是不是有点错误,初始化扩展后的空间应该在将原数组中的所有元素复制到新数组的前面吧 |
Beta Was this translation helpful? Give feedback.
-
|
Java 实现数组类型 import java.lang.reflect.Array;
public class MyArray<T> {
private T[] array;
@SuppressWarnings("unchecked")
public MyArray(int len, Class<T> clazz) {
this.array = (T[]) Array.newInstance(clazz, len);
}
public T get(int index) {
indexChecker(index);
return array[index];
}
public void set(int index, T t) {
indexChecker(index);
array[index] = t;
}
public int lenth() {
return array.length;
}
public void insert(int index, T t) {
indexChecker(index);
for (int i = lenth() - 1; i > index; i--) {
array[i] = array[i - 1];
}
array[index] = t;
}
public void remove(int index) {
indexChecker(index);
for (int i = index; i < lenth() - 1; i++) {
array[i] = array[i + 1];
}
}
public int find(T target) {
for (int i = 0; i < lenth(); i++) {
if (get(i) == target) {
return i;
}
}
return -1;
}
public void trarverse(ArrayConsumer<T> consumer) {
for (int i = 0; i < lenth(); i++) {
consumer.accept(array[i], i);
}
}
public MyArray<T> extend(int enlarge, Class<T> clazz) {
MyArray<T> newArray = new MyArray<>(lenth() + enlarge, clazz);
trarverse((value, index) -> {
newArray.set(index, value);
});
return newArray;
}
private void indexChecker(int index) {
if (index < 0 || index >= lenth()) {
throw new ArrayIndexOutOfBoundsException("out of index: " + index);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
C#备忘 namespace _1.Console;
public class CustomArray<T>(int size)
{
public const string Description = "自定义数组实现";
private readonly T[] _array = new T[size];
public int Length => _array.Length;
public T this[int index]
{
get
{
IsValidIndex(index);
return _array[index];
}
set
{
IsValidIndex(index);
_array[index] = value;
}
}
private void IsValidIndex(int index)
{
if (index < 0 || index >= _array.Length)
throw new IndexOutOfRangeException("索引超出数组范围");
}
public void Insert(int index, T value)
{
IsValidIndex(index);
for (int i = _array.Length - 1; i > index; i--)
_array[i] = _array[i - 1];
_array[index] = value;
}
public void Remove(int index)
{
IsValidIndex(index);
for (int i = index; i < _array.Length - 1; i++)
_array[i] = _array[i + 1];
// 将最后一个元素置为默认值
_array[^1] = default!;
}
} |
Beta Was this translation helpful? Give feedback.
-
|
day03 |
Beta Was this translation helpful? Give feedback.
-
|
为什么怎么看这都是数组求和:def traverse(nums: list[int]): |
Beta Was this translation helpful? Give feedback.
-
|
day4 |
Beta Was this translation helpful? Give feedback.
-
|
哈喽作者大大,我觉得python的代码,insert list 那里不合理,应该先扩容的,不然最后一个元素在insert后会丢失的,这是改进后的代码 def insert(nums, num, index): |
Beta Was this translation helpful? Give feedback.
-
|
有点抽象,感觉概念突脸还是太难理解了,慢慢来吧 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
chapter_array_and_linkedlist/array/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_array_and_linkedlist/array/
Beta Was this translation helpful? Give feedback.
All reactions