본문 바로가기

코딩테스트/Coding Bat

[코딩뱃] [자바] Warmup - 2단계 : arrayCount9 문제

728x90
728x90

https://codingbat.com/prob/p184031

 

CodingBat Java Warmup-2 arrayCount9

Given an array of ints, return the number of 9's in the array.arrayCount9([1, 2, 9]) → 1arrayCount9([1, 9, 9]) → 2arrayCount9([1, 9, 9, 3, 9]) → 3Go...Save, Compile, Run (ctrl-enter)Show Solution

codingbat.com



= 문제 번역 =

int 배열이 주어지면 그 배열에서 9가 몇 개 존재하는지 그 개수를 반환합니다.

 

= 문제푸는 팁 =

기본적인 nums[i]가 ==9라면 count++로 설정하기

 

= 해설 =

< 1 >
public int arrayCount9(int[] nums) {
  int count = 0;
  for (int i=0; i<nums.length; i++) {
    if (nums[i] == 9) {
      count++;
    }
  }
  return count;
}

728x90