[프로그래머스] Lv.0 정수 찾기.java

hgghfgf·2023년 4월 26일
0

프로그래머스

목록 보기
8/227

정수 찾기.java

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

answer 변수를 0으로 초기화하고, 반복문을 이용해 num_list의 각 원소를 하나씩 순회하면서 n과 일치하는 원소가 있으면 answer를 1로 변경합니다. 이후 answer를 반환합니다.

출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

0개의 댓글