Wednesday, February 3, 2016

[LintCode] 485 Generate ArrayList with Given Size

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]


Code
public class Solution {
    /**
     * @param size an integer
     * @return an array list
     */
    public ArrayList<Integer> generate(int size) {
        // Write your code here
        ArrayList<Integer> result = new ArrayList<>();
        
        for (int i = 1; i <= size; i++) {
            result.add(i);
        }
        
        return result;
    }
}