[프로그래머스/Java] Lv.0 첫 번째로 나오는 음수

febCho·2024년 3월 9일
0

코딩테스트

목록 보기
57/253
post-thumbnail

문제

정수 리스트 num_list가 주어질 때, 첫 번째로 나오는 음수의 인덱스를 return하도록 solution 함수를 완성해주세요. 음수가 없다면 -1을 return합니다.

- 제한사항4

  • 5 ≤ num_list의 길이 ≤ 100
  • -10 ≤ num_list의 원소 ≤ 100

풀이

class Solution {
    public int solution(int[] num_list) {
        int answer = 0;
        
        for(int i=0;i<num_list.length;i++){
            if(num_list[i] < 0){
                answer = i;
                break;
            }else{
                answer = -1;
            }
        }
        
        return answer;
    }
}

결과

profile
Done is better than perfect.

0개의 댓글