two pointers
-
[array] 3sum closestAlgorithm/LeetCode 2025. 4. 5. 12:28
✅ Problemhttps://leetcode.com/problems/3sum-closest/description/ ✅ Approach & Solution방식) 정렬 후 투 포인터 사용더보기3개를 찾아야 할 때 : 1개는 고정해두고 나머지 2개를 찾자나머지 2개 찾을 때 투 포인터 사용정렬 필수 (배열의 인덱스 값 활용하지 않기 때문에 정렬 가능)class Solution { public int threeSumClosest(int[] nums, int target) { int result = 0; // 반환할 세 수의 합 int minDiff = Integer.MAX_VALUE; // target과 세 수의 합의 최소 차이 int left, right, sum;..
-
[array] container with most waterAlgorithm/LeetCode 2025. 3. 22. 14:39
✅ Problemhttps://leetcode.com/problems/container-with-most-water/description ✅ Approach & Solution방식)더보기투 포인터 사용포인터 이동 시 한칸 씩 이동할 필요없음, 최소 height보다 클 때까지 이동시킴결과값이 커지려면 minHeight 값이 커져야 함 (right - left 는 포인터 이동할 때마다 무조건 작아지는 값이니까)class Solution { public int maxArea(int[] height) { int maxArea = 0; int left = 0; int right = height.length - 1; while (left
-
[string] valid palindromeAlgorithm/LeetCode 2025. 3. 8. 11:05
✅ Problemhttps://leetcode.com/problems/valid-palindrome/description/ ✅ Approach & Solution방식)더보기주어진 문자열만 사용해 비교Character 클래스의 isLetterOrDigit(), toLowerCase()String 클래스의 charAt()투 포인터 사용해 문자열의 앞, 뒤 비교 class Solution { public boolean isPalindrome(String s) { // 조건 // 대소문자 상관 X // 문자 or 숫자 // 앞에서 읽었을 때, 뒤에서 읽었을 때 동일 boolean result = true; int start = 0;..