Algorithm
-
[array] 3sumAlgorithm/LeetCode 2025. 2. 1. 15:01
✅ Problemhttps://leetcode.com/problems/3sum/description/ ✅ Approach & Solution방식) 정렬 후 투 포인터 사용더보기3개를 찾아야 할 때 : 1개는 고정해두고 나머지 2개를 찾자나머지 2개 찾을 때 투 포인터 사용정렬 필수 (배열의 인덱스 값 활용하지 않기 때문에 정렬 가능)class Solution { public List> threeSum(int[] nums) { int left, right, sum; List> results = new ArrayList(); Arrays.sort(nums); for (int i = 0; i 0 && nums[i] == nums[i-1]) { ..
-
[array] trapping rain waterAlgorithm/LeetCode 2025. 1. 26. 15:50
✅ Problemhttps://leetcode.com/problems/trapping-rain-water/description/ ✅ Approach & Solution조건물이 고이려면 양 쪽이 막혀있어야 한다.양 쪽 높이 중 더 낮은 높이까지만 물이 고일 수 있다. 방식1) 투 포인터 사용더보기배열이 주어졌을 때 O(N) 시간복잡도로 구현하려면 투 포인터 방식 사용배열의 양 끝에서 시작, left class Solution { public int trap(int[] height) { int volume = 0; int left = 0; int right = height.length - 1; int leftMax = height[left]; ..
-
[array] two sumAlgorithm/LeetCode 2025. 1. 25. 13:51
✅ Problemhttps://leetcode.com/problems/two-sum/description/ ✅ Approach & Solution방식1) 브루트 포스로 계산더보기배열 순회하여 모든 조합 확인class Solution { public int[] twoSum(int[] nums, int target) { for (int i = 0; i 방식2) 첫 번째 수를 뺀 결과 키 조회더보기(target - nums[i])에 해당하는 값이 배열에 존재하는지 확인 (Map 사용)class Solution { public int[] twoSum(int[] nums, int target) { Map numsMap = new HashMap(); // 키와 값을..
-
[string] longest palindromic substringAlgorithm/LeetCode 2025. 1. 19. 13:26
✅ Problemhttps://leetcode.com/problems/longest-palindromic-substring/description/ ✅ Approach & Solution방식)더보기투 포인터 방식을 사용한다.offset, limit 사용 (아래 코드에서 left가 offset, maxLen이 limit이 됨)팰린드롬이 될 수 있는 최소 길이(1 초과)의 값을 기준으로 양 옆으로 확장해나간다.최소 길이 : 2 or 3ex) aa, abaclass Solution { int left, maxLen; public String longestPalindrome(String s) { // 문자 길이 저장 int len = s.length(); /..