- 实现文本编辑器, 支持 append, insert, delete; follow up: 数据结构怎样提高搜索的效率
For a text editor, the first step should be deisgning a set of basic APIs. Since people are likely going to constantly modify what they type, an in-memory buffer should be created so that it can be saved, edited, and undo. We should expost the different possible actions for the buffer as APIs so we can provide better controls to it later.
The data structure:
Bad choice: Array.
One might think array is a good choice since new input can be appended quickly at the end of the array. However, new insertion and deletion would incur a lot of random access in the array which would make the performance very slow.