강의로 돌아가기
팔이

Index out of bounds 에러가 뜹니다.... 아무리봐도 에러를 찾을 수 없는데 ㅠ 선배님들 도와주세요

import java.util.LinkedList;
import java.util.Queue;

class Solution {

public int[] solution(int[] p, int[] s) {


    Queue <Integer> a = new LinkedList<>();
    int index=0;
    int count=0;


    while (index < p.length ){
            for (int i=0; i<p.length ; i++){
                 p[i] += s[i];

             }

        if (p[index]>=100){
            while(p[index]>=100 && index <p.length){
                count++;
                index++;
            }
            a.offer(count);
            count=0;

         }
    }

    int[] answer = new int[a.size()];
    for (int i=0; i<answer.length ; i++)
        answer[i]= a.poll();

    return answer;


}

}

작성중인 코드―solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def solution(p, s):

    result= []

    while p:
        for i in range(len(p)):
            p[i]+=s[i]
        count=0

        while p and p[0] >=100:
            del p[0]
            del s[0]
            count+=1

        if count:
            result.append(count)

    return result
1 개의 답변
남기현

while(p[index]>=100 && index <p.length)

이 부분에서 index < p.length보다 p[index] >= 100을 먼저 체크해서 p[index]에서 에러가 나는 것으로 보이네요. java는 순서대로 확인하기 때문에 이 둘의 순서만 바꿔도 해결될 겁니다.

  • 팔이

    정말 감사합니다!!

    팔이―2020.11.05 13:06
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다.