Junior — Middle
Kuidas määratletakse sisemise massiivi elementidele juurdepääsu ajakulud dünaamilises lõigus?
sobes.tech AI
Vastus AI-lt
In Go, access to slice elements by index has a time complexity of O(1), because a slice is a structure containing a pointer to an internal array, its length, and capacity. When accessing an element, direct access to the array element via an offset occurs, which is performed in constant time.
Example:
s := []int{10, 20, 30, 40}
fmt.Println(s[2]) // Fast access to the element with index 2
Thus, regardless of the size of the slice, access to an element by index is always efficient and does not depend on the length of the slice.