본문 바로가기

전체 글

Spring의 @Configuration, @Bean 스프링을 공부한지 얼마 안되서 아직은 모르는게 많다. 그래서 책을 읽어보면서 모르는 내용이 나오면 검색해서 알아보고 포스트를 작성하는 식으로 조금 꼼꼼하게 진행하고자 한다. 먼저 스프링을 설치하고 예제로 작성했던 코드 중 일부는 다음과 같다. package basic; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppContext { @Bean public Greeter greeter(){ Greeter g = new Greeter(); g.setFormat("%s, Hello!"); return .. 더보기
Java의 HashSet에서 String, Object의 비교 자바에서는 고유한 객체들을 담을 수 있는 Set 자료구조, 정확히는 인터페이스를 제공하고 있다. 이를 구현한 객체는 HashSet, TreeSet, LinkedHashSet 등이 있으며 자주 사용하는 것은 HashSet일 것이다. 그런데 요즘 LeetCode에서 알고리즘 공부를 하다가 HashSet에 String 객체를 넣었을 때 이를 어떻게 비교하는 거지?라는 궁금증이 들었다. 분명 String도 결국엔 객체고 일반적으로 서로 다른 객체끼리는 다르다고 비교되는데 문자열은 어떻게 비교되는 것일까? 문득 HashSet의 contains 메서드를 사용하다가 이와 같은 의문이 들었기 때문에 좀 조사해보았다. 예전에 학교 다닐 때 자바 수업에서도 귀가 아프게 들었던 얘기지만 String 클래스의 equals, .. 더보기
Flood Fill (DFS) LeetCode의 Queue, Stack 튜토리얼의 마지막 섹션의 세 번째 문제인 Flood Fill이다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제는 간단하다. 2차원 배열로 주어진 이미지 픽셀 값에 대하여 특정 위치의 좌표와 새로운 값을 주면 해당 좌표부터 시작해서 동서남북 4방향으로 탐색하며 시작 좌표와 동일한 값을 가진 픽셀을 새로운 .. 더보기
Decode String (Stack) LeetCode의 Queue, Stack 튜토리얼의 마지막 섹션의 세 번째 문제인 Decode String이다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 일정한 규칙으로 주어진 문자열을 해독하는 것이 목적인데 문제의 규칙은 다음과 같다. 문자열에 k[encoded_string] 포맷이 있다면 encoded_string을 k번 반복해서 이어 붙인 .. 더보기
Implement Stack using Queues LeetCode의 Queue, Stack 튜토리얼에서 마지막 섹션의 두 번째 문제인 Implement Stack using Queues다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 이번 문제에서는 지난 문제(스택을 이용한 큐 구현)처럼 큐를 이용하여 스택을 구현하는 것을 목표로 하고 있다. 역시 큐 자료구조에서 사용할 수 있는 연산(push, p.. 더보기
Implement Queue using Stacks 이번 문제는 LeetCode의 Queue, Stack 튜토리얼에서 마지막 섹션의 첫 번째 문제인 Implement Queue using Stacks다. 이전에 학습할 때 큐와 스택의 가장 큰 차이점은 선입선출이냐 선입후출이냐의 차이였다. 즉 먼저 삽입된 요소가 먼저 삭제되거나 가장 나중에 삭제되는 완전 반대의 역할을 수행하고 있는데 스택을 이용하여 반대의 역할을 하는 큐를 구현할 수 있을까? 결론적으로는 가능하다. 하지만 추가적인 메모리 공간이 필요한데 일단 문제에서 요구하는 것은 다음과 같다. 구현된 큐는 push, pop, peek, empty 네 가지 연산을 수행할 수 있어야 한다. 큐를 구현할 때는 Stack 자료구조에서 지원하는 연산(push, pop, size, empty 등)만 사용해야 한다.. 더보기
Binary Tree Inorder Traversal (Stack) LeetCode의 Queue, Stack 튜토리얼에서 Stack과 DFS 섹션의 마지막 문제인 Binary Tree Inorder Traversal다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 이전까지는 주로 재귀를 이용하여 DFS를 구현했다면 이번에는 명확하게 Stack 자료구조를 활용하여 문제를 풀어보라고 하고 있다. 그럼에도 불구하고 역시 .. 더보기
Target Sum (DFS) LeetCode의 Queue, Stack 튜토리얼에서 Stack, DFS의 세 번째 문제인 Target Sum이다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com Stack과 DFS에 포함된 문제긴 하지만 Stack을 활용한 풀이는 거의 찾아볼 수 없고 나도 Stack으로 풀어보려다가 포기했다. DFS를 활용할 수 있지만 DP, Dynamic Prog.. 더보기