티스토리 뷰

반응형

 

 

18일차 코드 리뷰를 진행해보자.

Day 18 결과

 

 

오늘 문제는 Tutorial에서 배웠던

Stack, Queue를 선언하고

 push, pop, enqueue, dequeue

메서드를 구현하는 문제였다.

 

수업시간에 배운 내용을 조금만 응용하면 되는 문제였다.

바로 코드를 통해 확인해보자.

 

 

 

import java.io.*;
import java.util.*;

public class Solution {
    // Write your code here.
   LinkedList<Character> queue = new LinkedList<>();
   Stack<Character> stack = new Stack<>();

    // Write your code here.
    // pushCharacter();
    public void pushCharacter(char c) { stack.push(c); }

    // enqueueCharacter();
    public void enqueueCharacter(char c) { queue.addLast(c); }

    // popCharacter();
    public char popCharacter() { return stack.pop(); }

    // dequeueCharacter();
    public char dequeueCharacter() { return queue.remove(); }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();
        scan.close();

        // Convert input String to an array of characters:
        char[] s = input.toCharArray();

        // Create a Solution object:
        Solution p = new Solution();

        // Enqueue/Push all chars to their respective data structures:
        for (char c : s) {
            p.pushCharacter(c);
            p.enqueueCharacter(c);
        }

        // Pop/Dequeue the chars at the head of both data structures and compare them:
        boolean isPalindrome = true;
        for (int i = 0; i < s.length/2; i++) {
            if (p.popCharacter() != p.dequeueCharacter()) {
                isPalindrome = false;                
                break;
            }
        }

        //Finally, print whether string s is palindrome or not.
        System.out.println( "The word, " + input + ", is " 
                           + ( (!isPalindrome) ? "not a palindrome." : "a palindrome." ) );
    }
}

 

 

 

문제에서 주어진 부분을 제외하고

직접 구현해야 하는 부분만 살펴보자.

 

// Write your code here.
LinkedList<Character> queue = new LinkedList<>();
Stack<Character> stack = new Stack<>();

// Write your code here.
// pushCharacter();
public void pushCharacter(char c) { stack.push(c); }

// enqueueCharacter();
public void enqueueCharacter(char c) { queue.addLast(c); }

// popCharacter();
public char popCharacter() { return stack.pop(); }

// dequeueCharacter();
public char dequeueCharacter() { return queue.remove(); }

 

이 부분에서 queue와 stack을 선언하고

push, enqueue, pop, dequeue를 선언해주면 된다.

 

push와 pop은 stack에

enqueue와 dequeue는 queue에

사용한다는 것은 다들 알고 있을 것이고

 

push, enqueue처럼 추가하는 부분은

매개변수로 값을 가져받아와야 하고

함수는 void 타입이며

 

pop, dequeue처럼 제거하는 부분은

매개변수로 값을 받을 필요없이

char 타입으로 제거한 결과를 반환한다.

 

 


 

전에도 말했다싶이

해커랭크는 빈칸 채우기 문제?가 많이 출제되는데

백준을 온전히 풀기에는 어렵지만

개념은 알고 있는 사람이 풀면 좋을 듯한 문제 수준이다.

 

필자도 현재 그 수준이라 생각하여

해커랭크 챌린지를 하면서

쉽게 푼 문제도 있고

해맨 문제도 있고

어려워서 손도 못댄 문제도 있다.

 

빈칸 채우기로 문제의 핵심만 파악하면서

문제 해결의 갈피를 잡는 뿌리를 늘리고 있는 느낌이다.

 

그럼 우리는 내일 19일 챌린지를 통해 만나자. 😆

 

 

반응형