编译问题

Compilation Issues

我在https://leetcode.com/上为帕斯卡三角形写了下面的代码,得到的错误如下:

Line 10: error: incompatible types: int cannot be converted to List<List<Integer>>.

    public class Solution {
        public List<List<Integer>> generate(int numRows) {
        List list;
        int  temp;
        for(int i=0;i<numRows;i++) {
            temp = (int) Math.pow(11,i);
            list.add(Arrays.asList(temp));

        }
        return temp;
    }
    public static void main(String s[]) {
        Solution solution = new Solution();
        java.util.Scanner scan = new java.util.Scanner();
        System.out.println("Enter the no.of Rows");
        int numRows = scan.nextInt();
        solution.generate(numRows);
    }
}

帮我找到解决办法。

 public List<Integer> generate(int numRows) {
    List list;
    int  temp;
    for(int i=0;i<numRows;i++) {
        temp = (int) Math.pow(11,i);
        list.add(Arrays.asList(temp));

    }
    return list;

为什么 returning int 值为 List return 类型。请将其更改为列表。它将正确编译。

你几乎答对了。只是几个错误。这是一个很好的:

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> generate(int numRows) {
        List<Integer> list=new ArrayList<Integer>();
        int temp;
        for (int i = 0; i < numRows; i++) {
            temp = (int) Math.pow(11, i);
            list.add(temp);
        }
        return list;
    }

    public static void main(String s[]) {
        Solution solution = new Solution();
        java.util.Scanner scan = new java.util.Scanner(System.in);
        System.out.println("Enter the no.of Rows");
        int numRows = scan.nextInt();
        Object answer=solution.generate(numRows);
        System.out.println(answer);
    }
}

您的方法被定义为 returning List<List<Integer>>(整数列表的列表),但您正在尝试 return 一个整数。

您已经在方法中创建了 List,因此您应该 return,而不是整数 temp