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] isiz
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]
i<size+1 比 i<=size 更快
public class Solution { /** * @param size: An integer * @return: An integer list */ public List<Integer> generate(int size) { // write your code here List<Integer> list = new ArrayList<>(); for (int i=1; i<size+1; i++){ list.add(i); } return list; } }
描述 给你一个大小size,生成一个1-size的数组 您在真实的面试中是否遇到过这个题? 样例 size = 4. 返回 [1,2,3,4]