初学java,力扣里面java解题的答案,没有主方法,怎样能在IDEA里运行验证?

朱临贵朱临贵 · 2022-02-27 12:42
例如485题,答案如下: class Solution { public int findMaxConsecutiveOnes(int[] nums) { if (nums == null || nums.length == 0) { return 0; } int consecutive_ones = nums[0] == 1? 1 : 0; int max = consecutive_ones; for (int i = 1; i < nums.length; i++) { if (nums[i] == 1) { consecutive_ones += 1; } else { consecutive_ones = 0; } max = Math.max(max, consecutive_ones); } return max; } } 我想在IDEA里具体定义1个数组(比如int[] nums = {1,0,0,1,1,1,1,1,0,0,1,0,1,0};),来验证网上这个答案是否正确,要在代码里加入哪些行代码,或者如何 修改一下,才能看到输出的内容。
1 个回答
小ᯤ²ᴳ
小ᯤ²ᴳ

那就写个主方法调用这个方法就可以了!

 class XXXpublic static void main(String args[]){
int[] nums = {1,0,0,1,1,1,1,1,0,0,1,0,1,0};
System.out.println(findMaxConsecutiveOnes(nums));
}
public static int findMaxConsecutiveOnes(int[] nums) {
    if (nums == null || nums.length == 0) {
        return 0;
    }

    int consecutive_ones = nums[0] == 1? 1 : 0;
    int max = consecutive_ones;


    for (int i = 1; i < nums.length; i++) {
        if (nums[i] == 1) {
            consecutive_ones += 1;
        } else {
            consecutive_ones = 0;
        }
        max = Math.max(max, consecutive_ones);
    }

    return max;

}
 }
4
反对
1
收藏
2022-03-03 11:22