Why it has this error“Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1”


  • 0
    C

    public class PascalTriangle {
    static ArrayList<ArrayList<Integer>> all=new ArrayList<ArrayList<Integer>>();
    public static ArrayList<ArrayList<Integer>> generate(int numRows) {
    int i=0;
    int count;
    ArrayList<ArrayList<Integer>> all=new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> list = new ArrayList<Integer>();
    while(i<=numRows)
    {
    if(i==0)
    {
    list.add(1);
    all.add(list);
    }
    if(i==1)
    {
    list.add(1);
    list.add(1);
    all.add(list);
    }
    if(i>=2)
    {
    list.add(1);
    int k=i;
    k--;
    for(int j=0;j<k;j++)
    {
    count=all.get(k).get(j)+all.get(k).get(j+1);
    list.add(count);
    }
    list.add(1);
    all.add(list);
    }
    list.removeAll(list);
    i++;
    }
    i=0;
    return all;
    }


  • 0
    T

    at least, make sure your code is readable


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.