본문 바로가기

반응형

Java/Java 알고리즘 LeetCode

(60)
[LeetCode- Ch4. 투 포인터] 2. 최대 2개의 고유 문자가있는 가장 긴 부분 문자열 ## https://www.lintcode.com/problem/928/description LintCode 炼码 www.lintcode.com 1. 배열을 이용한 풀이 2. Map을 이용한 풀이 1. 배열을 이용한 풀이 public class Solution { public int lengthOfLongestSubstringTwoDistinct(String s) { int[] map = new int[128]; int left = 0, right = 0, maxLen = 0, counter = 0; while (right < s.length()) { char c1 = s.charAt(right); if (map[c1] == 0) counter++; map[c1]++; right++; while (counte..
[LeetCode- Ch4. 투 포인터] 1. 단어중복없는 가장 긴 문자열 #(+ 슬라이딩 윈도우) https://leetcode.com/problems/longest-substring-without-repeating-characters/submissions/ Longest Substring Without Repeating Characters - 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. 중복 처리 체크 3. 중복시 0으로 초기화 class Solution { public int lengthOfLo..
[LeetCode- Ch3. 배열] 7. 나선형 매트릭스 # https://leetcode.com/problems/spiral-matrix/ Spiral Matrix - 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 나선형 매트릭스 :m x n의 요소로 이루어진 매트릭스로, 나선형 순으로 모든 요소를 출력 [동글뱅이] -> 상하좌우 위치 좌표값 변경을 이용해 문제해결 [0,0]:1, [0,1]:2, [0,2]:3, [0,3]:4 [1,0]:5, [1,1]:6, [1,2]:7, [1,3]:8 [2,0]:9, [2,1]:10..
[LeetCode- Ch3. 배열] 6. 누락된 범위 # (+ 삼항 연산자) https://www.lintcode.com/problem/641/ LintCode 炼码 www.lintcode.com 1. 예외처리 필수 2. 범위설정이 중요하다. -> 처음 / 중간 /끝 //배열이 존재하지 않을 때 list.add(makeRange(lower, upper)); //처음 list.add(makeRange(lower,nums[0]-1)); //중간 list.add(makeRange(nums[i]+1, nums[i+1]-1)); //끝 list.add(makeRange(nums[n-1]+1,upper)); 3. 삼항 연산자 사용한 범위 생성 메서드 작성 private static String makeRange(int start, int end){ //return start==end ? I..
[LeetCode- Ch3. 배열] 5. 빗물 담기 # [+ 분할과 정복] https://leetcode.com/problems/trapping-rain-water/ Trapping Rain Water - 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 문제 해결 [Divide & Conquer] 1. 물이 차는 영역 결정 -> Math.min -> 왼쪽벽과 오른쪽 벽의 값의 차이 만큼 물이 찬다. 2. 밑의 높이만큼 빼야한다. -> Math.min -밑의 높이 3. 필요한 자료구조 left[] : 왼쪽 벽 최대 높이 배열 right[]..
[LeetCode- Ch3. 배열] 4. 그룹 아나그램 (+ computeIfAbsent() ) https://leetcode.com/problems/group-anagrams/ Group Anagrams - 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 Map 자료구조를 만들어서 해결 1. toCharArray()후 정렬을 수행해 유일한 키로 만든다 2. 정렬한 키의 리스트가 존재하면, 존재하지 않으면 케이스분류해서 작성 1. 자바8 람다식 이용 : computeIfAbsent() 2. List 존재하면, 존재하지 않으면 분류 import java.uti..
[LeetCode- Ch3. 배열] 3. 부분배열 최댓값 (+ DP) https://leetcode.com/problems/maximum-subarray/submissions/ Maximum Subarray - 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. DFS 이용 : 타임리밋 발생 2. DP 이용 : 현재까지 가장 큰 값 저장하는 배열 -> dy[0] : 배열의 첫번째 값으로 초기화 -> 반복은 nums[1]부터 시작해, 이전 배열의 값을 더한 값과 현재 배열 값 중 Max값 선택 1. DFS 이용 class Solut..
[LeetCode- Ch3. 배열] 2. 일일 온도 # (+ arr+stack) https://leetcode.com/problems/daily-temperatures/submissions/ Daily Temperatures - 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. 이중 For문 이용 : 타임리밋 발생 2. for+while문: 타임리밋 발생 : 더 높은 온도 찾을 때 까지 while문으로 반복 3. 스택이용 : 스택에 배열의 인덱스를 넣고, 배열을 돌면서 인덱스간 차이를 이용 1. 이중 for문 이용 import java.u..

반응형