LintCode: Generate ArrayList with Given Size

Generate ArrayList with Given Size



Similar Problems:


Description
Generate an arrayList with given size, initialize the array list with numbers from 1 to size.

Example

Given size = 4. return an array list that contains numbers from 1 to 4: [1,2,3,4]

Github: code.dennyzhang.com

Credits To: lintcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// Blog link: https://code.dennyzhang.com/generate-arraylist-with-given-size
// Basic Ideas:
// Complexity: Time O(n), Space O(1)
/**
 * @param size: An integer
 * @return: An integer list
 */
func generate (size int) []int {
    res := make([]int, size)
    for i, _ := range res { res[i] = i+1 }
    return res
}
linkedin
github
slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.