프로그래머스(자바)

    주사위의 개수 → 연산의 순서에 주의하자 (괄호의 중요성)

    나의 틀린 풀이 public class Solution { public int solution(int[] box, int n) { int answer = 0; answer=box[0]/n * box[1]/n * box[2]/n; return answer; } } ▶ 위와 같은 경우 몫* 몫 * 몫이 아니라 ▶ 몫*box[1]가 되는 불상사가 일어난다. 올바른 풀이 public class Solution { public int solution(int[] box, int n) { int answer = 0; answer=(box[0]/n) * (box[1]/n) * (box[2]/n); return answer; } } ▶ 괄호를 반드시 해줘서 올바른 연산을 하자

    약수 구하기 - IntStream.rangeClosed(1, n)

    나의 풀이 import java.util.ArrayList; class Solution { public int[] solution(int n) { int[] answer = {}; ArrayList arr= new ArrayList(); for(int i=1; ii).toArray(); } } ▶ 배열의 크기가 일정하지 않은 경우에는 일단 list에 넣자★★ 다른 사람의 풀이 import java.util.stream.IntStream; import java.util.Arrays; class Solution { public int[] solution(int n) { return IntStream.rangeClosed(1, n).filter(i -> n % i == 0).toArray(); } } ▶ 연속..

    가장 큰 수 찾기→max(Integer::compareTo).ortElse(0) ★

    나의 풀이 import java.util.*; class Solution { public int[] solution(int[] array) { ArrayList list = new ArrayList(); int max=0; int idx=0; for(int i=0; imax){ max=array[i]; idx=i; } } list.add(max); list.add(idx); return list.stream().mapToInt(i -> i).toArray(); } } ▶ 배열을 리스트로 전환하여 반환 다른 사람의 풀이1 import java.util.*; import java.util.stream.Collectors; class Solution..

    n배수 고르기 - List를 int형 배열로 전환★ +filter()★

    나의 풀이 import java.util.*; class Solution { public int[] solution(int n, int[] numlist) { List list = new ArrayList(); for(int i=0; ii).toArray(); } } ▶ list.stream().mapToInt(i->i).toArray() ▶ mapToInt( i → i) 리스트의 Integer요소타입을 int타입으로 바꾼다.★★ 다른 사람의 풀이 import java.util.Arrays; class Solution { public int[] solution(int n, int[] numList) { return Arrays.stream(numList).filter(value -> value % n =..