341. Flatten Nested List iterator Expand multilayer arrays

Source: Internet
Author: User

[Copy question]:

Given a nested list of integers, implement an iterator to flatten it.

Each element was either an integer, or a list--whose elements may also be integers or other lists.

Example 1:
Given the list [[1,1],2,[1,1]] ,

By calling next repeatedly until hasnext returns FALSE, the order of elements returned by next should be: [1,1,2,1,1] .

Example 2:
Given the list [1,[4,[6]]] ,

By calling next repeatedly until hasnext returns FALSE, the order of elements returned by next should be: [1,4,6] .

[Brute force solution]:

Time Analysis:

Spatial Analysis:

[After optimization]:

Time Analysis:

Spatial Analysis:

[Wonderful output CONDITIONS]:

[Wonderful corner case]:

[Thinking questions]:

Don't know what the difference is between next and. Hasnext: Take it out and just see if there's

[a sentence of thought]:

[input]: null: Normal: Large: Extra Small: Special cases handled in the program: abnormal conditions (unreasonable input):

[Drawing]:

[One brush]:

[Two brushes]:

[Three brushes]:

[Four brushes]:

[Five brushes]:

[Results of five-minute visual debug]:

[Summary]:

[Complexity]:time Complexity:o () Space complexity:o ()

[English data structure or algorithm, why not other data structures or algorithms]:

Only the stack can be removed one layer at a time, and the array cannot be removed directly from one layer. So use stack.

Stack has

.getInteger()
.getList()

Method

[Algorithmic thinking: Recursion/division/greed]:

[Key templating code]:

[Other solutions]:

[Follow up]:

[The problem given by the LC becomes variable]:

[Code Style]:

341. Flatten Nested List iterator Expand multilayer arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.