본문 바로가기

반응형

Java

(376)
[LeetCode- Ch7. DFS & BFS] 5. 단어 검색 # https://leetcode.com/problems/word-search/ Word Search - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 원하는 단어를 배열에서 한줄로 이어진 상태로 찾으면 true 리턴 2. DFS를 이용 : 좌표, visited, word 1. 타임리밋 발생 class Point{ public int x,y; Point(int x, int y){ this.x=x; this.y=y; } } class Solution { stat..
[LeetCode- Ch7. DFS & BFS] 4. 단어 사다리 # https://leetcode.com/problems/word-ladder/ Word Ladder - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 2. DFS 시도 3. BFS 시도 DFS 시도 class Solution { static int answer=Integer.MAX_VALUE; static int rawnum=97; static List list; static String end; public int ladderLength(String beginW..
[LeetCode- Ch7. DFS & BFS] 3. 섬의 최대 면적 https://leetcode.com/problems/max-area-of-island/submissions/ Max Area of Island - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 섬의 개수를 구한다. 2. 구한 섬의 개수를 이용해, BFS를 시작할때 1로 시작해 돌면서 한칸씩 추가한다. class Point{ public int x, y; Point(int x, int y){ this.x=x; this.y=y; } } class Soluti..
[LeetCode- Ch7. DFS & BFS] 1. 섬의 수 https://leetcode.com/problems/number-of-islands/ Number of Islands - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. BFS 이용 -> 큐 -> 너비우선 2. DFS 이용 -> 스택 -> 깊이우선 1. BFS 큐 이용 import java.util.LinkedList; import java.util.Queue; class Point{ public int x, y; Point(int x, int y){ th..
[LeetCode- Ch6. 큐와 스택] 2. 유효한 괄호 https://leetcode.com/problems/valid-parentheses/ 1. 여는 괄호가 있을 때와 없을 때로 구분하기 : 여는 괄호 있으면 현재 값을 확인해 여는 괄호와 맞는 닫는 괄호면 여는괄호 삭제 2. 그 외의 경우엔 넣기 3. 스택이 비었을 때 true 반환 import java.util.Stack; public class Solution { public static boolean isValid(String s) { //open일때 close일 때 //모두 비었을 때 -> true; int n = s.length(); Stack stack = new Stack(); for (int i = 0; i < n; i++) { System.out.println(i + " " +s.char..
[LeetCode- Ch6. 큐와 스택] 1. 야구게임 ## (+ Switch, Integer.parseInt, Integer.valueOf) https://leetcode.com/problems/baseball-game/ Baseball Game - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 정규표현식을 이용해 음수 / 양수 포함한 숫자 체크 : 음수와 소수까지 처리 가능. public static boolean isNumeric(String s) { return s != null && s.matches("[-+]?\\d*\\.?\\d+"); } : 음수와 양수만 처리가능 public sta..
[LeetCode- Ch5. 연결 리스트] 1. 두 숫자 더하기 https://leetcode.com/problems/add-two-numbers/ Add Two Numbers - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. 노드의 구성요소 : 값과 다음 노드 2. 널이 아니면 반복해서 연결한다. -> 합이 일의자리가 넘어갈 때 사용하는 변수 carry /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next..
[LeetCode- Ch4. 투 포인터] 3. 문자열에서 모든 아나그램 찾기 https://leetcode.com/problems/find-all-anagrams-in-a-string/ Find All Anagrams in a String - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 1. rt만 이용해 추가할 경우 -> 타임리밋 발생 2. 초기값에서 문자를 하나씩 추가하는 방식으로 변경 1. rt만 이용해 반복해서 일치 확인 class Solution { public List findAnagrams(String s, String p..

반응형