← 返回 jpmorgan 的题目列表Generate Table of Contents
类型:online_judge
You are given a document as an array of strings. Your task is to generate a Table of Contents (TOC) based on simple markup rules.\n- A chapter is any line starting with # .\n- A section is any line starting with ## and belongs to the most recent chapter.\n- All other lines must be ignored.\n\nBuild the TOC using the following numbering:\n- Chapters: 1, 2, 3,...\n- Sections: 1.1, 1.2, 2.1, etc. (chapter number first, then section number)\n\nOutput the TOC as an array of strings where:\n- Chapter entries look like: "1. Title"\n- Section entries look like: "1.1. Title"\n\nExample:\nSuppose text contains 12 strings:\n\n"# Algorithms" \n"This chapter covers the most basic algorithms."\n"## Sorting"\n"Quicksort is fast and widely used in practice"\n"Merge sort is a deterministic algorithm"\n"## Searching"\n"DFS and BFS are widely used graph searching algorithms"\n"Some variants of DFS are also used in game theory applications"\n"# Data Structures"\n"This chapter is all about data structures"\n"It's a draft for now and will contain more sections in the future"\n"# Binary Search Trees"\n\nShould output:\n\n["1. Algorithms", "1.1. Sorting", "1.2. Searching", "2. Data Structures", "3. Binary Search Trees"]\n\n\nConstraints:\n- 1 ≤ size of text[] ≤ 1000\n- 1 ≤ length of text[i] ≤ 100\n- When a line starts with # or ## , these special characters are always followed by a space.\n- The first line of the text is guaranteed to be a chapter line.
Example
Input
['# Algorithms', 'This chapter covers the most basic algorithms.', '## Sorting', 'Quicksort is fast and widely used in practice', 'Merge sort is a deterministic algorithm', '## Searching', 'DFS and BFS are widely used graph searching algorithms', 'Some variants of DFS are also used in game theory applications', '# Data Structures', 'This chapter is all about data structures', 'It\'s a draft for now and will contain more sections in the future', '# Binary Search Trees']
Output
['1. Algorithms', '1.1. Sorting', '1.2. Searching', '2. Data Structures', '3. Binary Search Trees']