Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

How to retrieve an element from a jagged array given its true index?

If one has a jagged array and the "true index" of an element, which is just the index of the element if the array was flattened (coined by me, I don't know what that's called), what is the fastest way to retrieve the element, given it's index?

For example, take this array:

[[0, 1, 2, 3, 4],
 [5, 6, 7, 8, 9, 10, 11, 12, 13],
 [14, 15],
 [16, 17, 18]]

The value 11 in this jagged 2D array would have an index of 11. What is the fastest and cleanest way to retrieve the value of an element in a jagged array (let's say it's 2D if that makes it easier) given it's "true index"?

Would appreciate a javascript answer, but as this is a general algorithmic question, I don't care what language it is in. If you will provide an answer that is not in javascript, perhaps don't use libraries/modules that wouldn't be available in javascript.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...