inblog logo
|
stupefyee
    알고리즘문제풀기

    [JAVA 문제 풀이] 248. 같은 숫자는 싫어

    프로그래머스 (12906)
    Stupefyee's avatar
    Stupefyee
    May 12, 2025
    [JAVA 문제 풀이] 248. 같은 숫자는 싫어
    Contents
    내가 작성한 코드다른 사람의 코드
    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/12906
    notion image
     

    내가 작성한 코드

    import java.util.*; public class Solution { public int[] solution(int[] arr) { Stack<Integer> stack = new Stack<>(); stack.push(arr[0]); for (int i = 1; i < arr.length; i++) { if (stack.peek() == arr[i]) { continue; } else { stack.push(arr[i]); } } int answer[] = new int[stack.size()]; for (int i = stack.size() - 1; i >= 0; i--) { answer[i] = stack.pop(); } return answer; } }
     

    다른 사람의 코드

    import java.util.*; public class Solution { public int[] solution(int []arr) { ArrayList<Integer> tempList = new ArrayList<Integer>(); int preNum = 10; for(int num : arr) { if(preNum != num) tempList.add(num); preNum = num; } int[] answer = new int[tempList.size()]; for(int i=0; i<answer.length; i++) { answer[i] = tempList.get(i).intValue(); } return answer; } }
     
    Share article
    Contents
    내가 작성한 코드다른 사람의 코드

    stupefyee

    RSS·Powered by Inblog